diff options
| author | kre <kre@NetBSD.org> | 2017-03-22 01:49:36 +0000 |
|---|---|---|
| committer | kre <kre@NetBSD.org> | 2017-03-22 01:49:36 +0000 |
| commit | 3e341abaac42623fc7e617fc0ad86a23fba43508 (patch) | |
| tree | 7e129dcfe58a2275d3920d6c74feaa5f0566592f /lib/libutil | |
| parent | 4c0d6fc01e78b9d07ab9c4e4fec74e0cd527fd46 (diff) | |
Make parsedate handle "12 noon" and "12 midnight" (including when
the time is "12:00" or "12:00:00) - but only for exactly 12 o'clock.
"12:00:01" is am or pm, not noon or midnight.
"12 am" remains as an alias for "12 midnight", and "12 pm" for midnight,
though both are strictly invalid (and meaningless.)
Note that "12 pm" means 00:00:00 (ie: midnight at the start of the
day, not at the end.)
Diffstat (limited to 'lib/libutil')
| -rw-r--r-- | lib/libutil/parsedate.3 | 18 | ||||
| -rw-r--r-- | lib/libutil/parsedate.y | 66 |
2 files changed, 71 insertions, 13 deletions
diff --git a/lib/libutil/parsedate.3 b/lib/libutil/parsedate.3 index 89ae9fe9a29..0068804cb58 100644 --- a/lib/libutil/parsedate.3 +++ b/lib/libutil/parsedate.3 @@ -1,4 +1,4 @@ -.\" $NetBSD: parsedate.3,v 1.22 2016/12/23 06:01:41 abhinav Exp $ +.\" $NetBSD: parsedate.3,v 1.23 2017/03/22 01:49:36 kre Exp $ .\" .\" Copyright (c) 2006 The NetBSD Foundation, Inc. .\" All rights reserved. @@ -27,7 +27,7 @@ .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.Dd December 7, 2015 +.Dd March 22, 2017 .Dt PARSEDATE 3 .Os .Sh NAME @@ -393,9 +393,21 @@ otherwise years less than 100 mean 1900 + .Fa year . .It 3 +The +.Fn parsedate +function accepts +.Dq "12 am" +where +.Dq "12 midnight" +is correct, and similarly +.Dq "12 pm" +for +.Dq "12 noon" . +The correct forms are also accepted. +.It 4 There are various weird cases that are hard to explain, but are nevertheless considered correct. -.It 4 +.It 5 It is very hard to specify years BC, and in any case, conversions of times before the diff --git a/lib/libutil/parsedate.y b/lib/libutil/parsedate.y index 02f2868b144..cf828143a08 100644 --- a/lib/libutil/parsedate.y +++ b/lib/libutil/parsedate.y @@ -14,7 +14,7 @@ #include <sys/cdefs.h> #ifdef __RCSID -__RCSID("$NetBSD: parsedate.y,v 1.30 2017/03/22 00:59:06 kre Exp $"); +__RCSID("$NetBSD: parsedate.y,v 1.31 2017/03/22 01:49:36 kre Exp $"); #endif #include <stdio.h> @@ -64,10 +64,10 @@ typedef enum _DSTMODE { } DSTMODE; /* -** Meridian: am, pm, or 24-hour style. +** Meridian: am, pm, or 24-hour style (plus "noon" and "midnight"). */ typedef enum _MERIDIAN { - MERam, MERpm, MER24 + MERam, MERpm, MER24, MER_NOON, MER_MN } MERIDIAN; @@ -191,22 +191,58 @@ at_number: time: tUNUMBER tMERIDIAN { - param->yyHour = $1; param->yyMinutes = 0; param->yySeconds = 0; - param->yyMeridian = $2; + if ($2 == MER_NOON || $2 == MER_MN) { + if ($1 == 12) { + switch ($2) { + case MER_NOON: param->yyHour = 12; break; + case MER_MN : param->yyHour = 0; break; + default: /* impossible */; break; + } + param->yyMeridian = MER24; + } else + YYREJECT; + } else { + param->yyHour = $1; + param->yyMeridian = $2; + } } | tUNUMBER ':' tUNUMBER o_merid { - param->yyHour = $1; param->yyMinutes = $3; param->yySeconds = 0; - param->yyMeridian = $4; + if ($4 == MER_NOON || $4 == MER_MN) { + if ($1 == 12 && $3 == 0) { + switch ($4) { + case MER_NOON: param->yyHour = 12; break; + case MER_MN : param->yyHour = 0; break; + default: /* impossible */; break; + } + param->yyMeridian = MER24; + } else + YYREJECT; + } else { + param->yyHour = $1; + param->yyMeridian = $4; + } } | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid { - param->yyHour = $1; param->yyMinutes = $3; param->yySeconds = $5; - param->yyMeridian = $6; + if ($6 == MER_NOON || $6 == MER_MN) { + if ($1 == 12 && $3 == 0 && $5 == 0) { + switch ($6) { + case MER_NOON: param->yyHour = 12; break; + case MER_MN : param->yyHour = 0; break; + default: /* impossible */; break; + } + param->yyMeridian = MER24; + } else + YYREJECT; + } else { + param->yyHour = $1; + param->yyMeridian = $6; + } } | tUNUMBER ':' tUNUMBER ':' tUNUMBER '.' tUNUMBER { param->yyHour = $1; @@ -223,7 +259,16 @@ time: /* Tues midnight --> Weds 00:00, midnight Tues -> Tues 00:00 */ if ($1 == 0 && param->yyHaveDay) param->yyDayNumber++; - } + } + | tUNUMBER tTIME { + if ($1 == 12 && ($2 == 0 || $2 == 12)) { + param->yyHour = $2; + param->yyMinutes = 0; + param->yySeconds = 0; + param->yyMeridian = MER24; + } else + YYREJECT; + } ; time_numericzone: @@ -362,6 +407,7 @@ number: o_merid: /* empty */ { $$ = MER24; } | tMERIDIAN { $$ = $1; } + | tTIME { $$ = $1 == 0 ? MER_MN : MER_NOON; } ; %% |
