@matheusbsbs / matheus-bagliano / commits / 9644c06163
Fork 0

Export parse_date_basic() to convert a date string to timestamp

approxidate() is not appropriate for reading machine-written dates because it guesses instead of erroring out on malformed dates. parse_date() is less convenient since it returns its output as a string. So export the underlying function that writes a timestamp. While at it, change the return value to match the usual convention: return 0 for success and -1 for failure. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Acked-by: Ramkumar Ramachandra <artagnon@gmail.com> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Nieder committed Jul 15, 2010 at 18:22 UTC 9644c0616374f8c621b10793b1732b26a0482820
2 files changed +7 -8
cache.h
+1
@@ -811,6 +811,7 @@ const char *show_date_relative(unsigned long time, int tz,
811 char *timebuf,
812 size_t timebuf_size);
813 int parse_date(const char *date, char *buf, int bufsize);
814 +int parse_date_basic(const char *date, unsigned long *timestamp, int *offset);
815 void datestamp(char *buf, int bufsize);
816 #define approxidate(s) approxidate_careful((s), NULL)
817 unsigned long approxidate_careful(const char *, int *);
date.c
+6 -8
@@ -586,7 +586,7 @@ static int date_string(unsigned long date, int offset, char *buf, int len)
586
587 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
588 (i.e. English) day/month names, and it doesn't work correctly with %z. */
589 -int parse_date_toffset(const char *date, unsigned long *timestamp, int *offset)
589 +int parse_date_basic(const char *date, unsigned long *timestamp, int *offset)
590 {
591 struct tm tm;
592 int tm_gmt;
@@ -642,17 +642,16 @@ int parse_date_toffset(const char *date, unsigned long *timestamp, int *offset)
642
643 if (!tm_gmt)
644 *timestamp -= *offset * 60;
645 - return 1; /* success */
645 + return 0; /* success */
646 }
647
648 int parse_date(const char *date, char *result, int maxlen)
649 {
650 unsigned long timestamp;
651 int offset;
652 - if (parse_date_toffset(date, &timestamp, &offset) > 0)
653 - return date_string(timestamp, offset, result, maxlen);
654 - else
652 + if (parse_date_basic(date, &timestamp, &offset))
653 return -1;
654 + return date_string(timestamp, offset, result, maxlen);
655 }
656
657 enum date_mode parse_date_format(const char *format)
@@ -1004,9 +1003,8 @@ unsigned long approxidate_relative(const char *date, const struct timeval *tv)
1003 int offset;
1004 int errors = 0;
1005
1007 - if (parse_date_toffset(date, &timestamp, &offset) > 0)
1006 + if (!parse_date_basic(date, &timestamp, &offset))
1007 return timestamp;
1009 -
1008 return approxidate_str(date, tv, &errors);
1009 }
1010
@@ -1019,7 +1017,7 @@ unsigned long approxidate_careful(const char *date, int *error_ret)
1017 if (!error_ret)
1018 error_ret = &dummy;
1019
1022 - if (parse_date_toffset(date, &timestamp, &offset) > 0) {
1020 + if (!parse_date_basic(date, &timestamp, &offset)) {
1021 *error_ret = 0;
1022 return timestamp;
1023 }