@@ -696,6 +696,11 @@ static unsigned long update_tm(struct tm *tm, struct tm *now, unsigned long sec)
696
696
return n ;
697
697
}
698
698
699
+ static void date_now (struct tm * tm , struct tm * now , int * num )
700
+ {
701
+ update_tm (tm , now , 0 );
702
+ }
703
+
699
704
static void date_yesterday (struct tm * tm , struct tm * now , int * num )
700
705
{
701
706
update_tm (tm , now , 24 * 60 * 60 );
@@ -770,6 +775,7 @@ static const struct special {
770
775
{ "PM" , date_pm },
771
776
{ "AM" , date_am },
772
777
{ "never" , date_never },
778
+ { "now" , date_now },
773
779
{ NULL }
774
780
};
775
781
@@ -790,7 +796,7 @@ static const struct typelen {
790
796
{ NULL }
791
797
};
792
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 )
794
800
{
795
801
const struct typelen * tl ;
796
802
const struct special * s ;
@@ -804,6 +810,7 @@ static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm
804
810
int match = match_string (date , month_names [i ]);
805
811
if (match >= 3 ) {
806
812
tm -> tm_mon = i ;
813
+ * touched = 1 ;
807
814
return end ;
808
815
}
809
816
}
@@ -812,6 +819,7 @@ static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm
812
819
int len = strlen (s -> name );
813
820
if (match_string (date , s -> name ) == len ) {
814
821
s -> fn (tm , now , num );
822
+ * touched = 1 ;
815
823
return end ;
816
824
}
817
825
}
@@ -821,11 +829,14 @@ static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm
821
829
int len = strlen (number_name [i ]);
822
830
if (match_string (date , number_name [i ]) == len ) {
823
831
* num = i ;
832
+ * touched = 1 ;
824
833
return end ;
825
834
}
826
835
}
827
- if (match_string (date , "last" ) == 4 )
836
+ if (match_string (date , "last" ) == 4 ) {
828
837
* num = 1 ;
838
+ * touched = 1 ;
839
+ }
829
840
return end ;
830
841
}
831
842
@@ -835,6 +846,7 @@ static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm
835
846
if (match_string (date , tl -> type ) >= len - 1 ) {
836
847
update_tm (tm , now , tl -> length * * num );
837
848
* num = 0 ;
849
+ * touched = 1 ;
838
850
return end ;
839
851
}
840
852
tl ++ ;
@@ -852,6 +864,7 @@ static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm
852
864
diff += 7 * n ;
853
865
854
866
update_tm (tm , now , diff * 24 * 60 * 60 );
867
+ * touched = 1 ;
855
868
return end ;
856
869
}
857
870
}
@@ -866,13 +879,15 @@ static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm
866
879
tm -> tm_year -- ;
867
880
}
868
881
tm -> tm_mon = n ;
882
+ * touched = 1 ;
869
883
return end ;
870
884
}
871
885
872
886
if (match_string (date , "years" ) >= 4 ) {
873
887
update_tm (tm , now , 0 ); /* fill in date fields if needed */
874
888
tm -> tm_year -= * num ;
875
889
* num = 0 ;
890
+ * touched = 1 ;
876
891
return end ;
877
892
}
878
893
@@ -929,9 +944,12 @@ static void pending_number(struct tm *tm, int *num)
929
944
}
930
945
}
931
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 )
933
950
{
934
951
int number = 0 ;
952
+ int touched = 0 ;
935
953
struct tm tm , now ;
936
954
time_t time_sec ;
937
955
@@ -951,33 +969,42 @@ static unsigned long approxidate_str(const char *date, const struct timeval *tv)
951
969
if (isdigit (c )) {
952
970
pending_number (& tm , & number );
953
971
date = approxidate_digit (date - 1 , & tm , & number );
972
+ touched = 1 ;
954
973
continue ;
955
974
}
956
975
if (isalpha (c ))
957
- date = approxidate_alpha (date - 1 , & tm , & now , & number );
976
+ date = approxidate_alpha (date - 1 , & tm , & now , & number , & touched );
958
977
}
959
978
pending_number (& tm , & number );
979
+ if (!touched )
980
+ * error_ret = 1 ;
960
981
return update_tm (& tm , & now , 0 );
961
982
}
962
983
963
984
unsigned long approxidate_relative (const char * date , const struct timeval * tv )
964
985
{
965
986
char buffer [50 ];
987
+ int errors = 0 ;
966
988
967
989
if (parse_date (date , buffer , sizeof (buffer )) > 0 )
968
990
return strtoul (buffer , NULL , 0 );
969
991
970
- return approxidate_str (date , tv );
992
+ return approxidate_str (date , tv , & errors );
971
993
}
972
994
973
- unsigned long approxidate (const char * date )
995
+ unsigned long approxidate_careful (const char * date , int * error_ret )
974
996
{
975
997
struct timeval tv ;
976
998
char buffer [50 ];
999
+ int dummy = 0 ;
1000
+ if (!error_ret )
1001
+ error_ret = & dummy ;
977
1002
978
- if (parse_date (date , buffer , sizeof (buffer )) > 0 )
1003
+ if (parse_date (date , buffer , sizeof (buffer )) > 0 ) {
1004
+ * error_ret = 0 ;
979
1005
return strtoul (buffer , NULL , 0 );
1006
+ }
980
1007
981
1008
gettimeofday (& tv , NULL );
982
- return approxidate_str (date , & tv );
1009
+ return approxidate_str (date , & tv , error_ret );
983
1010
}
0 commit comments