From 4cc28c4e59201c27c94bec55013260772bea651a Mon Sep 17 00:00:00 2001
From: Maxime Bizon <mbizon@freebox.fr>
Date: Thu, 1 Apr 2021 23:38:39 +0200
Subject: [PATCH] constant time_t

---
 src/rrd.h                | 40 +++++++++++++++++++++++++---------------
 src/rrd_cgi.c            | 12 ++++++------
 src/rrd_create.c         |  4 ++--
 src/rrd_daemon.c         | 16 ++++++++--------
 src/rrd_dump.c           |  2 +-
 src/rrd_fetch.c          | 16 ++++++++--------
 src/rrd_fetch_libdbi.c   | 12 ++++++------
 src/rrd_first.c          |  6 +++---
 src/rrd_format.c         |  2 +-
 src/rrd_format.h         |  4 ++--
 src/rrd_graph.c          | 26 +++++++++++++-------------
 src/rrd_graph.h          | 28 ++++++++++++++--------------
 src/rrd_graph_helper.c   |  2 +-
 src/rrd_last.c           |  6 +++---
 src/rrd_lastupdate.c     |  4 ++--
 src/rrd_open.c           |  6 +++---
 src/rrd_parsetime.c      | 12 ++++++------
 src/rrd_restore.c        | 14 +++++++-------
 src/rrd_rpncalc.c        | 10 +++++-----
 src/rrd_thread_safe_nt.c |  6 +++---
 src/rrd_tool.c           |  4 ++--
 src/rrd_tool.h           | 12 ++++++------
 src/rrd_update.c         | 30 +++++++++++++++---------------
 src/rrd_xport.c          | 20 ++++++++++----------
 src/strftime.c           |  2 +-
 src/win32comp.c          |  6 +++---
 26 files changed, 156 insertions(+), 146 deletions(-)

