@matheusbsbs / matheus-bagliano / commits / c5043cc185
Fork 0

Refactor parse_date for approxidate functions

approxidate_relative and approxidate_careful both use parse_date to dump the timestamp to a character buffer and parse it back into a long unsigned using strtoul(). Avoid doing this by creating a new parse_date_toffset method. Noticed-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ramkumar Ramachandra committed Jun 3, 2010 at 22:28 UTC c5043cc18579854681aa51ddb6e744fe2db24f51
1 file changed +37 -19
date.c
+37 -19
@@ -585,11 +585,17 @@ static int date_string(unsigned long date, int offset, char *buf, int len)
585
586 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
587 (i.e. English) day/month names, and it doesn't work correctly with %z. */
588 -int parse_date(const char *date, char *result, int maxlen)
588 +int parse_date_toffset(const char *date, unsigned long *timestamp, int *offset)
589 {
590 struct tm tm;
591 - int offset, tm_gmt;
592 - time_t then;
591 + int tm_gmt;
592 + unsigned long dummy_timestamp;
593 + int dummy_offset;
594 +
595 + if (!timestamp)
596 + timestamp = &dummy_timestamp;
597 + if (!offset)
598 + offset = &dummy_offset;
599
600 memset(&tm, 0, sizeof(tm));
601 tm.tm_year = -1;
@@ -599,7 +605,7 @@ int parse_date(const char *date, char *result, int maxlen)
605 tm.tm_hour = -1;
606 tm.tm_min = -1;
607 tm.tm_sec = -1;
602 - offset = -1;
608 + *offset = -1;
609 tm_gmt = 0;
610
611 for (;;) {
@@ -611,11 +617,11 @@ int parse_date(const char *date, char *result, int maxlen)
617 break;
618
619 if (isalpha(c))
614 - match = match_alpha(date, &tm, &offset);
620 + match = match_alpha(date, &tm, offset);
621 else if (isdigit(c))
616 - match = match_digit(date, &tm, &offset, &tm_gmt);
622 + match = match_digit(date, &tm, offset, &tm_gmt);
623 else if ((c == '-' || c == '+') && isdigit(date[1]))
618 - match = match_tz(date, &offset);
624 + match = match_tz(date, offset);
625
626 if (!match) {
627 /* BAD CRAP */
@@ -626,16 +632,26 @@ int parse_date(const char *date, char *result, int maxlen)
632 }
633
634 /* mktime uses local timezone */
629 - then = tm_to_time_t(&tm);
630 - if (offset == -1)
631 - offset = (then - mktime(&tm)) / 60;
635 + *timestamp = tm_to_time_t(&tm);
636 + if (*offset == -1)
637 + *offset = (*timestamp - mktime(&tm)) / 60;
638
633 - if (then == -1)
639 + if (*timestamp == -1)
640 return -1;
641
642 if (!tm_gmt)
637 - then -= offset * 60;
638 - return date_string(then, offset, result, maxlen);
643 + *timestamp -= *offset * 60;
644 + return 1; /* success */
645 +}
646 +
647 +int parse_date(const char *date, char *result, int maxlen)
648 +{
649 + unsigned long timestamp;
650 + int offset;
651 + if (parse_date_toffset(date, &timestamp, &offset) > 0)
652 + return date_string(timestamp, offset, result, maxlen);
653 + else
654 + return -1;
655 }
656
657 enum date_mode parse_date_format(const char *format)
@@ -983,11 +999,12 @@ static unsigned long approxidate_str(const char *date,
999
1000 unsigned long approxidate_relative(const char *date, const struct timeval *tv)
1001 {
986 - char buffer[50];
1002 + unsigned long timestamp;
1003 + int offset;
1004 int errors = 0;
1005
989 - if (parse_date(date, buffer, sizeof(buffer)) > 0)
990 - return strtoul(buffer, NULL, 0);
1006 + if (parse_date_toffset(date, &timestamp, &offset) > 0)
1007 + return timestamp;
1008
1009 return approxidate_str(date, tv, &errors);
1010 }
@@ -995,14 +1012,15 @@ unsigned long approxidate_relative(const char *date, const struct timeval *tv)
1012 unsigned long approxidate_careful(const char *date, int *error_ret)
1013 {
1014 struct timeval tv;
998 - char buffer[50];
1015 + unsigned long timestamp;
1016 + int offset;
1017 int dummy = 0;
1018 if (!error_ret)
1019 error_ret = &dummy;
1020
1003 - if (parse_date(date, buffer, sizeof(buffer)) > 0) {
1021 + if (parse_date_toffset(date, &timestamp, &offset) > 0) {
1022 *error_ret = 0;
1005 - return strtoul(buffer, NULL, 0);
1023 + return timestamp;
1024 }
1025
1026 gettimeofday(&tv, NULL);