Add date formatting and parsing functions relative to a given time
PATCH 1/2 Add date formatting and parsing functions relative to a given time
The main purpose is to allow predictable testing of the code.
---
Rebased on current master.
cache.h | 2 + date.c | 150 ++++++++++++++++++++++++++++++++++++-------------------------- 2 files changed, 89 insertions(+), 63 deletions(-)
cache.h +2 -0
@@ -731,9 +731,11 @@ enum date_mode { };const char *show_date (unsigned long time,int timezone,enum date_mode mode); +const char *show_date_relative (unsigned long time,int tz,const struct timeval *now);int parse_date (const char *date,char *buf,int bufsize);void datestamp (char *buf,int bufsize);unsigned long approxidate (const char *); +unsigned long approxidate_relative (const char *date,const struct timeval *now);enum date_mode parse_date_format (const char *format);#define IDENT_WARN_ON_NO_NAME 1
date.c +87 -63
178 lines not on this page · raw message
Signature · 1 line
1.6.4.1.263.g468a
PATCH 2/2 Allow testing of _relative family of time formatting and parsing functions
To complement the testability of approxidate.
--- test-date.c | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-)
test-date.c +17 -0
@@ -4,6 +4,17 @@ int main(int argc, char **argv) {int i; +struct tm tm; +struct timeval when = {0 ,0 }; + tm.tm_sec =0 ; + tm.tm_min =0 ; + tm.tm_hour =12 ; + tm.tm_mday =1 ; + tm.tm_mon =0 /* January */ ; + tm.tm_year =90 /* 1990 */ ; + tm.tm_isdst =-1 ; + when.tv_sec =mktime (&tm); +for (i =1 ; i < argc; i++) {char result[100 ];time_t t;
@@ -15,6 +26,12 @@ int main(int argc, char **argv) t =approxidate (argv[i]);printf ("%s -> %s\n" , argv[i],ctime (&t)); + + t =approxidate_relative (argv[i], &when); +printf ("relative: %s -> %s\n" , argv[i],ctime (&t)); + +printf ("relative: %s, out of %s\n" , +show_date_relative (t,0 , &when),ctime (&t)); }return 0 ; }
Signature · 1 line
1.6.4.1.263.g468a
date.c static int local_tzoffset(unsigned long time) · line 89
Quoted 3 lines · Alex Riesen
date.c +3
-0+const char *show_date_relative (unsigned long time,int tz,const struct timeval *now) +{ +static char timebuf[100 /* TODO: can be optimized */ ];
This was 200 in the original version. I doubt that it makes a difference, but I think in a refactoring patch I think it is best to simply reorganize and make no other changes.
Quoted 1 line
+static unsigned long
approximation(const char *date, const struct timeval *tv)
I know it's static, but this is a terribly undescriptive function name. Approximation of what? Can we call it approxidate_internal or something?
-Peff
Quoted 9 lines · Jeff King
date.c static int local_tzoffset(unsigned long time) · line 89
Quoted 3 lines · Alex Riesen
date.c +3
-0+const char *show_date_relative (unsigned long time,int tz,const struct timeval *now) +{ + static char timebuf[100 /* TODO: can be optimized */];This was 200 in the original version. I doubt that it makes a difference, but I think in a refactoring patch I think it is best to simply reorganize and make no other changes.
Yes, I just noticed that 200 was much too much, made the note to fix it sometime and forgot the note in the final submission.
Quoted 5 lines
Quoted 1 line
+static unsigned long
approximation(const char *date, const struct timeval *tv)I know it's static, but this is a terribly undescriptive function name. Approximation of what? Can we call it
approxidate_internalor something?
Been there, tried that. Didn't like it, because it didn't feel enough approxidate (the original) anymore. Not even internally, because of missing parse_date. My other attempts were guessdate and approxidate_bottom_half (but only very shortly).
The "approximation", if you consider the functions arguments, seems to me the closest to what the function _is_. OTOH, maybe I should have used a verb...