Skip to content
Snippets Groups Projects
Verified Commit 5fcf3fac authored by Tuomas Ahola's avatar Tuomas Ahola
Browse files

approxidate: overwrite mday for now, today and yesterday


In c27cc94f (approxidate: handle pending number for
"specials", 2018-11-02) "Jan 5 yesterday" was given as
an example of an approxidate string where "the number
shouldn't be respected at all".  Likewise, "tea today"
shouldn't respect the usual tea-time logic of going
backwards in time; instead "today" should be able to
override the mday change possibly caused by "tea".

Add a new format "today" as an alias of "now", and make
it and "yesterday" overwrite mday.  Make date_time() use
the new method for deferred mday adjustment, and add tests
to cover the changes.

Signed-off-by: default avatarTuomas Ahola <taahol@utu.fi>
parent 1c241daf
No related branches found
No related tags found
No related merge requests found
...@@ -1127,45 +1127,47 @@ static void pending_number(struct tm *tm, int *num) ...@@ -1127,45 +1127,47 @@ static void pending_number(struct tm *tm, int *num)
static void date_now(struct tm *tm, struct tm *now, int *num) static void date_now(struct tm *tm, struct tm *now, int *num)
{ {
*num = 0; *num = 0;
tm->tm_mday = -1;
update_tm(tm, now, 0); update_tm(tm, now, 0);
} }
static void date_yesterday(struct tm *tm, struct tm *now, int *num) static void date_yesterday(struct tm *tm, struct tm *now, int *num)
{ {
*num = 0; *num = 0;
tm->tm_mday = -1;
update_tm(tm, now, 24*60*60); update_tm(tm, now, 24*60*60);
} }
static void date_time(struct tm *tm, struct tm *now, int hour) static void date_time(struct tm *tm, int hour)
{ {
/* /*
* By default, "tea" and "noon" refer to last such time in the * By default, "tea" and "noon" refer to last such time in the
* past, be it today or yesterday. With a specified mday, * past, be it today or yesterday. With a specified mday,
* that logic is overridden. * or e.g. "noon today", that logic is overridden.
*/ */
if (tm->tm_mday < 0 && tm->tm_hour < hour) if (tm->tm_mday < 0 && tm->tm_hour < hour)
update_tm(tm, now, 24*60*60); tm->tm_mday = -2; /* eventually handled by update_tm() */
tm->tm_hour = hour; tm->tm_hour = hour;
tm->tm_min = 0; tm->tm_min = 0;
tm->tm_sec = 0; tm->tm_sec = 0;
} }
static void date_midnight(struct tm *tm, struct tm *now, int *num) static void date_midnight(struct tm *tm, struct tm *now UNUSED, int *num)
{ {
pending_number(tm, num); pending_number(tm, num);
date_time(tm, now, 0); date_time(tm, 0);
} }
static void date_noon(struct tm *tm, struct tm *now, int *num) static void date_noon(struct tm *tm, struct tm *now UNUSED, int *num)
{ {
pending_number(tm, num); pending_number(tm, num);
date_time(tm, now, 12); date_time(tm, 12);
} }
static void date_tea(struct tm *tm, struct tm *now, int *num) static void date_tea(struct tm *tm, struct tm *now UNUSED, int *num)
{ {
pending_number(tm, num); pending_number(tm, num);
date_time(tm, now, 17); date_time(tm, 17);
} }
static void date_pm(struct tm *tm, struct tm *now UNUSED, int *num) static void date_pm(struct tm *tm, struct tm *now UNUSED, int *num)
...@@ -1215,6 +1217,7 @@ static const struct special { ...@@ -1215,6 +1217,7 @@ static const struct special {
{ "AM", date_am }, { "AM", date_am },
{ "never", date_never }, { "never", date_never },
{ "now", date_now }, { "now", date_now },
{ "today", date_now },
{ NULL } { NULL }
}; };
......
...@@ -186,7 +186,11 @@ check_approxidate '6pm yesterday' '2009-08-29 18:00:00' ...@@ -186,7 +186,11 @@ check_approxidate '6pm yesterday' '2009-08-29 18:00:00'
check_approxidate '3:00' '2009-08-30 03:00:00' check_approxidate '3:00' '2009-08-30 03:00:00'
check_approxidate '15:00' '2009-08-30 15:00:00' check_approxidate '15:00' '2009-08-30 15:00:00'
check_approxidate 'noon today' '2009-08-30 12:00:00' check_approxidate 'noon today' '2009-08-30 12:00:00'
check_approxidate 'noon today' '2009-08-30 12:00:00' success '-12'
check_approxidate 'noon today' '2009-09-01 12:00:00' success '+36'
check_approxidate 'noon yesterday' '2009-08-29 12:00:00' check_approxidate 'noon yesterday' '2009-08-29 12:00:00'
check_approxidate 'noon yesterday' '2009-08-29 12:00:00' success '-12'
check_approxidate 'tea last saturday' '2009-08-29 17:00:00' success '-12'
check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00' check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00' success '-12' check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00' success '-12'
check_approxidate '10am noon' '2009-08-29 12:00:00' check_approxidate '10am noon' '2009-08-29 12:00:00'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment