696
return n;
697
}
698
699
+static void date_now(struct tm *tm, struct tm *now, int *num)
700
+{
701
+ update_tm(tm, now, 0);
702
+}
703
+
704
static void date_yesterday(struct tm *tm, struct tm *now, int *num)
705
{
706
update_tm(tm, now, 24*60*60);
775
{ "PM", date_pm },
776
{ "AM", date_am },
777
{ "never", date_never },
778
+ { "now", date_now },
779
{ NULL }
780
};
781
796
{ NULL }
797
};
798
793
-static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num)
799
+static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num, int *touched)
800
{
801
const struct typelen *tl;
802
const struct special *s;
810
int match = match_string(date, month_names[i]);
811
if (match >= 3) {
812
tm->tm_mon = i;
813
+ *touched = 1;
814
return end;
815
}
816
}
819
int len = strlen(s->name);
820
if (match_string(date, s->name) == len) {
821
s->fn(tm, now, num);
822
+ *touched = 1;
823
return end;
824
}
825
}
829
int len = strlen(number_name[i]);
830
if (match_string(date, number_name[i]) == len) {
831
*num = i;
832
+ *touched = 1;
833
return end;
834
}
835
}
827
- if (match_string(date, "last") == 4)
836
+ if (match_string(date, "last") == 4) {
837
*num = 1;
838
+ *touched = 1;
839
+ }
840
return end;
841
}
842
846
if (match_string(date, tl->type) >= len-1) {
847
update_tm(tm, now, tl->length * *num);
848
*num = 0;
849
+ *touched = 1;
850
return end;
851
}
852
tl++;
864
diff += 7*n;
865
866
update_tm(tm, now, diff * 24 * 60 * 60);
867
+ *touched = 1;
868
return end;
869
}
870
}
879
tm->tm_year--;
880
}
881
tm->tm_mon = n;
882
+ *touched = 1;
883
return end;
884
}
885
887
update_tm(tm, now, 0); /* fill in date fields if needed */
888
tm->tm_year -= *num;
889
*num = 0;
890
+ *touched = 1;
891
return end;
892
}
893
944
}
945
}
946
932
-static unsigned long approxidate_str(const char *date, const struct timeval *tv)
947
+static unsigned long approxidate_str(const char *date,
948
+ const struct timeval *tv,
949
+ int *error_ret)
950
{
951
int number = 0;
952
+ int touched = 0;
953
struct tm tm, now;
954
time_t time_sec;
955
969
if (isdigit(c)) {
970
pending_number(&tm, &number);
971
date = approxidate_digit(date-1, &tm, &number);
972
+ touched = 1;
973
continue;
974
}
975
if (isalpha(c))
957
- date = approxidate_alpha(date-1, &tm, &now, &number);
976
+ date = approxidate_alpha(date-1, &tm, &now, &number, &touched);
977
}
978
pending_number(&tm, &number);
979
+ if (!touched)
980
+ *error_ret = 1;
981
return update_tm(&tm, &now, 0);
982
}
983
984
unsigned long approxidate_relative(const char *date, const struct timeval *tv)
985
{
986
char buffer[50];
987
+ int errors = 0;
988
989
if (parse_date(date, buffer, sizeof(buffer)) > 0)
990
return strtoul(buffer, NULL, 0);
991
970
- return approxidate_str(date, tv);
992
+ return approxidate_str(date, tv, &errors);
993
}
994
973
-unsigned long approxidate(const char *date)
995
+unsigned long approxidate_careful(const char *date, int *error_ret)
996
{
997
struct timeval tv;
998
char buffer[50];
999
+ int dummy = 0;
1000
+ if (!error_ret)
1001
+ error_ret = &dummy;
1002
978
- if (parse_date(date, buffer, sizeof(buffer)) > 0)
1003
+ if (parse_date(date, buffer, sizeof(buffer)) > 0) {
1004
+ *error_ret = 0;
1005
return strtoul(buffer, NULL, 0);
1006
+ }
1007
1008
gettimeofday(&tv, NULL);
982
- return approxidate_str(date, &tv);
1009
+ return approxidate_str(date, &tv, error_ret);
1010
}