diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2020-08-28 16:29:17 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2020-08-28 16:29:17 +0000 |
commit | 4e8411100235854d65f0eb90ad1b09da7610b41e (patch) | |
tree | 57ee6f5ca048ae756e94831f2b08f17045040f2a | |
parent | 63332380ad76c1a3962d9533d28f8ff3c19842b8 (diff) |
Implement mktime() function for compatibility with mawk and gawk.
This is the only missing time function compared to those two
implementations. Doc changes OK jmc@
-rw-r--r-- | usr.bin/awk/awk.1 | 42 | ||||
-rw-r--r-- | usr.bin/awk/awk.h | 3 | ||||
-rw-r--r-- | usr.bin/awk/lex.c | 3 | ||||
-rw-r--r-- | usr.bin/awk/run.c | 24 |
4 files changed, 66 insertions, 6 deletions
diff --git a/usr.bin/awk/awk.1 b/usr.bin/awk/awk.1 index 2fbaf6680f5..a902f2d44b4 100644 --- a/usr.bin/awk/awk.1 +++ b/usr.bin/awk/awk.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: awk.1,v 1.56 2020/07/24 01:57:06 millert Exp $ +.\" $OpenBSD: awk.1,v 1.57 2020/08/28 16:29:16 millert Exp $ .\" .\" Copyright (C) Lucent Technologies 1997 .\" All Rights Reserved @@ -22,7 +22,7 @@ .\" ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF .\" THIS SOFTWARE. .\" -.Dd $Mdocdate: July 24 2020 $ +.Dd $Mdocdate: August 28 2020 $ .Dt AWK 1 .Os .Sh NAME @@ -684,6 +684,41 @@ This version of provides the following functions for obtaining and formatting time stamps. .Bl -tag -width indent +.It Fn mktime datespec +Converts +.Fa datespec +into a timestamp in the same form as a value returned by +.Fn systime . +The +.Fa datespec +is a string composed of six or seven numbers separated by whitespace: +.Bd -literal -offset indent +YYYY MM DD HH MM SS [DST] +.Ed +.Pp +The fields in +.Fa datespec +are as follows: +.Bl -tag -width "YYYY" +.It YYY +Year: a four-digit year, including the century. +.It MM +Month: a number from 1 to 12. +.It DD +Day: a number from 1 to 31. +.It HH +Hour: a number from 0 to 23. +.It MM +Minute: a number from 0 to 59. +.It SS +Second: a number from 0 to 60 (permitting a leap second). +.It DST +Daylight Saving Time: a positive or zero value indicates that +DST is or is not in effect. +If DST is not specified, or is negative, +.Fn mktime +will attempt to determine the correct value. +.El .It Fn strftime "[format [, timestamp]]" Formats .Ar timestamp @@ -696,6 +731,8 @@ manual page, as well as any arbitrary text. The .Ar timestamp must be in the same form as a value returned by +.Fn mktime +and .Fn systime . If .Ar timestamp @@ -935,6 +972,7 @@ as well as the functions .Fn xor , .Fn lshift , .Fn rshift , +.Fn mktime , .Fn strftime and .Fn systime diff --git a/usr.bin/awk/awk.h b/usr.bin/awk/awk.h index 68e03a809c6..25dfec2fcc4 100644 --- a/usr.bin/awk/awk.h +++ b/usr.bin/awk/awk.h @@ -1,4 +1,4 @@ -/* $OpenBSD: awk.h,v 1.26 2020/06/26 15:57:39 millert Exp $ */ +/* $OpenBSD: awk.h,v 1.27 2020/08/28 16:29:16 millert Exp $ */ /**************************************************************** Copyright (C) Lucent Technologies 1997 All Rights Reserved @@ -160,6 +160,7 @@ extern Cell *symtabloc; /* SYMTAB */ #define FRSHIFT 20 #define FSYSTIME 21 #define FSTRFTIME 22 +#define FMKTIME 23 /* Node: parse tree is made of nodes, with Cell's at bottom */ diff --git a/usr.bin/awk/lex.c b/usr.bin/awk/lex.c index 6a835c24cb1..1dc1f991fa0 100644 --- a/usr.bin/awk/lex.c +++ b/usr.bin/awk/lex.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lex.c,v 1.25 2020/07/30 17:45:44 millert Exp $ */ +/* $OpenBSD: lex.c,v 1.26 2020/08/28 16:29:16 millert Exp $ */ /**************************************************************** Copyright (C) Lucent Technologies 1997 All Rights Reserved @@ -75,6 +75,7 @@ const Keyword keywords[] = { /* keep sorted: binary searched */ { "log", FLOG, BLTIN }, { "lshift", FLSHIFT, BLTIN }, { "match", MATCHFCN, MATCHFCN }, + { "mktime", FMKTIME, BLTIN }, { "next", NEXT, NEXT }, { "nextfile", NEXTFILE, NEXTFILE }, { "or", FFOR, BLTIN }, diff --git a/usr.bin/awk/run.c b/usr.bin/awk/run.c index dcd959aa52d..f6b141c695c 100644 --- a/usr.bin/awk/run.c +++ b/usr.bin/awk/run.c @@ -1,4 +1,4 @@ -/* $OpenBSD: run.c,v 1.67 2020/08/11 16:57:05 millert Exp $ */ +/* $OpenBSD: run.c,v 1.68 2020/08/28 16:29:16 millert Exp $ */ /**************************************************************** Copyright (C) Lucent Technologies 1997 All Rights Reserved @@ -1594,7 +1594,7 @@ Cell *bltin(Node **a, int n) /* builtin functions. a[0] is type, a[1] is arg lis FILE *fp; int status = 0; time_t tv; - struct tm *tm; + struct tm *tm, tmbuf; t = ptoi(a[0]); x = execute(a[1]); @@ -1749,6 +1749,26 @@ Cell *bltin(Node **a, int n) /* builtin functions. a[0] is type, a[1] is arg lis else u = fflush(fp); break; + case FMKTIME: + memset(&tmbuf, 0, sizeof(tmbuf)); + tm = &tmbuf; + t = sscanf(getsval(x), "%d %d %d %d %d %d %d", + &tm->tm_year, &tm->tm_mon, &tm->tm_mday, &tm->tm_hour, + &tm->tm_min, &tm->tm_sec, &tm->tm_isdst); + switch (t) { + case 6: + tm->tm_isdst = -1; /* let mktime figure it out */ + /* FALLTHROUGH */ + case 7: + tm->tm_year -= 1900; + tm->tm_mon--; + u = mktime(tm); + break; + default: + u = -1; + break; + } + break; case FSYSTIME: u = time((time_t *) 0); break; |