Use a 64-bit 'timestamp_t' for all timestamps, rather than 'time_t'

This makes the time type unambiguous, and we can use G_TYPE_INT64 for it
in the divelist too.

It also implements a portable (and thread-safe) "utc_mkdate()" function
that acts kind of like gmtime_r(), but using the 64-bit timestamp_t.  It
matches our original "utc_mktime()".

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
Linus Torvalds 2012-09-19 17:35:52 -07:00
parent d14932058f
commit dce08deb34
10 changed files with 195 additions and 114 deletions

View file

@ -50,7 +50,7 @@ const char *tripflag_names[NUM_TRIPFLAGS] = { "TF_NONE", "NOTRIP", "INTRIP" };
enum {
DIVE_INDEX = 0,
DIVE_NR, /* int: dive->nr */
DIVE_DATE, /* time_t: dive->when */
DIVE_DATE, /* timestamp_t: dive->when */
DIVE_RATING, /* int: 0-5 stars */
DIVE_DEPTH, /* int: dive->maxdepth in mm */
DIVE_DURATION, /* int: in seconds */
@ -75,16 +75,16 @@ static gboolean dump_model_entry(GtkTreeModel *model, GtkTreePath *path,
char *location;
int idx, nr, duration;
struct dive *dive;
time_t when;
struct tm *tm;
timestamp_t when;
struct tm tm;
gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_NR, &nr, DIVE_DATE, &when,
DIVE_DURATION, &duration, DIVE_LOCATION, &location, -1);
tm = gmtime(&when);
utc_mkdate(when, &tm);
printf("iter %x:%x entry #%d : nr %d @ %04d-%02d-%02d %02d:%02d:%02d duration %d location %s ",
iter->stamp, iter->user_data, idx, nr,
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec,
tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec,
duration, location);
dive = get_dive(idx);
if (dive)
@ -379,8 +379,8 @@ static void date_data_func(GtkTreeViewColumn *col,
gpointer data)
{
int val, idx, nr;
struct tm *tm;
time_t when;
struct tm tm;
timestamp_t when;
char buffer[40];
gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_DATE, &val, -1);
@ -388,21 +388,21 @@ static void date_data_func(GtkTreeViewColumn *col,
/* 2038 problem */
when = val;
tm = gmtime(&when);
utc_mkdate(when, &tm);
if (idx < 0) {
snprintf(buffer, sizeof(buffer),
"Trip %s, %s %d, %d (%d dive%s)",
weekday(tm->tm_wday),
monthname(tm->tm_mon),
tm->tm_mday, tm->tm_year + 1900,
weekday(tm.tm_wday),
monthname(tm.tm_mon),
tm.tm_mday, tm.tm_year + 1900,
nr, nr > 1 ? "s" : "");
} else {
snprintf(buffer, sizeof(buffer),
"%s, %s %d, %d %02d:%02d",
weekday(tm->tm_wday),
monthname(tm->tm_mon),
tm->tm_mday, tm->tm_year + 1900,
tm->tm_hour, tm->tm_min);
weekday(tm.tm_wday),
monthname(tm.tm_mon),
tm.tm_mday, tm.tm_year + 1900,
tm.tm_hour, tm.tm_min);
}
g_object_set(renderer, "text", buffer, NULL);
}
@ -944,7 +944,7 @@ void update_dive_list_col_visibility(void)
return;
}
static GList *find_matching_trip(time_t when)
static GList *find_matching_trip(timestamp_t when)
{
GList *trip = dive_trip_list;
if (!trip || DIVE_TRIP(trip)->when > when)
@ -977,7 +977,7 @@ static gboolean dive_can_be_in_trip(int idx, struct dive *dive_trip)
{
struct dive *dive, *pdive;
int i = idx;
time_t when = dive_trip->when;
timestamp_t when = dive_trip->when;
dive = get_dive(idx);
/* if the dive is before the trip start but within the threshold
@ -1261,7 +1261,7 @@ void add_dive_cb(GtkWidget *menuitem, gpointer data)
void edit_trip_cb(GtkWidget *menuitem, GtkTreePath *path)
{
GtkTreeIter iter;
time_t when;
timestamp_t when;
struct dive *dive_trip;
GList *trip;
@ -1336,13 +1336,13 @@ static int copy_tree_node(GtkTreeIter *a, GtkTreeIter *b)
}
/* to avoid complicated special cases based on ordering or number of children,
we always take the first and last child and pick the smaller time_t (which
we always take the first and last child and pick the smaller timestamp_t (which
works regardless of ordering and also with just one child) */
static void update_trip_timestamp(GtkTreeIter *parent, struct dive *divetrip)
{
GtkTreeIter first_child, last_child;
int nr;
time_t t1, t2, tnew;
timestamp_t t1, t2, tnew;
if (gtk_tree_store_iter_depth(STORE(dive_list), parent) != 0 ||
gtk_tree_model_iter_n_children(MODEL(dive_list), parent) == 0)
@ -1367,7 +1367,7 @@ static GtkTreeIter *move_dive_between_trips(GtkTreeIter *dive_iter, GtkTreeIter
GtkTreeIter *sibling, gboolean before)
{
int idx;
time_t old_when, new_when;
timestamp_t old_when, new_when;
struct dive *dive, *old_divetrip, *new_divetrip;
GtkTreeIter *new_iter = malloc(sizeof(GtkTreeIter));
@ -1442,7 +1442,7 @@ static void turn_dive_into_trip(GtkTreePath *path)
{
GtkTreeIter iter, *newiter, newparent;
GtkTreePath *treepath;
time_t when;
timestamp_t when;
char *location;
int idx;
struct dive *dive;
@ -1484,7 +1484,7 @@ static void insert_trip_before(GtkTreePath *path)
copy_tree_node(&parent, &newparent);
gtk_tree_model_get(MODEL(dive_list), &iter, DIVE_INDEX, &idx, -1);
dive = get_dive(idx);
/* make sure that the time_t of the previous divetrip is correct before
/* make sure that the timestamp_t of the previous divetrip is correct before
* inserting a new one */
if (dive->when < prev_dive->when)
if (prev_dive->divetrip && prev_dive->divetrip->when < prev_dive->when)
@ -1693,7 +1693,7 @@ void merge_trips_cb(GtkWidget *menuitem, GtkTreePath *trippath)
GtkTreeIter thistripiter, prevtripiter, newiter, iter;
GtkTreeModel *tm = MODEL(dive_list);
GList *trip, *prevtrip;
time_t when;
timestamp_t when;
/* this only gets called when we are on a trip and there is another trip right before */
prevpath = gtk_tree_path_copy(trippath);
@ -1995,7 +1995,7 @@ GtkWidget *dive_list_create(void)
dive_list.listmodel = gtk_tree_store_new(DIVELIST_COLUMNS,
G_TYPE_INT, /* index */
G_TYPE_INT, /* nr */
G_TYPE_LONG, /* Date */
G_TYPE_INT64, /* Date */
G_TYPE_INT, /* Star rating */
G_TYPE_INT, /* Depth */
G_TYPE_INT, /* Duration */
@ -2011,7 +2011,7 @@ GtkWidget *dive_list_create(void)
dive_list.treemodel = gtk_tree_store_new(DIVELIST_COLUMNS,
G_TYPE_INT, /* index */
G_TYPE_INT, /* nr */
G_TYPE_LONG, /* Date */
G_TYPE_INT64, /* Date */
G_TYPE_INT, /* Star rating */
G_TYPE_INT, /* Depth */
G_TYPE_INT, /* Duration */
@ -2090,7 +2090,7 @@ void remove_autogen_trips()
{
GtkTreeIter iter;
GtkTreePath *path;
time_t when;
timestamp_t when;
int idx;
GList *trip;