Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libutil Make parsedate handle "12 noon" and "12 midnight...



details:   https://anonhg.NetBSD.org/src/rev/2112d45f4ef4
branches:  trunk
changeset: 822479:2112d45f4ef4
user:      kre <kre%NetBSD.org@localhost>
date:      Wed Mar 22 01:49:36 2017 +0000

description:
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:

 lib/libutil/parsedate.3 |  18 +++++++++++--
 lib/libutil/parsedate.y |  66 +++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 71 insertions(+), 13 deletions(-)

diffs (157 lines):

diff -r 76236917f877 -r 2112d45f4ef4 lib/libutil/parsedate.3
--- a/lib/libutil/parsedate.3   Wed Mar 22 01:00:19 2017 +0000
+++ b/lib/libutil/parsedate.3   Wed Mar 22 01:49:36 2017 +0000
@@ -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 @@
 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 -r 76236917f877 -r 2112d45f4ef4 lib/libutil/parsedate.y
--- a/lib/libutil/parsedate.y   Wed Mar 22 01:00:19 2017 +0000
+++ b/lib/libutil/parsedate.y   Wed Mar 22 01:49:36 2017 +0000
@@ -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 @@
 } 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 @@
 
 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 @@
                /* 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 @@
 o_merid:
          /* empty */           { $$ = MER24; }
        | tMERIDIAN             { $$ = $1; }
+       | tTIME                 { $$ = $1 == 0 ? MER_MN : MER_NOON; }
 ;
 
 %%



Home | Main Index | Thread Index | Old Index