diff --git a/src/rrd.h b/src/rrd.h
index e02ebb1..b1bda24 100644
--- a/src/rrd.h
+++ b/src/rrd.h
@@ -71,6 +71,16 @@ extern    "C" {
 #include <stdio.h>      /* for FILE */
 #include <string.h>
 
+#include <stdint.h>
+#if UINTPTR_MAX == 0xffffffff
+typedef int32_t rrdtime_t;
+#elif UINTPTR_MAX == 0xffffffffffffffff
+typedef int64_t rrdtime_t;
+#else
+#error "could not guess arch long size"
+#endif
+
+
 /* Formerly rrd_nan_inf.h */
 #ifndef DNAN
 # define DNAN rrd_set_to_DNAN()
@@ -179,8 +189,8 @@ extern    "C" {
     int       rrd_fetch(
     int,
     char **,
-    time_t *,
-    time_t *,
+    rrdtime_t *,
+    rrdtime_t *,
     unsigned long *,
     unsigned long *,
     char ***,
@@ -194,11 +204,11 @@ extern    "C" {
     int       rrd_tune(
     int,
     char **);
-    time_t    rrd_last(
+    rrdtime_t    rrd_last(
     int,
     char **);
     int rrd_lastupdate(int argc, char **argv);
-    time_t    rrd_first(
+    rrdtime_t    rrd_first(
     int,
     char **);
     int       rrd_resize(
@@ -212,8 +222,8 @@ extern    "C" {
     int,
     char **,
     int *,
-    time_t *,
-    time_t *,
+    rrdtime_t *,
+    rrdtime_t *,
     unsigned long *,
     unsigned long *,
     char ***,
@@ -227,7 +237,7 @@ extern    "C" {
     int       rrd_create_r(
     const char *filename,
     unsigned long pdp_step,
-    time_t last_up,
+    rrdtime_t last_up,
     int argc,
     const char **argv);
     rrd_info_t *rrd_info_r(
@@ -243,8 +253,8 @@ extern    "C" {
     int rrd_fetch_r (
             const char *filename,
             const char *cf,
-            time_t *start,
-            time_t *end,
+            rrdtime_t *start,
+            rrdtime_t *end,
             unsigned long *step,
             unsigned long *ds_cnt,
             char ***ds_namv,
@@ -252,13 +262,13 @@ extern    "C" {
     int       rrd_dump_r(
     const char *filename,
     char *outname);
-    time_t    rrd_last_r (const char *filename);
+    rrdtime_t    rrd_last_r (const char *filename);
     int rrd_lastupdate_r (const char *filename,
-            time_t *ret_last_update,
+            rrdtime_t *ret_last_update,
             unsigned long *ret_ds_count,
             char ***ret_ds_names,
             char ***ret_last_ds);
-    time_t    rrd_first_r(
+    rrdtime_t    rrd_first_r(
     const char *filename,
     int rraindex);
 
@@ -305,8 +315,8 @@ extern    "C" {
 int       rrd_proc_start_end(
     rrd_time_value_t *,
     rrd_time_value_t *,
-    time_t *,
-    time_t *);
+    rrdtime_t *,
+    rrdtime_t *);
 
 /* HELPER FUNCTIONS */
     void      rrd_set_error(
@@ -419,7 +429,7 @@ int       rrd_proc_start_end(
     rrd_file_t *rrd_file,
     int rra_idx,
     unsigned long rra_row,
-    time_t rra_time)
+    rrdtime_t rra_time)
               RRD_DEPRECATED;
     unsigned long rrd_select_initial_row(
     rrd_file_t *rrd_file,
diff --git a/src/rrd_cgi.c b/src/rrd_cgi.c
index 9848715..2933da1 100644
--- a/src/rrd_cgi.c
+++ b/src/rrd_cgi.c
@@ -114,7 +114,7 @@ char     *printstrftime(
 
 /** HTTP protocol needs special format, and GMT time **/
 char     *http_time(
-    time_t *);
+    rrdtime_t *);
 
 /* return a pointer to newly allocated copy of this string */
 char     *stralloc(
@@ -562,7 +562,7 @@ int main(
                "Content-Length: %zd\n", strlen(buffer));
 
         if (labs(goodfor) > 0) {
-            time_t    now;
+            rrdtime_t    now;
 
             now = time(NULL);
             printf("Last-Modified: %s\n", http_time(&now));
@@ -733,7 +733,7 @@ char     *printstrftime(
     char     *parsetime_error = NULL;
     char      formatted[MAX_STRFTIME_SIZE];
     struct tm *the_tm;
-    time_t    start_tmp, end_tmp;
+    rrdtime_t    start_tmp, end_tmp;
 
     /* Make sure that we were given the right number of args */
     if (argc != 4) {
@@ -984,7 +984,7 @@ char     *printtimelast(
     long argc,
     const char **args)
 {
-    time_t    last;
+    rrdtime_t    last;
     struct tm tm_last;
     char     *buf;
 
@@ -1016,7 +1016,7 @@ char     *printtimenow(
     long argc,
     const char **args)
 {
-    time_t    now = time(NULL);
+    rrdtime_t    now = time(NULL);
     struct tm tm_now;
     char     *buf;
 
@@ -1308,7 +1308,7 @@ int parse(
 }
 
 char     *http_time(
-    time_t *now)
+    rrdtime_t *now)
 {
     struct tm *tmptime;
     static char buf[60];
diff --git a/src/rrd_create.c b/src/rrd_create.c
index 50e2665..d0d73a2 100644
--- a/src/rrd_create.c
+++ b/src/rrd_create.c
@@ -45,7 +45,7 @@ int rrd_create(
     };
     int       option_index = 0;
     int       opt;
-    time_t    last_up = time(NULL) - 10;
+    rrdtime_t    last_up = time(NULL) - 10;
     unsigned long pdp_step = 300;
     rrd_time_value_t last_up_tv;
     char     *parsetime_error = NULL;
@@ -119,7 +119,7 @@ int rrd_create(
 int rrd_create_r(
     const char *filename,
     unsigned long pdp_step,
-    time_t last_up,
+    rrdtime_t last_up,
     int argc,
     const char **argv)
 {
diff --git a/src/rrd_daemon.c b/src/rrd_daemon.c
index 17153ce..3fe1871 100644
--- a/src/rrd_daemon.c
+++ b/src/rrd_daemon.c
@@ -128,7 +128,7 @@ struct listen_socket_s
   int family;
 
   /* state for BATCH processing */
-  time_t batch_start;
+  rrdtime_t batch_start;
   int batch_cmd;
 
   /* buffered IO */
@@ -150,7 +150,7 @@ struct command_s;
 typedef struct command_s command_t;
 /* note: guard against "unused" warnings in the handlers */
 #define DISPATCH_PROTO	listen_socket_t UNUSED(*sock),\
-			time_t UNUSED(now),\
+			rrdtime_t UNUSED(now),\
 			char  UNUSED(*buffer),\
 			size_t UNUSED(buffer_size)
 
@@ -178,7 +178,7 @@ struct cache_item_s
   char *file;
   char **values;
   size_t values_num;
-  time_t last_flush_time;
+  rrdtime_t last_flush_time;
   double last_update_stamp;
 #define CI_FLAGS_IN_TREE  (1<<0)
 #define CI_FLAGS_IN_QUEUE (1<<1)
@@ -190,8 +190,8 @@ struct cache_item_s
 
 struct callback_flush_data_s
 {
-  time_t now;
-  time_t abs_timeout;
+  rrdtime_t now;
+  rrdtime_t abs_timeout;
   char **keys;
   size_t keys_num;
 };
@@ -639,7 +639,7 @@ static int send_response (listen_socket_t *sock, response_code rc,
   return 0;
 } /* }}} */
 
-static void wipe_ci_values(cache_item_t *ci, time_t when)
+static void wipe_ci_values(cache_item_t *ci, rrdtime_t when)
 {
   ci->values = NULL;
   ci->values_num = 0;
@@ -2001,7 +2001,7 @@ static int journal_replay (const char *file) /* {{{ */
   int fail_cnt = 0;
   uint64_t line = 0;
   char entry[CMD_MAX];
-  time_t now;
+  rrdtime_t now;
 
   if (file == NULL) return 0;
 
@@ -2233,7 +2233,7 @@ static void *connection_thread_main (void *args) /* {{{ */
     char *cmd;
     ssize_t cmd_len;
     ssize_t rbytes;
-    time_t now;
+    rrdtime_t now;
 
     struct pollfd pollfd;
     int status;
diff --git a/src/rrd_dump.c b/src/rrd_dump.c
index 560d1a9..7773bf8 100644
--- a/src/rrd_dump.c
+++ b/src/rrd_dump.c
@@ -69,7 +69,7 @@ int rrd_dump_cb_r(
     void *user)
 {
     unsigned int i, ii, ix, iii = 0;
-    time_t    now;
+    rrdtime_t    now;
     char      somestring[255];
     rrd_value_t my_cdp;
     off_t     rra_base, rra_start, rra_next;
diff --git a/src/rrd_fetch.c b/src/rrd_fetch.c
index cf5dc24..0f4aa65 100644
--- a/src/rrd_fetch.c
+++ b/src/rrd_fetch.c
@@ -61,8 +61,8 @@
 int rrd_fetch(
     int argc,
     char **argv,
-    time_t *start,
-    time_t *end,        /* which time frame do you want ?
+    rrdtime_t *start,
+    rrdtime_t *end,        /* which time frame do you want ?
                          * will be changed to represent reality */
     unsigned long *step,    /* which stepsize do you want? 
                              * will be changed to represent reality */
@@ -71,7 +71,7 @@ int rrd_fetch(
     rrd_value_t **data)
 {                       /* two dimensional array containing the data */
     long      step_tmp = 1;
-    time_t    start_tmp = 0, end_tmp = 0;
+    rrdtime_t    start_tmp = 0, end_tmp = 0;
     const char *cf;
     char *opt_daemon = NULL;
     int status;
@@ -183,8 +183,8 @@ int rrd_fetch(
 int rrd_fetch_r(
     const char *filename,   /* name of the rrd */
     const char *cf,     /* which consolidation function ? */
-    time_t *start,
-    time_t *end,        /* which time frame do you want ?
+    rrdtime_t *start,
+    rrdtime_t *end,        /* which time frame do you want ?
                          * will be changed to represent reality */
     unsigned long *step,    /* which stepsize do you want? 
                              * will be changed to represent reality */
@@ -205,8 +205,8 @@ int rrd_fetch_r(
 int rrd_fetch_fn(
     const char *filename,   /* name of the rrd */
     enum cf_en cf_idx,  /* which consolidation function ? */
-    time_t *start,
-    time_t *end,        /* which time frame do you want ?
+    rrdtime_t *start,
+    rrdtime_t *end,        /* which time frame do you want ?
                          * will be changed to represent reality */
     unsigned long *step,    /* which stepsize do you want? 
                              * will be changed to represent reality */
@@ -215,7 +215,7 @@ int rrd_fetch_fn(
     rrd_value_t **data)
 {                       /* two dimensional array containing the data */
     long      i, ii;
-    time_t    cal_start, cal_end, rra_start_time, rra_end_time;
+    rrdtime_t    cal_start, cal_end, rra_start_time, rra_end_time;
     long      best_full_rra = 0, best_part_rra = 0, chosen_rra =
         0, rra_pointer = 0;
     long      best_full_step_diff = 0, best_part_step_diff =
diff --git a/src/rrd_fetch_libdbi.c b/src/rrd_fetch_libdbi.c
index c1ad26f..590f768 100644
--- a/src/rrd_fetch_libdbi.c
+++ b/src/rrd_fetch_libdbi.c
@@ -20,7 +20,7 @@ struct sql_table_helper {
 /* the prototypes */
 static void _sql_close(struct sql_table_helper* th);
 static int _sql_setparam(struct sql_table_helper* th,char* key, char* value);
-static int _sql_fetchrow(struct sql_table_helper* th,time_t *timestamp, rrd_value_t *value,int ordered);
+static int _sql_fetchrow(struct sql_table_helper* th,rrdtime_t *timestamp, rrd_value_t *value,int ordered);
 static char* _find_next_separator(char* start,char separator);
 static char* _find_next_separator_twice(char*start,char separator);
 static char _hexcharhelper(char c);
@@ -201,10 +201,10 @@ static int _sql_setparam(struct sql_table_helper* th,char* key, char* value) {
   return 0;
 }
 
-static int _sql_fetchrow(struct sql_table_helper* th,time_t *timestamp, rrd_value_t *value,int ordered) {
+static int _sql_fetchrow(struct sql_table_helper* th,rrdtime_t *timestamp, rrd_value_t *value,int ordered) {
   char* dbi_errstr=NULL;
   char sql[10240];
-  time_t startt=0,endt=0;
+  rrdtime_t startt=0,endt=0;
   /*connect to the database if needed */
   if (! th->conn) {
       rrd_set_error( "libdbi no parameters set for libdbi",th->filename,dbi_errstr);
@@ -367,8 +367,8 @@ int
 rrd_fetch_fn_libdbi(
     const char     *filename,  /* name of the rrd */
     enum cf_en     UNUSED(cf_idx), /* consolidation function */
-    time_t         *start,
-    time_t         *end,       /* which time frame do you want ?
+    rrdtime_t         *start,
+    rrdtime_t         *end,       /* which time frame do you want ?
 			        * will be changed to represent reality */
     unsigned long  *step,      /* which stepsize do you want? 
 				* will be changed to represent reality */
@@ -465,7 +465,7 @@ rrd_fetch_fn_libdbi(
 #ifdef HAVE_TIMEZONE
     extern long timezone;
 #endif
-    time_t t=time(NULL);
+    rrdtime_t t=time(NULL);
     localtime_r(&t,&tm);
 #ifdef HAVE_TM_GMTOFF
     gmt_offset=tm.TM_GMTOFF;
diff --git a/src/rrd_first.c b/src/rrd_first.c
index 34b0737..2d55fec 100644
--- a/src/rrd_first.c
+++ b/src/rrd_first.c
@@ -10,7 +10,7 @@
 #include "rrd_tool.h"
 
 
-time_t rrd_first(
+rrdtime_t rrd_first(
     int argc,
     char **argv)
 {
@@ -57,12 +57,12 @@ time_t rrd_first(
 }
 
 
-time_t rrd_first_r(
+rrdtime_t rrd_first_r(
     const char *filename,
     const int rraindex)
 {
     off_t     rra_start, timer;
-    time_t    then = -1;
+    rrdtime_t    then = -1;
     rrd_t     rrd;
     rrd_file_t *rrd_file;
 
diff --git a/src/rrd_format.c b/src/rrd_format.c
index ac85fc5..522aef2 100644
--- a/src/rrd_format.c
+++ b/src/rrd_format.c
@@ -108,7 +108,7 @@ off_t rrd_get_header_size(
     return sizeof(stat_head_t) + \
         sizeof(ds_def_t) * rrd->stat_head->ds_cnt + \
         sizeof(rra_def_t) * rrd->stat_head->rra_cnt + \
-        ( atoi(rrd->stat_head->version) < 3 ? sizeof(time_t) : sizeof(live_head_t) ) + \
+        ( atoi(rrd->stat_head->version) < 3 ? sizeof(rrdtime_t) : sizeof(live_head_t) ) + \
         sizeof(pdp_prep_t) * rrd->stat_head->ds_cnt + \
         sizeof(cdp_prep_t) * rrd->stat_head->ds_cnt * rrd->stat_head->rra_cnt + \
         sizeof(rra_ptr_t) * rrd->stat_head->rra_cnt;
diff --git a/src/rrd_format.h b/src/rrd_format.h
index e383662..f929b31 100644
--- a/src/rrd_format.h
+++ b/src/rrd_format.h
@@ -279,7 +279,7 @@ typedef struct rra_def_t {
  ****************************************************************************/
 
 typedef struct live_head_t {
-    time_t    last_up;  /* when was rrd last updated */
+    rrdtime_t    last_up;  /* when was rrd last updated */
     long      last_up_usec; /* micro seconds part of the
                                update timestamp. Always >= 0 */
 } live_head_t;
@@ -396,7 +396,7 @@ typedef struct rrd_t {
     ds_def_t *ds_def;   /* list of data source definitions */
     rra_def_t *rra_def; /* list of round robin archive def */
     live_head_t *live_head; /* rrd v >= 3 last_up with us */
-    time_t   *legacy_last_up;   /* rrd v < 3 last_up time */
+    rrdtime_t   *legacy_last_up;   /* rrd v < 3 last_up time */
     pdp_prep_t *pdp_prep;   /* pdp data prep area */
     cdp_prep_t *cdp_prep;   /* cdp prep area */
     rra_ptr_t *rra_ptr; /* list of rra pointers */
diff --git a/src/rrd_graph.c b/src/rrd_graph.c
index 5f70a38..c503740 100644
--- a/src/rrd_graph.c
+++ b/src/rrd_graph.c
@@ -176,7 +176,7 @@ gfx_color_t graph_col[] =   /* default colors */
 /* initialize with xtr(im,0); */
 int xtr(
     image_desc_t *im,
-    time_t mytime)
+    rrdtime_t mytime)
 {
     static double pixie;
 
@@ -644,8 +644,8 @@ void apply_gridfit(
 void reduce_data(
     enum cf_en cf,      /* which consolidation function ? */
     unsigned long cur_step, /* step the data currently is in */
-    time_t *start,      /* start, end and step as requested ... */
-    time_t *end,        /* ... by the application will be   ... */
+    rrdtime_t *start,      /* start, end and step as requested ... */
+    rrdtime_t *end,        /* ... by the application will be   ... */
     unsigned long *step,    /* ... adjusted to represent reality    */
     unsigned long *ds_cnt,  /* number of data sources in file */
     rrd_value_t **data)
@@ -991,7 +991,7 @@ int data_calc(
     int       dataidx;
     long     *steparray, rpi;
     int       stepcnt;
-    time_t    now;
+    rrdtime_t    now;
     rpnstack_t rpnstack;
 
     rpnstack_init(&rpnstack);
@@ -1162,7 +1162,7 @@ int data_calc(
 
                 /* 3rd arg of rpn_calc is for OP_VARIABLE lookups;
                  * in this case we are advancing by timesteps;
-                 * we use the fact that time_t is a synonym for long
+                 * we use the fact that rrdtime_t is a synonym for long
                  */
                 if (rpn_calc(rpnp, &rpnstack, (long) now,
                              im->gdes[gdi].data, ++dataidx) == -1) {
@@ -1393,8 +1393,8 @@ static int find_first_weekday(void){
 
 /* identify the point where the first gridline, label ... gets placed */
 
-time_t find_first_time(
-    time_t start,       /* what is the initial time */
+rrdtime_t find_first_time(
+    rrdtime_t start,       /* what is the initial time */
     enum tmt_en baseint,    /* what is the basic interval */
     long basestep       /* how many if these do we jump a time */
     )
@@ -1460,14 +1460,14 @@ time_t find_first_time(
 }
 
 /* identify the point where the next gridline, label ... gets placed */
-time_t find_next_time(
-    time_t current,     /* what is the initial time */
+rrdtime_t find_next_time(
+    rrdtime_t current,     /* what is the initial time */
     enum tmt_en baseint,    /* what is the basic interval */
     long basestep       /* how many if these do we jump a time */
     )
 {
     struct tm tm;
-    time_t    madetime;
+    rrdtime_t    madetime;
 
     localtime_r(&current, &tm);
 
@@ -1532,7 +1532,7 @@ int print_calc(
     int       prline_cnt = 0;
 
     /* wow initializing tmvdef is quite a task :-) */
-    time_t    now = time(NULL);
+    rrdtime_t    now = time(NULL);
 
     localtime_r(&now, &tmvdef);
     for (i = 0; i < im->gdes_c; i++) {
@@ -2462,7 +2462,7 @@ void vertical_grid(
     image_desc_t *im)
 {
     int       xlab_sel; /* which sort of label and grid ? */
-    time_t    ti, tilab, timajor;
+    rrdtime_t    ti, tilab, timajor;
     long      factor;
     char      graph_label[100];
     double    X0, Y0, Y1;   /* points for filled graph and more */
@@ -4185,7 +4185,7 @@ void rrd_graph_options(
     int       stroff;
     char     *parsetime_error = NULL;
     char      scan_gtm[12], scan_mtm[12], scan_ltm[12], col_nam[12];
-    time_t    start_tmp = 0, end_tmp = 0;
+    rrdtime_t    start_tmp = 0, end_tmp = 0;
     long      long_tmp;
     rrd_time_value_t start_tv, end_tv;
     long unsigned int color;
diff --git a/src/rrd_graph.h b/src/rrd_graph.h
index 525e019..7b5cdcb 100644
--- a/src/rrd_graph.h
+++ b/src/rrd_graph.h
@@ -117,7 +117,7 @@ typedef struct vdef_t {
     enum vdef_op_en op;
     double    param;    /* parameter for function, if applicable */
     double    val;      /* resulting value */
-    time_t    when;     /* timestamp, if applicable */
+    rrdtime_t    when;     /* timestamp, if applicable */
 } vdef_t;
 
 typedef struct xlab_t {
@@ -172,18 +172,18 @@ typedef struct graph_desc_t {
     int       strftm;   /* should the VDEF legend be formated with strftime */
     double    leg_x, leg_y; /* location of legend */
     double    yrule;    /* value for y rule line and for VDEF */
-    time_t    xrule;    /* time for x rule line and for VDEF */
+    rrdtime_t    xrule;    /* time for x rule line and for VDEF */
     vdef_t    vf;       /* instruction for VDEF function */
     rpnp_t   *rpnp;     /* instructions for CDEF function */
 
     /* SHIFT implementation */
     int       shidx;    /* gdes reference for offset (-1 --> constant) */
-    time_t    shval;    /* offset if shidx is -1 */
-    time_t    shift;    /* current shift applied */
+    rrdtime_t    shval;    /* offset if shidx is -1 */
+    rrdtime_t    shift;    /* current shift applied */
 
     /* description of data fetched for the graph element */
-    time_t    start, end;   /* timestaps for first and last data element */
-    time_t    start_orig, end_orig; /* timestaps for first and last data element */
+    rrdtime_t    start, end;   /* timestaps for first and last data element */
+    rrdtime_t    start_orig, end_orig; /* timestaps for first and last data element */
     unsigned long step; /* time between samples */
     unsigned long step_orig;    /* time between samples */
     unsigned long ds_cnt;   /* how many data sources are there in the fetch */
@@ -228,7 +228,7 @@ typedef struct image_desc_t {
     double    ygridstep;    /* user defined step for y grid */
     int       ylabfact; /* every how many y grid shall a label be written ? */
     double    tabwidth; /* tabwdith */
-    time_t    start, end;   /* what time does the graph cover */
+    rrdtime_t    start, end;   /* what time does the graph cover */
     unsigned long step; /* any preference for the default step ? */
     rrd_value_t minval, maxval; /* extreme values in the data */
     int       rigid;    /* do not expand range even with 
@@ -288,7 +288,7 @@ typedef struct image_desc_t {
 /* Prototypes */
 int       xtr(
     image_desc_t *,
-    time_t);
+    rrdtime_t);
 double    ytr(
     image_desc_t *,
     double);
@@ -318,8 +318,8 @@ void      apply_gridfit(
 void      reduce_data(
     enum cf_en,
     unsigned long,
-    time_t *,
-    time_t *,
+    rrdtime_t *,
+    rrdtime_t *,
     unsigned long *,
     unsigned long *,
     rrd_value_t **);
@@ -337,12 +337,12 @@ int       data_calc(
     image_desc_t *);
 int       data_proc(
     image_desc_t *);
-time_t    find_first_time(
-    time_t,
+rrdtime_t    find_first_time(
+    rrdtime_t,
     enum tmt_en,
     long);
-time_t    find_next_time(
-    time_t,
+rrdtime_t    find_next_time(
+    rrdtime_t,
     enum tmt_en,
     long);
 int       print_calc(
diff --git a/src/rrd_graph_helper.c b/src/rrd_graph_helper.c
index 00922a5..08be520 100644
--- a/src/rrd_graph_helper.c
+++ b/src/rrd_graph_helper.c
@@ -888,7 +888,7 @@ int rrd_parse_def(
     char      command[7];   /* step, start, end, reduce */
     char      tmpstr[256];
     rrd_time_value_t start_tv, end_tv;
-    time_t    start_tmp = 0, end_tmp = 0;
+    rrdtime_t    start_tmp = 0, end_tmp = 0;
     char     *parsetime_error = NULL;
 
     start_tv.type = end_tv.type = ABSOLUTE_TIME;
diff --git a/src/rrd_last.c b/src/rrd_last.c
index e6ad2dc..3594b5f 100644
--- a/src/rrd_last.c
+++ b/src/rrd_last.c
@@ -9,7 +9,7 @@
 #include "rrd_tool.h"
 #include "rrd_client.h"
 
-time_t rrd_last(
+rrdtime_t rrd_last(
     int argc,
     char **argv)
 {
@@ -65,10 +65,10 @@ time_t rrd_last(
     return (rrd_last_r (argv[optind]));
 }
 
-time_t rrd_last_r(
+rrdtime_t rrd_last_r(
     const char *filename)
 {
-    time_t    lastup = -1;
+    rrdtime_t    lastup = -1;
     rrd_file_t *rrd_file;
 
     rrd_t     rrd;
diff --git a/src/rrd_lastupdate.c b/src/rrd_lastupdate.c
index acc1821..c62c497 100644
--- a/src/rrd_lastupdate.c
+++ b/src/rrd_lastupdate.c
@@ -12,7 +12,7 @@
 
 int rrd_lastupdate (int argc, char **argv)
 {
-    time_t    last_update;
+    rrdtime_t    last_update;
     char    **ds_names;
     char    **last_ds;
     unsigned long ds_count, i;
@@ -90,7 +90,7 @@ int rrd_lastupdate (int argc, char **argv)
 } /* int rrd_lastupdate */
 
 int rrd_lastupdate_r(const char *filename,
-        time_t *ret_last_update,
+        rrdtime_t *ret_last_update,
         unsigned long *ret_ds_count,
         char ***ret_ds_names,
         char ***ret_last_ds)
diff --git a/src/rrd_open.c b/src/rrd_open.c
index 2a454c5..cc4125c 100644
--- a/src/rrd_open.c
+++ b/src/rrd_open.c
@@ -356,9 +356,9 @@ rrd_file_t *rrd_open(
         }
 #if defined USE_MADVISE
         /* the live_head will be needed soonish, so hint accordingly */
-        madvise(data + PAGE_START(offset), sizeof(time_t), MADV_WILLNEED);
+        madvise(data + PAGE_START(offset), sizeof(rrdtime_t), MADV_WILLNEED);
 #endif
-        __rrd_read(rrd->legacy_last_up, time_t,
+        __rrd_read(rrd->legacy_last_up, rrdtime_t,
                    1);
 
         rrd->live_head->last_up = *rrd->legacy_last_up;
@@ -785,7 +785,7 @@ void rrd_notify_row(
     rrd_file_t UNUSED(*rrd_file),
     int UNUSED(rra_idx),
     unsigned long UNUSED(rra_row),
-    time_t UNUSED(rra_time))
+    rrdtime_t UNUSED(rra_time))
 {
 }
 
diff --git a/src/rrd_parsetime.c b/src/rrd_parsetime.c
index 1b59f45..22c5db2 100644
--- a/src/rrd_parsetime.c
+++ b/src/rrd_parsetime.c
@@ -666,7 +666,7 @@ static char *assign_date(
         year += 100;    /* Allow year 2000-2037 to be specified as   */
     }
     /* 00-37 until the problem of 2038 year will */
-    /* arise for unices with 32-bit time_t :)    */
+    /* arise for unices with 32-bit rrdtime_t :)    */
     if (year < 70) {
         panic(e("won't handle dates before epoch (01/01/1970), sorry"));
     }
@@ -685,8 +685,8 @@ static char *assign_date(
 static char *day(
     rrd_time_value_t * ptv)
 {
-    /* using time_t seems to help portability with 64bit oses */
-    time_t    mday = 0, wday, mon, year = ptv->tm.tm_year;
+    /* using rrdtime_t seems to help portability with 64bit oses */
+    rrdtime_t    mday = 0, wday, mon, year = ptv->tm.tm_year;
     int       tlen;
 
     switch (sc_tokid) {
@@ -839,7 +839,7 @@ char     *rrd_parsetime(
     const char *tspec,
     rrd_time_value_t * ptv)
 {
-    time_t    now = time(NULL);
+    rrdtime_t    now = time(NULL);
     int       hr = 0;
 
     /* this MUST be initialized to zero for midnight/noon/teatime */
@@ -999,8 +999,8 @@ char     *rrd_parsetime(
 int rrd_proc_start_end(
     rrd_time_value_t * start_tv,
     rrd_time_value_t * end_tv,
-    time_t *start,
-    time_t *end)
+    rrdtime_t *start,
+    rrdtime_t *end)
 {
     if (start_tv->type == RELATIVE_TO_END_TIME &&   /* same as the line above */
         end_tv->type == RELATIVE_TO_START_TIME) {
diff --git a/src/rrd_restore.c b/src/rrd_restore.c
index b368417..8c6ef9d 100644
--- a/src/rrd_restore.c
+++ b/src/rrd_restore.c
@@ -219,12 +219,12 @@ static int get_xml_string(
 }
 
  
-static int get_xml_time_t(
+static int get_xml_rrdtime_t(
     xmlTextReaderPtr reader,
-    time_t *value)
+    rrdtime_t *value)
 {    
     xmlChar *text;
-    time_t temp;    
+    rrdtime_t temp;    
     if ((text = get_xml_text(reader)) != NULL){
         errno = 0;        
 #ifdef TIME_T_IS_32BIT
@@ -233,7 +233,7 @@ static int get_xml_time_t(
 #ifdef TIME_T_IS_64BIT
         temp = strtoll((char *)text,NULL, 0);        
 #else
-        if (sizeof(time_t) == 4){
+        if (sizeof(rrdtime_t) == 4){
             temp = strtol((char *)text,NULL, 0);
         } else {
             temp = strtoll((char *)text,NULL, 0);
@@ -241,7 +241,7 @@ static int get_xml_time_t(
 #endif
 #endif    
         if (errno>0){
-            rrd_set_error("ling %d: get_xml_time_t from '%s' %s",
+            rrd_set_error("ling %d: get_xml_rrdtime_t from '%s' %s",
                           xmlTextReaderGetParserLineNumber(reader),
                           text,rrd_strerror(errno));
             xmlFree(text);            
@@ -252,7 +252,7 @@ static int get_xml_time_t(
         return 0;
     }
     return -1;
-} /* get_xml_time_t */
+} /* get_xml_rrdtime_t */
 
 static int get_xml_ulong(
     xmlTextReaderPtr reader,
@@ -1025,7 +1025,7 @@ static int parse_tag_rrd(
             status = get_xml_ulong(reader,
                                         &rrd->stat_head->pdp_step);
         else if (xmlStrcasecmp(element, (const xmlChar *) "lastupdate") == 0) {
-                status = get_xml_time_t(reader, &rrd->live_head->last_up);
+                status = get_xml_rrdtime_t(reader, &rrd->live_head->last_up);
         }
         else if (xmlStrcasecmp(element, (const xmlChar *) "ds") == 0){            
             xmlFree(element);
diff --git a/src/rrd_rpncalc.c b/src/rrd_rpncalc.c
index b3d7760..3ecc473 100644
--- a/src/rrd_rpncalc.c
+++ b/src/rrd_rpncalc.c
@@ -19,7 +19,7 @@ short     addop2str(
     char **result_str,
     unsigned short *offset);
 int       tzoffset(
-    time_t);            /* used to implement LTIME */
+    rrdtime_t);            /* used to implement LTIME */
 
 short rpn_compact(
     rpnp_t *rpnp,
@@ -813,7 +813,7 @@ short rpn_calc(
 		/* the real calculation */
 		double val=DNAN;
 		/* the info on the datasource */
-		time_t  dsstep = (time_t) rpnp[rpi - 1].step;
+		rrdtime_t  dsstep = (rrdtime_t) rpnp[rpi - 1].step;
 		int    dscount = rpnp[rpi - 1].ds_cnt;
 		int   locstep = (int)ceil((float)locstepsize/(float)dsstep);
 
@@ -880,8 +880,8 @@ short rpn_calc(
                 rrd_set_error("malformed trend arguments");
                 return -1;
             } else {
-                time_t    dur = (time_t) rpnstack->s[stptr];
-                time_t    step = (time_t) rpnp[rpi - 2].step;
+                rrdtime_t    dur = (rrdtime_t) rpnstack->s[stptr];
+                rrdtime_t    step = (rrdtime_t) rpnp[rpi - 2].step;
 
                 if (output_idx + 1 >= (int) ceil((float) dur / (float) step)) {
                     int       ignorenan = (rpnp[rpi].op == OP_TREND);
@@ -953,7 +953,7 @@ short rpn_calc(
 /* figure out what the local timezone offset for any point in
    time was. Return it in seconds */
 int tzoffset(
-    time_t now)
+    rrdtime_t now)
 {
     int       gm_sec, gm_min, gm_hour, gm_yday, gm_year,
         l_sec, l_min, l_hour, l_yday, l_year;
diff --git a/src/rrd_thread_safe_nt.c b/src/rrd_thread_safe_nt.c
index 8313794..7060061 100644
--- a/src/rrd_thread_safe_nt.c
+++ b/src/rrd_thread_safe_nt.c
@@ -98,7 +98,7 @@ const char *rrd_strerror(
  * there much be a re-entrant version of these somewhere in win32 land
  */
 struct tm *localtime_r(
-    const time_t *timep,
+    const rrdtime_t *timep,
     struct tm *result)
 {
     struct tm *local;
@@ -113,7 +113,7 @@ struct tm *localtime_r(
 }
 
 char     *ctime_r(
-    const time_t *timep,
+    const rrdtime_t *timep,
     char *result)
 {
     char     *local;
@@ -128,7 +128,7 @@ char     *ctime_r(
 }
 
 struct tm *gmtime_r(
-    const time_t *timep,
+    const rrdtime_t *timep,
     struct tm *result)
 {
     struct tm *local;
diff --git a/src/rrd_tool.c b/src/rrd_tool.c
index 187b3e3..393c960 100644
--- a/src/rrd_tool.c
+++ b/src/rrd_tool.c
@@ -678,7 +678,7 @@ int HandleInputLine(
     else if (strcmp("update", argv[1]) == 0)
         rrd_update(argc - 1, &argv[1]);
     else if (strcmp("fetch", argv[1]) == 0) {
-        time_t    start, end, ti;
+        rrdtime_t    start, end, ti;
         unsigned long step, ds_cnt, i, ii;
         rrd_value_t *data, *datai;
         char    **ds_namv;
@@ -706,7 +706,7 @@ int HandleInputLine(
 #ifdef HAVE_RRD_GRAPH
         int       xxsize;
         unsigned long int j = 0;
-        time_t    start, end, ti;
+        rrdtime_t    start, end, ti;
         unsigned long step, col_cnt, row_cnt;
         rrd_value_t *data, *ptr;
         char    **legend_v;
diff --git a/src/rrd_tool.h b/src/rrd_tool.h
index f9e0016..89e8a1e 100644
--- a/src/rrd_tool.h
+++ b/src/rrd_tool.h
@@ -28,13 +28,13 @@ extern    "C" {
 #include <io.h>         /* for chdir   */
 
     struct tm *localtime_r(
-    const time_t *timep,
+    const rrdtime_t *timep,
     struct tm *result);
     char     *ctime_r(
-    const time_t *timep,
+    const rrdtime_t *timep,
     char *result);
     struct tm *gmtime_r(
-    const time_t *timep,
+    const rrdtime_t *timep,
     struct tm *result);
     char     *strtok_r(
     char *str,
@@ -87,8 +87,8 @@ extern    "C" {
     rrd_t *rrd);
     int rrd_fetch_fn (const char *filename,
             enum cf_en cf_idx,
-            time_t *start,
-            time_t *end,
+            rrdtime_t *start,
+            rrdtime_t *end,
             unsigned long *step,
             unsigned long *ds_cnt,
             char ***ds_namv,
@@ -97,7 +97,7 @@ extern    "C" {
 
 #ifdef HAVE_LIBDBI
 int rrd_fetch_fn_libdbi(const char *filename, enum cf_en cf_idx,
- 			time_t *start,time_t *end,
+ 			rrdtime_t *start,rrdtime_t *end,
  			unsigned long *step,
  			unsigned long *ds_cnt,
  			char        ***ds_namv,
diff --git a/src/rrd_update.c b/src/rrd_update.c
index b5bf46e..b3ad4a4 100644
--- a/src/rrd_update.c
+++ b/src/rrd_update.c
@@ -34,7 +34,7 @@
 
 #ifndef __MINGW32__
 struct timeval {
-    time_t    tv_sec;   /* seconds */
+    rrdtime_t    tv_sec;   /* seconds */
     long      tv_usec;  /* microseconds */
 };
 #endif
@@ -97,7 +97,7 @@ static int process_arg(
     rrd_t *rrd,
     rrd_file_t *rrd_file,
     unsigned long rra_begin,
-    time_t *current_time,
+    rrdtime_t *current_time,
     unsigned long *current_time_usec,
     rrd_value_t *pdp_temp,
     rrd_value_t *pdp_new,
@@ -116,7 +116,7 @@ static int parse_ds(
     long *tmpl_idx,
     char *input,
     unsigned long tmpl_cnt,
-    time_t *current_time,
+    rrdtime_t *current_time,
     unsigned long *current_time_usec,
     int version);
 
@@ -124,7 +124,7 @@ static int get_time_from_reading(
     rrd_t *rrd,
     char timesyntax,
     char **updvals,
-    time_t *current_time,
+    rrdtime_t *current_time,
     unsigned long *current_time_usec,
     int version);
 
@@ -254,7 +254,7 @@ static int write_to_rras(
     rrd_file_t *rrd_file,
     unsigned long *rra_step_cnt,
     unsigned long rra_begin,
-    time_t current_time,
+    rrdtime_t current_time,
     unsigned long *skip_update,
     rrd_info_t ** pcdp_summary);
 
@@ -264,7 +264,7 @@ static int write_RRA_row(
     unsigned long rra_idx,
     unsigned short CDP_scratch_idx,
     rrd_info_t ** pcdp_summary,
-    time_t rra_time);
+    rrdtime_t rra_time);
 
 static int smooth_all_rras(
     rrd_t *rrd,
@@ -296,7 +296,7 @@ static void normalize_time(
  * current_time_usec is set to 0 if the version number is 1 or 2.
  */
 static void initialize_time(
-    time_t *current_time,
+    rrdtime_t *current_time,
     unsigned long *current_time_usec,
     int version)
 {
@@ -502,7 +502,7 @@ int _rrd_update(
                          * transported by the tmplt index */
     unsigned long tmpl_cnt = 2; /* time and data */
     rrd_t     rrd;
-    time_t    current_time = 0;
+    rrdtime_t    current_time = 0;
     unsigned long current_time_usec = 0;    /* microseconds part of current time */
     char    **updvals;
     int       schedule_smooth = 0;
@@ -771,7 +771,7 @@ static int process_arg(
     rrd_t *rrd,
     rrd_file_t *rrd_file,
     unsigned long rra_begin,
-    time_t *current_time,
+    rrdtime_t *current_time,
     unsigned long *current_time_usec,
     rrd_value_t *pdp_temp,
     rrd_value_t *pdp_new,
@@ -874,7 +874,7 @@ static int parse_ds(
     long *tmpl_idx,
     char *input,
     unsigned long tmpl_cnt,
-    time_t *current_time,
+    rrdtime_t *current_time,
     unsigned long *current_time_usec,
     int version)
 {
@@ -940,7 +940,7 @@ static int get_time_from_reading(
     rrd_t *rrd,
     char timesyntax,
     char **updvals,
-    time_t *current_time,
+    rrdtime_t *current_time,
     unsigned long *current_time_usec,
     int version)
 {
@@ -1900,13 +1900,13 @@ static int write_to_rras(
     rrd_file_t *rrd_file,
     unsigned long *rra_step_cnt,
     unsigned long rra_begin,
-    time_t current_time,
+    rrdtime_t current_time,
     unsigned long *skip_update,
     rrd_info_t ** pcdp_summary)
 {
     unsigned long rra_idx;
     unsigned long rra_start;
-    time_t    rra_time = 0; /* time of update for a RRA */
+    rrdtime_t    rra_time = 0; /* time of update for a RRA */
 
     unsigned long ds_cnt = rrd->stat_head->ds_cnt;
     
@@ -1986,7 +1986,7 @@ static int write_RRA_row(
     unsigned long rra_idx,
     unsigned short CDP_scratch_idx,
     rrd_info_t ** pcdp_summary,
-    time_t rra_time)
+    rrdtime_t rra_time)
 {
     unsigned long ds_idx, cdp_idx;
     rrd_infoval_t iv;
@@ -2078,7 +2078,7 @@ static int write_changes_to_disk(
         }
     } else {
         if (rrd_write(rrd_file, rrd->legacy_last_up,
-                      sizeof(time_t) * 1) != sizeof(time_t) * 1) {
+                      sizeof(rrdtime_t) * 1) != sizeof(rrdtime_t) * 1) {
             rrd_set_error("rrd_write live_head to rrd");
             return -1;
         }
diff --git a/src/rrd_xport.c b/src/rrd_xport.c
index 7c2fe61..2762563 100644
--- a/src/rrd_xport.c
+++ b/src/rrd_xport.c
@@ -22,8 +22,8 @@ int       rrd_xport(
     int,
     char **,
     int *,
-    time_t *,
-    time_t *,
+    rrdtime_t *,
+    rrdtime_t *,
     unsigned long *,
     unsigned long *,
     char ***,
@@ -31,8 +31,8 @@ int       rrd_xport(
 
 int       rrd_xport_fn(
     image_desc_t *,
-    time_t *,
-    time_t *,
+    rrdtime_t *,
+    rrdtime_t *,
     unsigned long *,
     unsigned long *,
     char ***,
@@ -45,8 +45,8 @@ int rrd_xport(
     int argc,
     char **argv,
     int UNUSED(*xsize),
-    time_t *start,
-    time_t *end,        /* which time frame do you want ?
+    rrdtime_t *start,
+    rrdtime_t *end,        /* which time frame do you want ?
                          * will be changed to represent reality */
     unsigned long *step,    /* which stepsize do you want? 
                              * will be changed to represent reality */
@@ -55,7 +55,7 @@ int rrd_xport(
     rrd_value_t **data)
 {                       /* two dimensional array containing the data */
     image_desc_t im;
-    time_t    start_tmp = 0, end_tmp = 0;
+    rrdtime_t    start_tmp = 0, end_tmp = 0;
     rrd_time_value_t start_tv, end_tv;
     char     *parsetime_error = NULL;
 
@@ -186,8 +186,8 @@ int rrd_xport(
 
 int rrd_xport_fn(
     image_desc_t *im,
-    time_t *start,
-    time_t *end,        /* which time frame do you want ?
+    rrdtime_t *start,
+    rrdtime_t *end,        /* which time frame do you want ?
                          * will be changed to represent reality */
     unsigned long *step,    /* which stepsize do you want? 
                              * will be changed to represent reality */
@@ -305,7 +305,7 @@ int rrd_xport_fn(
     for (dst_row = 0; (int) dst_row < (int) row_cnt; dst_row++) {
         for (i = 0; i < (int) (*col_cnt); i++) {
             long vidx = im->gdes[ref_list[i]].vidx;
-            time_t now = *start + dst_row * *step;
+            rrdtime_t now = *start + dst_row * *step;
             (*dstptr++) = im->gdes[vidx].data[(unsigned long)
                                               floor((double)
                                                     (now - im->gdes[vidx].start)
diff --git a/src/strftime.c b/src/strftime.c
index 3928a6f..3c05af1 100644
--- a/src/strftime.c
+++ b/src/strftime.c
@@ -346,7 +346,7 @@ int main(
 {
     int       len;
     char     *fmt;
-    time_t    now;
+    rrdtime_t    now;
 
     time(&now);
 
diff --git a/src/win32comp.c b/src/win32comp.c
index 457e69e..c57be56 100644
--- a/src/win32comp.c
+++ b/src/win32comp.c
@@ -4,7 +4,7 @@
 #include <time.h>
 
 struct tm *localtime_r(
-    const time_t *t,
+    const rrdtime_t *t,
     struct tm *r)
 {
     struct tm *temp;
@@ -15,7 +15,7 @@ struct tm *localtime_r(
 }
 
 struct tm *gmtime_r(
-    const time_t *t,
+    const rrdtime_t *t,
     struct tm *r)
 {
     struct tm *temp;
@@ -26,7 +26,7 @@ struct tm *gmtime_r(
 }
 
 char     *ctime_r(
-    const time_t *t,
+    const rrdtime_t *t,
     char *buf)
 {
     char     *temp;
-- 
2.17.1

