diff options
Diffstat (limited to 'bin/date')
| -rw-r--r-- | bin/date/Makefile | 10 | ||||
| -rw-r--r-- | bin/date/Makefile.depend | 15 | ||||
| -rw-r--r-- | bin/date/date.1 | 621 | ||||
| -rw-r--r-- | bin/date/date.c | 466 | ||||
| -rw-r--r-- | bin/date/tests/Makefile | 3 | ||||
| -rw-r--r-- | bin/date/tests/Makefile.depend | 10 | ||||
| -rwxr-xr-x | bin/date/tests/format_string_test.sh | 161 | ||||
| -rw-r--r-- | bin/date/vary.c | 505 | ||||
| -rw-r--r-- | bin/date/vary.h | 34 |
9 files changed, 1825 insertions, 0 deletions
diff --git a/bin/date/Makefile b/bin/date/Makefile new file mode 100644 index 000000000000..6f2e7b7cf900 --- /dev/null +++ b/bin/date/Makefile @@ -0,0 +1,10 @@ +.include <src.opts.mk> + +PACKAGE=runtime +PROG= date +SRCS= date.c vary.c + +HAS_TESTS= +SUBDIR.${MK_TESTS}+= tests + +.include <bsd.prog.mk> diff --git a/bin/date/Makefile.depend b/bin/date/Makefile.depend new file mode 100644 index 000000000000..6ef78fac5cbf --- /dev/null +++ b/bin/date/Makefile.depend @@ -0,0 +1,15 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + include \ + include/xlocale \ + lib/${CSU_DIR} \ + lib/libc \ + lib/libcompiler_rt \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/bin/date/date.1 b/bin/date/date.1 new file mode 100644 index 000000000000..f68892bd408d --- /dev/null +++ b/bin/date/date.1 @@ -0,0 +1,621 @@ +.\"- +.\" Copyright (c) 1980, 1990, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" the Institute of Electrical and Electronics Engineers, Inc. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.Dd November 5, 2025 +.Dt DATE 1 +.Os +.Sh NAME +.Nm date +.Nd display or set date and time +.Sh SYNOPSIS +.\" Display time. +.Nm +.Op Fl nRu +.Op Fl z Ar output_zone +.Op Fl I Ns Op Ar FMT +.Op Fl r Ar filename +.Op Fl r Ar seconds +.Oo +.Sm off +.Fl v +.Op Cm + | - +.Ar val Op Cm y | m | w | d | H | M | S +.Sm on +.Oc +.Op Cm + Ns Ar output_fmt +.\" Set time with the default input format. +.Nm +.Op Fl jnRu +.Op Fl z Ar output_zone +.Op Fl I Ns Op Ar FMT +.Oo +.Sm off +.Fl v +.Op Cm + | - +.Ar val Op Cm y | m | w | d | H | M | S +.Sm on +.Oc +.Sm off +.Oo Oo Oo Oo Oo +.Ar cc Oc +.Ar yy Oc +.Ar mm Oc +.Ar dd Oc +.Ar HH +.Oc Ar MM Op Cm \&. Ar SS +.Sm on +.Op Cm + Ns Ar output_fmt +.\" Set time with the user-provided input format. +.Nm +.Op Fl jnRu +.Op Fl z Ar output_zone +.Op Fl I Ns Op Ar FMT +.Oo +.Sm off +.Fl v +.Op Cm + | - +.Ar val Op Cm y | m | w | d | H | M | S +.Sm on +.Oc +.Fl f Ar input_fmt +.Ar new_date +.Op Cm + Ns Ar output_fmt +.Sh DESCRIPTION +When invoked without arguments, the +.Nm +utility displays the current date and time. +Otherwise, depending on the options specified, +.Nm +will set the date and time or print it in a user-defined way. +.Pp +The +.Nm +utility displays the date and time read from the kernel clock. +When used to set the date and time, +both the kernel clock and the hardware clock are updated. +.Pp +Only the superuser may set the date, +and if the system securelevel (see +.Xr securelevel 7 ) +is greater than 1, +the time may not be changed by more than 1 second. +.Pp +The options are as follows: +.Bl -tag -width Ds +.It Fl f Ar input_fmt +Use +.Ar input_fmt +as the format string to parse the +.Ar new_date +provided rather than using the default +.Sm off +.Oo Oo Oo Oo Oo +.Ar cc Oc +.Ar yy Oc +.Ar mm Oc +.Ar dd Oc +.Ar HH +.Oc Ar MM Op Cm \&. Ar SS +.Sm on +format. +Parsing is done using +.Xr strptime 3 . +.It Fl I Ns Op Ar FMT +Use extended +.St -iso8601 +output format. +.Ar FMT +may be omitted, in which case the default is +.Cm date . +Valid +.Ar FMT +values are +.Cm date , +.Cm hours , +.Cm minutes , +.Cm seconds , +and +.Cm ns +.Pq for nanoseconds . +The date and time is formatted to the specified precision. +When +.Ar FMT +is +.Cm hours +.Po or the more precise +.Cm minutes , +.Cm seconds , +or +.Cm ns Pc , +the extended +.St -iso8601 +format includes the timezone offset. +.It Fl j +Do not try to set the date. +This allows you to use the +.Fl f +flag in addition to the +.Cm + +option to convert one date format to another. +Note that any date or time components unspecified by the +.Fl f +format string take their values from the current time. +.It Fl n +Obsolete flag, accepted and ignored for compatibility. +.It Fl R +Use RFC 2822 date and time output format. +This is equivalent to using +.Ql %a, %d %b %Y \&%T %z +as +.Ar output_fmt +while +.Ev LC_TIME +is set to the +.Dq C +locale . +.It Fl r Ar seconds +Print the date and time represented by +.Ar seconds , +where +.Ar seconds +is the number of seconds since the Epoch +(00:00:00 UTC, January 1, 1970; +see +.Xr time 3 ) , +and can be specified in decimal, octal, or hex. +.It Fl r Ar filename +Print the date and time of the last modification of +.Ar filename . +.It Fl u +Display or set the date in UTC (Coordinated Universal) time. +By default +.Nm +displays the time in the time zone described by +.Pa /etc/localtime +or the +.Ev TZ +environment variable. +.It Fl z Ar output_zone +Just before printing the time, change to the specified timezone; +see the description of +.Ev TZ +below. +This can be used with +.Fl j +to easily convert time specifications from one zone to another. +.It Xo +.Fl v +.Sm off +.Op Cm + | - +.Ar val Op Cm y | m | w | d | H | M | S +.Sm on +.Xc +Adjust (i.e., take the current date and display the result of the +adjustment; not actually set the date) the second, minute, hour, month +day, week day, month or year according to +.Ar val . +If +.Ar val +is preceded by a plus or minus sign, +the date is adjusted forward or backward according to the remaining string, +otherwise the relevant part of the date is set. +The date can be adjusted as many times as required using these flags. +Flags are processed in the order given. +.Pp +When setting values +(rather than adjusting them), +seconds are in the range 0-59, minutes are in the range 0-59, hours are +in the range 0-23, month days are in the range 1-31, week days are in the +range 0-6 (Sun-Sat), +months are in the range 1-12 (Jan-Dec) +and years are in a limited range depending on the platform. +.Pp +On i386, years are in the range 69-38 representing 1969-2038. +On every other platform, years 0-68 are accepted and represent 2000-2068, and +69-99 are accepted and represent 1969-1999. +In both cases, years between 100 and 1900 (both included) are accepted and +interpreted as relative to 1900 of the Gregorian calendar with a limit of 138 on +i386 and a much higher limit on every other platform. +Years starting at 1901 are also accepted, and are interpreted as absolute years. +.Pp +If +.Ar val +is numeric, one of either +.Cm y , +.Cm m , +.Cm w , +.Cm d , +.Cm H , +.Cm M +or +.Cm S +must be used to specify which part of the date is to be adjusted. +.Pp +The week day or month may be specified using a name rather than a +number. +If a name is used with the plus +(or minus) +sign, the date will be put forwards +(or backwards) +to the next +(previous) +date that matches the given week day or month. +This will not adjust the date, +if the given week day or month is the same as the current one. +.Pp +When a date is adjusted to a specific value or in units greater than hours, +daylight savings time considerations are ignored. +Adjustments in units of hours or less honor daylight saving time. +So, assuming the current date is March 26, 0:30 and that the DST adjustment +means that the clock goes forward at 01:00 to 02:00, using +.Fl v No +1H +will adjust the date to March 26, 2:30. +Likewise, if the date is October 29, 0:30 and the DST adjustment means that +the clock goes back at 02:00 to 01:00, using +.Fl v No +3H +will be necessary to reach October 29, 2:30. +.Pp +When the date is adjusted to a specific value that does not actually exist +(for example March 26, 1:30 BST 2000 in the Europe/London timezone), +the date will be silently adjusted forward in units of one hour until it +reaches a valid time. +When the date is adjusted to a specific value that occurs twice +(for example October 29, 1:30 2000), +the resulting timezone will be set so that the date matches the earlier of +the two times. +.Pp +It is not possible to adjust a date to an invalid absolute day, so using +the switches +.Fl v No 31d Fl v No 12m +will simply fail five months of the year. +It is therefore usual to set the month before setting the day; using +.Fl v No 12m Fl v No 31d +always works. +.Pp +Adjusting the date by months is inherently ambiguous because +a month is a unit of variable length depending on the current date. +This kind of date adjustment is applied in the most intuitive way. +First of all, +.Nm +tries to preserve the day of the month. +If it is impossible because the target month is shorter than the present one, +the last day of the target month will be the result. +For example, using +.Fl v No +1m +on May 31 will adjust the date to June 30, while using the same option +on January 30 will result in the date adjusted to the last day of February. +This approach is also believed to make the most sense for shell scripting. +Nevertheless, be aware that going forth and back by the same number of +months may take you to a different date. +.Pp +Refer to the examples below for further details. +.El +.Pp +An operand with a leading plus +.Pq Sq + +sign signals a user-defined format string +which specifies the format in which to display the date and time. +The format string may contain any of the conversion specifications +described in the +.Xr strftime 3 +manual page and +.Ql \&%N +for nanoseconds, as well as any arbitrary text. +A newline +.Pq Ql \en +character is always output after the characters specified by +the format string. +The format string for the default display is +.Dq +%+ . +.Pp +If an operand does not have a leading plus sign, it is interpreted as +a value for setting the system's notion of the current date and time. +The canonical representation for setting the date and time is: +.Pp +.Bl -tag -width Ds -compact -offset indent +.It Ar cc +Century +(either 19 or 20) +prepended to the abbreviated year. +.It Ar yy +Year in abbreviated form +(e.g., 89 for 1989, 06 for 2006). +.It Ar mm +Numeric month, a number from 1 to 12. +.It Ar dd +Day, a number from 1 to 31. +.It Ar HH +Hour, a number from 0 to 23. +.It Ar MM +Minutes, a number from 0 to 59. +.It Ar SS +Seconds, a number from 0 to 60 +(59 plus a potential leap second). +.El +.Pp +Everything but the minutes is optional. +.Pp +.Nm +understands the time zone definitions from the IANA Time Zone Database, +.Sy tzdata , +located in +.Pa /usr/share/zoneinfo . +Time changes for Daylight Saving Time, standard time, leap seconds +and leap years are handled automatically. +.Pp +There are two ways to specify the time zone: +.Pp +If the file or symlink +.Pa /etc/localtime +exists, it is interpreted as a time zone definition file, usually in +the directory hierarchy +.Pa /usr/share/zoneinfo , +which contains the time zone definitions from +.Sy tzdata . +.Pp +If the environment variable +.Ev TZ +is set, its value is interpreted as the name of a time zone definition +file, either an absolute path or a relative path to a time zone +definition in +.Pa /usr/share/zoneinfo . +The +.Ev TZ +variable overrides +.Pa /etc/localtime . +.Pp +If the time zone definition file is invalid, +.Nm +silently reverts to UTC. +.Pp +Previous versions of +.Nm +included the +.Fl d +(set daylight saving time flag) and +.Fl t +(set negative time zone offset) options, but these details are now +handled automatically by +.Sy tzdata . +Modern offsets are positive for time zones ahead of UTC and negative +for time zones behind UTC, but like the obsolete +.Fl t +option, the +.Sy tzdata +files in the subdirectory +.Pa /usr/share/zoneinfo/Etc +still use an older convention where times ahead of UTC are considered +negative. +.Sh ENVIRONMENT +The following environment variable affects the execution of +.Nm : +.Bl -tag -width Ds +.It Ev TZ +The timezone to use when displaying dates. +The normal format is a pathname relative to +.Pa /usr/share/zoneinfo . +For example, the command +.Dq TZ=America/Los_Angeles date +displays the current time in California. +The variable can also specify an absolute path. +See +.Xr environ 7 +for more information. +.El +.Sh FILES +.Bl -tag -width /var/log/messages -compact +.It Pa /etc/localtime +Time zone information file for default system time zone. +May be omitted, in which case the default time zone is UTC. +.It Pa /usr/share/zoneinfo +Directory containing time zone information files. +.It Pa /var/log/messages +Record of the user setting the time. +.It Pa /var/log/utx.log +Record of date resets and time changes. +.El +.Sh EXIT STATUS +The +.Nm +utility exits 0 on success, 1 if unable to set the date, and 2 +if able to set the local date, but unable to set it globally. +.Sh EXAMPLES +The command: +.Pp +.Dl "date ""+DATE: %Y-%m-%d%nTIME: %H:%M:%S""" +.Pp +will display: +.Bd -literal -offset indent +DATE: 1987-11-21 +TIME: 13:36:16 +.Ed +.Pp +In the Europe/London timezone, the command: +.Pp +.Dl "date -v1m -v+1y" +.Pp +will display: +.Pp +.Dl "Sun Jan 4 04:15:24 GMT 1998" +.Pp +where it is currently +.Ql "Mon Aug 4 04:15:24 BST 1997" . +.Pp +The command: +.Pp +.Dl "date -v1d -v3m -v0y -v-1d" +.Pp +will display the last day of February in the year 2000: +.Pp +.Dl "Tue Feb 29 03:18:00 GMT 2000" +.Pp +So will the command: +.Pp +.Dl "date -v3m -v30d -v0y -v-1m" +.Pp +because there is no such date as the 30th of February. +.Pp +The command: +.Pp +.Dl "date -v1d -v+1m -v-1d -v-fri" +.Pp +will display the last Friday of the month: +.Pp +.Dl "Fri Aug 29 04:31:11 BST 1997" +.Pp +where it is currently +.Ql "Mon Aug 4 04:31:11 BST 1997" . +.Pp +The command: +.Pp +.Dl "date 8506131627" +.Pp +sets the date to +.Ql "June 13, 1985, 4:27 PM" . +.Pp +.Dl "date ""+%Y%m%d%H%M.%S""" +.Pp +may be used on one machine to print out the date +suitable for setting on another. +.Po Use +.Ql "+%m%d%H%M%Y.%S" +with GNU date on +Linux . +.Pc +.Pp +The command: +.Pp +.Dl "date 1432" +.Pp +sets the time to +.Ql "2:32 PM" , +without modifying the date. +.Pp +The command +.Pp +.Dl "TZ=America/Los_Angeles date -Iseconds -r 1533415339" +.Pp +will display +.Pp +.Dl "2018-08-04T13:42:19-07:00" +.Pp +The command: +.Pp +.Dl "env LC_ALL=C date -j -f ""%a %b %d %T %Z %Y"" ""`env LC_ALL=C date`"" ""+%s""" +.Pp +can be used to parse the output from +.Nm +and express it in Epoch time. +.Pp +Finally the command +.Pp +.Dl "TZ=America/Los_Angeles date -z Europe/Paris -j 0900" +.Pp +will print the time in the +.Dq Europe/Paris +timezone when it is 9:00 in the +.Dq America/Los_Angeles +timezone. +.Sh DIAGNOSTICS +It is invalid to combine the +.Fl I +flag with either +.Fl R +or an output format +.Dq ( + Ns ... ) +operand. +If this occurs, +.Nm +prints: +.Ql multiple output formats specified +and exits with status 1. +.Sh SEE ALSO +.Xr locale 1 , +.Xr clock_gettime 2 , +.Xr gettimeofday 2 , +.Xr getutxent 3 , +.Xr strftime 3 , +.Xr strptime 3 , +.Xr tzset 3 , +.Xr adjkerntz 8 , +.Xr ntpd 8 , +.Xr tzsetup 8 +.Rs +.%T "TSP: The Time Synchronization Protocol for UNIX 4.3BSD" +.%A R. Gusella +.%A S. Zatti +.Re +.Rs +.%U https://iana.org/time-zones +.%T Time Zone Database +.Re +.Sh STANDARDS +The +.Nm +utility is expected to be compatible with +.St -p1003.2 . +With the exception of the +.Fl u +option, all options are extensions to the standard. +.Pp +The format selected by the +.Fl I +flag is compatible with +.St -iso8601 . +.Pp +The +.Ql \&%N +conversion specification for nanoseconds is a non-standard extension. +It is compatible with GNU date's +.Ql \&%N . +.Sh HISTORY +A +.Nm +command appeared in +.At v1 . +.Pp +A number of options were added and then removed again, including the +.Fl d +(set DST flag) and +.Fl t +(set negative time zone offset). +Time zones are now handled by code bundled with +.Sy tzdata . +.Pp +The +.Fl I +flag was added in +.Fx 12.0 . +.Pp +The +.Ql \&%N +conversion specification was added in +.Fx 14.1 . diff --git a/bin/date/date.c b/bin/date/date.c new file mode 100644 index 000000000000..01797084c0d6 --- /dev/null +++ b/bin/date/date.c @@ -0,0 +1,466 @@ +/*- + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 1985, 1987, 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/param.h> +#include <sys/time.h> +#include <sys/stat.h> + +#include <ctype.h> +#include <err.h> +#include <errno.h> +#include <locale.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <syslog.h> +#include <unistd.h> +#include <utmpx.h> + +#include "vary.h" + +#ifndef TM_YEAR_BASE +#define TM_YEAR_BASE 1900 +#endif + +static void badformat(void); +static void iso8601_usage(const char *) __dead2; +static void multipleformats(void); +static void printdate(const char *); +static void printisodate(struct tm *, long); +static void setthetime(const char *, const char *, int, struct timespec *); +static size_t strftime_ns(char * __restrict, size_t, const char * __restrict, + const struct tm * __restrict, long); +static void usage(void) __dead2; + +static const struct iso8601_fmt { + const char *refname; + const char *format_string; +} iso8601_fmts[] = { + { "date", "%Y-%m-%d" }, + { "hours", "T%H" }, + { "minutes", ":%M" }, + { "seconds", ":%S" }, + { "ns", ",%N" }, +}; +static const struct iso8601_fmt *iso8601_selected; + +static const char *rfc2822_format = "%a, %d %b %Y %T %z"; + +int +main(int argc, char *argv[]) +{ + struct timespec ts; + int ch, rflag; + bool Iflag, jflag, Rflag; + const char *format; + char buf[1024]; + char *fmt, *outzone = NULL; + char *tmp; + struct vary *v; + const struct vary *badv; + struct tm *lt; + struct stat sb; + size_t i; + + v = NULL; + fmt = NULL; + (void) setlocale(LC_TIME, ""); + rflag = 0; + Iflag = jflag = Rflag = 0; + ts.tv_sec = 0; + ts.tv_nsec = 0; + while ((ch = getopt(argc, argv, "f:I::jnRr:uv:z:")) != -1) + switch((char)ch) { + case 'f': + fmt = optarg; + break; + case 'I': + if (Rflag) + multipleformats(); + Iflag = 1; + if (optarg == NULL) { + iso8601_selected = iso8601_fmts; + break; + } + for (i = 0; i < nitems(iso8601_fmts); i++) + if (strcmp(optarg, iso8601_fmts[i].refname) == 0) + break; + if (i == nitems(iso8601_fmts)) + iso8601_usage(optarg); + + iso8601_selected = &iso8601_fmts[i]; + break; + case 'j': + jflag = 1; /* don't set time */ + break; + case 'n': + break; + case 'R': /* RFC 2822 datetime format */ + if (Iflag) + multipleformats(); + Rflag = 1; + break; + case 'r': /* user specified seconds */ + rflag = 1; + ts.tv_sec = strtoq(optarg, &tmp, 0); + if (*tmp != 0) { + if (stat(optarg, &sb) == 0) { + ts.tv_sec = sb.st_mtim.tv_sec; + ts.tv_nsec = sb.st_mtim.tv_nsec; + } else + usage(); + } + break; + case 'u': /* do everything in UTC */ + (void)setenv("TZ", "UTC0", 1); + break; + case 'z': + outzone = optarg; + break; + case 'v': + v = vary_append(v, optarg); + break; + default: + usage(); + } + argc -= optind; + argv += optind; + + if (!rflag && clock_gettime(CLOCK_REALTIME, &ts) == -1) + err(1, "clock_gettime"); + + format = "%+"; + + if (Rflag) + format = rfc2822_format; + + /* allow the operands in any order */ + if (*argv && **argv == '+') { + if (Iflag) + multipleformats(); + format = *argv + 1; + ++argv; + } + + if (*argv) { + setthetime(fmt, *argv, jflag, &ts); + ++argv; + } else if (fmt != NULL) + usage(); + + if (*argv && **argv == '+') { + if (Iflag) + multipleformats(); + format = *argv + 1; + } + + if (outzone != NULL && setenv("TZ", outzone, 1) != 0) + err(1, "setenv(TZ)"); + lt = localtime(&ts.tv_sec); + if (lt == NULL) + errx(1, "invalid time"); + badv = vary_apply(v, lt); + if (badv) { + fprintf(stderr, "%s: Cannot apply date adjustment\n", + badv->arg); + vary_destroy(v); + usage(); + } + vary_destroy(v); + + if (Iflag) + printisodate(lt, ts.tv_nsec); + + if (format == rfc2822_format) + /* + * When using RFC 2822 datetime format, don't honor the + * locale. + */ + setlocale(LC_TIME, "C"); + + + (void)strftime_ns(buf, sizeof(buf), format, lt, ts.tv_nsec); + printdate(buf); +} + +static void +printdate(const char *buf) +{ + (void)printf("%s\n", buf); + if (fflush(stdout)) + err(1, "stdout"); + exit(EXIT_SUCCESS); +} + +static void +printisodate(struct tm *lt, long nsec) +{ + const struct iso8601_fmt *it; + char fmtbuf[64], buf[64], tzbuf[8]; + + fmtbuf[0] = 0; + for (it = iso8601_fmts; it <= iso8601_selected; it++) + strlcat(fmtbuf, it->format_string, sizeof(fmtbuf)); + + (void)strftime_ns(buf, sizeof(buf), fmtbuf, lt, nsec); + + if (iso8601_selected > iso8601_fmts) { + (void)strftime_ns(tzbuf, sizeof(tzbuf), "%z", lt, nsec); + memmove(&tzbuf[4], &tzbuf[3], 3); + tzbuf[3] = ':'; + strlcat(buf, tzbuf, sizeof(buf)); + } + + printdate(buf); +} + +#define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0')) + +static void +setthetime(const char *fmt, const char *p, int jflag, struct timespec *ts) +{ + struct utmpx utx; + struct tm *lt; + const char *dot, *t; + int century; + + lt = localtime(&ts->tv_sec); + if (lt == NULL) + errx(1, "invalid time"); + lt->tm_isdst = -1; /* divine correct DST */ + + if (fmt != NULL) { + t = strptime(p, fmt, lt); + if (t == NULL) { + fprintf(stderr, "Failed conversion of ``%s''" + " using format ``%s''\n", p, fmt); + badformat(); + } else if (*t != '\0') + fprintf(stderr, "Warning: Ignoring %ld extraneous" + " characters in date string (%s)\n", + (long) strlen(t), t); + } else { + for (t = p, dot = NULL; *t; ++t) { + if (isdigit(*t)) + continue; + if (*t == '.' && dot == NULL) { + dot = t; + continue; + } + badformat(); + } + + if (dot != NULL) { /* .ss */ + dot++; /* *dot++ = '\0'; */ + if (strlen(dot) != 2) + badformat(); + lt->tm_sec = ATOI2(dot); + if (lt->tm_sec > 61) + badformat(); + } else + lt->tm_sec = 0; + + century = 0; + /* if p has a ".ss" field then let's pretend it's not there */ + switch (strlen(p) - ((dot != NULL) ? 3 : 0)) { + case 12: /* cc */ + lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE; + century = 1; + /* FALLTHROUGH */ + case 10: /* yy */ + if (century) + lt->tm_year += ATOI2(p); + else { + lt->tm_year = ATOI2(p); + if (lt->tm_year < 69) /* hack for 2000 ;-} */ + lt->tm_year += 2000 - TM_YEAR_BASE; + else + lt->tm_year += 1900 - TM_YEAR_BASE; + } + /* FALLTHROUGH */ + case 8: /* mm */ + lt->tm_mon = ATOI2(p); + if (lt->tm_mon > 12) + badformat(); + --lt->tm_mon; /* time struct is 0 - 11 */ + /* FALLTHROUGH */ + case 6: /* dd */ + lt->tm_mday = ATOI2(p); + if (lt->tm_mday > 31) + badformat(); + /* FALLTHROUGH */ + case 4: /* HH */ + lt->tm_hour = ATOI2(p); + if (lt->tm_hour > 23) + badformat(); + /* FALLTHROUGH */ + case 2: /* MM */ + lt->tm_min = ATOI2(p); + if (lt->tm_min > 59) + badformat(); + break; + default: + badformat(); + } + } + + /* convert broken-down time to GMT clock time */ + lt->tm_yday = -1; + ts->tv_sec = mktime(lt); + if (lt->tm_yday == -1) + errx(1, "nonexistent time"); + ts->tv_nsec = 0; + + if (!jflag) { + utx.ut_type = OLD_TIME; + memset(utx.ut_id, 0, sizeof(utx.ut_id)); + (void)gettimeofday(&utx.ut_tv, NULL); + pututxline(&utx); + if (clock_settime(CLOCK_REALTIME, ts) != 0) + err(1, "clock_settime"); + utx.ut_type = NEW_TIME; + (void)gettimeofday(&utx.ut_tv, NULL); + pututxline(&utx); + + if ((p = getlogin()) == NULL) + p = "???"; + syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p); + } +} + +/* + * The strftime_ns function is a wrapper around strftime(3), which adds support + * for features absent from strftime(3). Currently, the only extra feature is + * support for %N, the nanosecond conversion specification. + * + * The functions scans the format string for the non-standard conversion + * specifications and replaces them with the date and time values before + * passing the format string to strftime(3). The handling of the non-standard + * conversion specifications happens before the call to strftime(3) to handle + * cases like "%%N" correctly ("%%N" should yield "%N" instead of nanoseconds). + */ +static size_t +strftime_ns(char * __restrict s, size_t maxsize, const char * __restrict format, + const struct tm * __restrict t, long nsec) +{ + size_t prefixlen; + size_t ret; + char *newformat; + char *oldformat; + const char *prefix; + const char *suffix; + const char *tok; + bool seen_percent; + + seen_percent = false; + if ((newformat = strdup(format)) == NULL) + err(1, "strdup"); + tok = newformat; + for (tok = newformat; *tok != '\0'; tok++) { + switch (*tok) { + case '%': + /* + * If the previous token was a percent sign, + * then there are two percent tokens in a row. + */ + if (seen_percent) + seen_percent = false; + else + seen_percent = true; + break; + case 'N': + if (seen_percent) { + oldformat = newformat; + prefix = oldformat; + prefixlen = tok - oldformat - 1; + suffix = tok + 1; + /* + * Construct a new format string from the + * prefix (i.e., the part of the old format + * from its beginning to the currently handled + * "%N" conversion specification), the + * nanoseconds, and the suffix (i.e., the part + * of the old format from the next token to the + * end). + */ + if (asprintf(&newformat, "%.*s%.9ld%s", + (int)prefixlen, prefix, nsec, + suffix) < 0) { + err(1, "asprintf"); + } + free(oldformat); + tok = newformat + prefixlen + 9; + } + seen_percent = false; + break; + default: + seen_percent = false; + break; + } + } + + ret = strftime(s, maxsize, newformat, t); + free(newformat); + return (ret); +} + +static void +badformat(void) +{ + warnx("illegal time format"); + usage(); +} + +static void +iso8601_usage(const char *badarg) +{ + errx(1, "invalid argument '%s' for -I", badarg); +} + +static void +multipleformats(void) +{ + errx(1, "multiple output formats specified"); +} + +static void +usage(void) +{ + (void)fprintf(stderr, "%s\n%s\n%s\n", + "usage: date [-jnRu] [-I[date|hours|minutes|seconds|ns]] [-f input_fmt]", + " " + "[ -z output_zone ] [-r filename|seconds] [-v[+|-]val[y|m|w|d|H|M|S]]", + " " + "[[[[[[cc]yy]mm]dd]HH]MM[.SS] | new_date] [+output_fmt]" + ); + exit(1); +} diff --git a/bin/date/tests/Makefile b/bin/date/tests/Makefile new file mode 100644 index 000000000000..98625c4b1b34 --- /dev/null +++ b/bin/date/tests/Makefile @@ -0,0 +1,3 @@ +ATF_TESTS_SH= format_string_test + +.include <bsd.test.mk> diff --git a/bin/date/tests/Makefile.depend b/bin/date/tests/Makefile.depend new file mode 100644 index 000000000000..11aba52f82cf --- /dev/null +++ b/bin/date/tests/Makefile.depend @@ -0,0 +1,10 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/bin/date/tests/format_string_test.sh b/bin/date/tests/format_string_test.sh new file mode 100755 index 000000000000..c2fe2111373f --- /dev/null +++ b/bin/date/tests/format_string_test.sh @@ -0,0 +1,161 @@ +# +# Regression tests for date(1) +# +# Submitted by Edwin Groothuis <edwin@FreeBSD.org> +# +# + +# +# These two date/times have been chosen carefully -- they +# create both the single digit and double/multidigit version of +# the values. +# +# To create a new one, make sure you are using the UTC timezone! +# + +TEST1=3222243 # 1970-02-07 07:04:03 +TEST2=1005600000 # 2001-11-12 21:11:12 + +check() +{ + local format_string exp_output_1 exp_output_2 + + format_string=${1} + exp_output_1=${2} + exp_output_2=${3} + + atf_check -o "inline:${exp_output_1}\n" \ + date -r ${TEST1} +%${format_string} + atf_check -o "inline:${exp_output_2}\n" \ + date -r ${TEST2} +%${format_string} +} + +atf_test_case flag_r_file_test +flag_r_file_test_body() +{ + local file + + file="./testfile" + touch "$file" + atf_check -o "inline:$(stat -f '%9Fm' "$file")\n" \ + date -r "$file" +%s.%N +} + +format_string_test() +{ + local desc exp_output_1 exp_output_2 flag + + desc=${1} + flag=${2} + exp_output_1=${3} + exp_output_2=${4} + + atf_test_case ${desc}_test + eval " +${desc}_test_body() { + check ${flag} '${exp_output_1}' '${exp_output_2}'; +}" + atf_add_test_case ${desc}_test +} + +iso8601_check() +{ + local arg flags exp_output_1 exp_output_2 + + arg="${1}" + flags="${2}" + exp_output_1="${3}" + exp_output_2="${4}" + + atf_check -o "inline:${exp_output_1}\n" \ + date $flags -r ${TEST1} "-I${arg}" + atf_check -o "inline:${exp_output_2}\n" \ + date $flags -r ${TEST2} "-I${arg}" +} + +iso8601_string_test() +{ + local desc arg exp_output_1 exp_output_2 flags + + desc="${1}" + arg="${2}" + flags="${3}" + exp_output_1="${4}" + exp_output_2="${5}" + + atf_test_case iso8601_${desc}_test + eval " +iso8601_${desc}_test_body() { + iso8601_check '${arg}' '${flags}' '${exp_output_1}' '${exp_output_2}' +}" + atf_add_test_case iso8601_${desc}_test + + if [ -z "$flags" ]; then + atf_test_case iso8601_${desc}_parity + eval " +iso8601_${desc}_parity_body() { + local exp1 exp2 + + atf_require_prog gdate + + exp1=\"\$(gdate --date '@${TEST1}' '-I${arg}')\" + exp2=\"\$(gdate --date '@${TEST2}' '-I${arg}')\" + + iso8601_check '${arg}' '' \"\${exp1}\" \"\${exp2}\" +}" + atf_add_test_case iso8601_${desc}_parity + fi +} + +atf_init_test_cases() +{ + atf_add_test_case flag_r_file_test + + format_string_test A A Saturday Monday + format_string_test a a Sat Mon + format_string_test B B February November + format_string_test b b Feb Nov + format_string_test C C 19 20 + format_string_test c c "Sat Feb 7 07:04:03 1970" "Mon Nov 12 21:20:00 2001" + format_string_test D D 02/07/70 11/12/01 + format_string_test d d 07 12 + format_string_test e e " 7" 12 + format_string_test F F "1970-02-07" "2001-11-12" + format_string_test G G 1970 2001 + format_string_test g g 70 01 + format_string_test H H 07 21 + format_string_test h h Feb Nov + format_string_test I I 07 09 + format_string_test j j 038 316 + format_string_test k k " 7" 21 + format_string_test l l " 7" " 9" + format_string_test M M 04 20 + format_string_test m m 02 11 + format_string_test N N 000000000 000000000 + format_string_test p p AM PM + format_string_test R R 07:04 21:20 + format_string_test r r "07:04:03 AM" "09:20:00 PM" + format_string_test S S 03 00 + format_string_test s s ${TEST1} ${TEST2} + format_string_test U U 05 45 + format_string_test u u 6 1 + format_string_test V V 06 46 + format_string_test v v " 7-Feb-1970" "12-Nov-2001" + format_string_test W W 05 46 + format_string_test w w 6 1 + format_string_test X X "07:04:03" "21:20:00" + format_string_test x x "02/07/70" "11/12/01" + format_string_test Y Y 1970 2001 + format_string_test y y 70 01 + format_string_test Z Z UTC UTC + format_string_test z z +0000 +0000 + format_string_test percent % % % + format_string_test plus + "Sat Feb 7 07:04:03 UTC 1970" "Mon Nov 12 21:20:00 UTC 2001" + + iso8601_string_test default "" "" "1970-02-07" "2001-11-12" + iso8601_string_test date date "" "1970-02-07" "2001-11-12" + iso8601_string_test hours hours "" "1970-02-07T07+00:00" "2001-11-12T21+00:00" + iso8601_string_test minutes minutes "" "1970-02-07T07:04+00:00" "2001-11-12T21:20+00:00" + iso8601_string_test seconds seconds "" "1970-02-07T07:04:03+00:00" "2001-11-12T21:20:00+00:00" + iso8601_string_test ns ns "" "1970-02-07T07:04:03,000000000+00:00" "2001-11-12T21:20:00,000000000+00:00" +} diff --git a/bin/date/vary.c b/bin/date/vary.c new file mode 100644 index 000000000000..ec556139c201 --- /dev/null +++ b/bin/date/vary.c @@ -0,0 +1,505 @@ +/*- + * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +#include <err.h> +#include <time.h> +#include <stdint.h> +#include <string.h> +#include <stdlib.h> +#include "vary.h" + +struct trans { + int64_t val; + const char *str; +}; + +static struct trans trans_mon[] = { + { 1, "january" }, { 2, "february" }, { 3, "march" }, { 4, "april" }, + { 5, "may"}, { 6, "june" }, { 7, "july" }, { 8, "august" }, + { 9, "september" }, { 10, "october" }, { 11, "november" }, { 12, "december" }, + { -1, NULL } +}; + +static struct trans trans_wday[] = { + { 0, "sunday" }, { 1, "monday" }, { 2, "tuesday" }, { 3, "wednesday" }, + { 4, "thursday" }, { 5, "friday" }, { 6, "saturday" }, + { -1, NULL } +}; + +static char digits[] = "0123456789"; +static int adjhour(struct tm *, char, int64_t, int); + +static int +domktime(struct tm *t, char type) +{ + time_t ret; + + while ((ret = mktime(t)) == -1 && t->tm_year > 68 && t->tm_year < 138) + /* While mktime() fails, adjust by an hour */ + adjhour(t, type == '-' ? type : '+', 1, 0); + + return ret; +} + +static int +trans(const struct trans t[], const char *arg) +{ + int f; + + for (f = 0; t[f].val != -1; f++) + if (!strncasecmp(t[f].str, arg, 3) || + !strncasecmp(t[f].str, arg, strlen(t[f].str))) + return t[f].val; + + return -1; +} + +struct vary * +vary_append(struct vary *v, char *arg) +{ + struct vary *result, **nextp; + + if (v) { + result = v; + while (v->next) + v = v->next; + nextp = &v->next; + } else + nextp = &result; + + if ((*nextp = (struct vary *)malloc(sizeof(struct vary))) == NULL) + err(1, "malloc"); + (*nextp)->arg = arg; + (*nextp)->next = NULL; + return result; +} + +static int mdays[12] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + +static int +daysinmonth(const struct tm *t) +{ + int year; + + year = t->tm_year + 1900; + + if (t->tm_mon == 1) + if (!(year % 400)) + return 29; + else if (!(year % 100)) + return 28; + else if (!(year % 4)) + return 29; + else + return 28; + else if (t->tm_mon >= 0 && t->tm_mon < 12) + return mdays[t->tm_mon]; + + return 0; +} + + +static int +adjyear(struct tm *t, char type, int64_t val, int mk) +{ + switch (type) { + case '+': + t->tm_year += val; + break; + case '-': + t->tm_year -= val; + break; + default: + t->tm_year = val; + if (t->tm_year < 69) + t->tm_year += 100; /* as per date.c */ + else if (t->tm_year > 1900) + t->tm_year -= 1900; /* struct tm holds years since 1900 */ + break; + } + return !mk || domktime(t, type) != -1; +} + +static int +adjmon(struct tm *t, char type, int64_t val, int istext, int mk) +{ + int lmdays; + + if (val < 0) + return 0; + + switch (type) { + case '+': + if (istext) { + if (val <= t->tm_mon) + val += 11 - t->tm_mon; /* early next year */ + else + val -= t->tm_mon + 1; /* later this year */ + } + if (val) { + if (!adjyear(t, '+', (t->tm_mon + val) / 12, 0)) + return 0; + val %= 12; + t->tm_mon += val; + if (t->tm_mon > 11) + t->tm_mon -= 12; + } + break; + + case '-': + if (istext) { + if (val-1 > t->tm_mon) + val = 13 - val + t->tm_mon; /* later last year */ + else + val = t->tm_mon - val + 1; /* early this year */ + } + if (val) { + if (!adjyear(t, '-', val / 12, 0)) + return 0; + val %= 12; + if (val > t->tm_mon) { + if (!adjyear(t, '-', 1, 0)) + return 0; + val -= 12; + } + t->tm_mon -= val; + } + break; + + default: + if (val > 12 || val < 1) + return 0; + t->tm_mon = --val; + } + + /* e.g., -v-1m on March, 31 is the last day of February in common sense */ + lmdays = daysinmonth(t); + if (t->tm_mday > lmdays) + t->tm_mday = lmdays; + + return !mk || domktime(t, type) != -1; +} + +static int +adjday(struct tm *t, char type, int64_t val, int mk) +{ + int lmdays; + + switch (type) { + case '+': + while (val) { + lmdays = daysinmonth(t); + if (val > lmdays - t->tm_mday) { + val -= lmdays - t->tm_mday + 1; + t->tm_mday = 1; + if (!adjmon(t, '+', 1, 0, 0)) + return 0; + } else { + t->tm_mday += val; + val = 0; + } + } + break; + case '-': + while (val) + if (val >= t->tm_mday) { + val -= t->tm_mday; + t->tm_mday = 1; + if (!adjmon(t, '-', 1, 0, 0)) + return 0; + t->tm_mday = daysinmonth(t); + } else { + t->tm_mday -= val; + val = 0; + } + break; + default: + if (val > 0 && val <= daysinmonth(t)) + t->tm_mday = val; + else + return 0; + break; + } + + return !mk || domktime(t, type) != -1; +} + +static int +adjwday(struct tm *t, char type, int64_t val, int istext, int mk) +{ + if (val < 0) + return 0; + + switch (type) { + case '+': + if (istext) + if (val < t->tm_wday) + val = 7 - t->tm_wday + val; /* early next week */ + else + val -= t->tm_wday; /* later this week */ + else + val *= 7; /* "-v+5w" == "5 weeks in the future" */ + return !val || adjday(t, '+', val, mk); + case '-': + if (istext) { + if (val > t->tm_wday) + val = 7 - val + t->tm_wday; /* later last week */ + else + val = t->tm_wday - val; /* early this week */ + } else + val *= 7; /* "-v-5w" == "5 weeks ago" */ + return !val || adjday(t, '-', val, mk); + default: + if (val < t->tm_wday) + return adjday(t, '-', t->tm_wday - val, mk); + else if (val > 6) + return 0; + else if (val > t->tm_wday) + return adjday(t, '+', val - t->tm_wday, mk); + } + return 1; +} + +static int +adjhour(struct tm *t, char type, int64_t val, int mk) +{ + if (val < 0) + return 0; + + switch (type) { + case '+': + if (val) { + int days; + + days = (t->tm_hour + val) / 24; + val %= 24; + t->tm_hour += val; + t->tm_hour %= 24; + if (!adjday(t, '+', days, 0)) + return 0; + } + break; + + case '-': + if (val) { + int days; + + days = val / 24; + val %= 24; + if (val > t->tm_hour) { + days++; + val -= 24; + } + t->tm_hour -= val; + if (!adjday(t, '-', days, 0)) + return 0; + } + break; + + default: + if (val > 23) + return 0; + t->tm_hour = val; + } + + return !mk || domktime(t, type) != -1; +} + +static int +adjmin(struct tm *t, char type, int64_t val, int mk) +{ + if (val < 0) + return 0; + + switch (type) { + case '+': + if (val) { + if (!adjhour(t, '+', (t->tm_min + val) / 60, 0)) + return 0; + val %= 60; + t->tm_min += val; + if (t->tm_min > 59) + t->tm_min -= 60; + } + break; + + case '-': + if (val) { + if (!adjhour(t, '-', val / 60, 0)) + return 0; + val %= 60; + if (val > t->tm_min) { + if (!adjhour(t, '-', 1, 0)) + return 0; + val -= 60; + } + t->tm_min -= val; + } + break; + + default: + if (val > 59) + return 0; + t->tm_min = val; + } + + return !mk || domktime(t, type) != -1; +} + +static int +adjsec(struct tm *t, char type, int64_t val, int mk) +{ + if (val < 0) + return 0; + + switch (type) { + case '+': + if (val) { + if (!adjmin(t, '+', (t->tm_sec + val) / 60, 0)) + return 0; + val %= 60; + t->tm_sec += val; + if (t->tm_sec > 59) + t->tm_sec -= 60; + } + break; + + case '-': + if (val) { + if (!adjmin(t, '-', val / 60, 0)) + return 0; + val %= 60; + if (val > t->tm_sec) { + if (!adjmin(t, '-', 1, 0)) + return 0; + val -= 60; + } + t->tm_sec -= val; + } + break; + + default: + if (val > 59) + return 0; + t->tm_sec = val; + } + + return !mk || domktime(t, type) != -1; +} + +const struct vary * +vary_apply(const struct vary *v, struct tm *t) +{ + char type; + char which; + char *arg; + size_t len; + int64_t val; + + for (; v; v = v->next) { + type = *v->arg; + arg = v->arg; + if (type == '+' || type == '-') + arg++; + else + type = '\0'; + len = strlen(arg); + if (len < 2) + return v; + + if (type == '\0') + t->tm_isdst = -1; + + if (strspn(arg, digits) != len-1) { + val = trans(trans_wday, arg); + if (val != -1) { + if (!adjwday(t, type, val, 1, 1)) + return v; + } else { + val = trans(trans_mon, arg); + if (val != -1) { + if (!adjmon(t, type, val, 1, 1)) + return v; + } else + return v; + } + } else { + val = atoi(arg); + which = arg[len-1]; + + switch (which) { + case 'S': + if (!adjsec(t, type, val, 1)) + return v; + break; + case 'M': + if (!adjmin(t, type, val, 1)) + return v; + break; + case 'H': + if (!adjhour(t, type, val, 1)) + return v; + break; + case 'd': + t->tm_isdst = -1; + if (!adjday(t, type, val, 1)) + return v; + break; + case 'w': + t->tm_isdst = -1; + if (!adjwday(t, type, val, 0, 1)) + return v; + break; + case 'm': + t->tm_isdst = -1; + if (!adjmon(t, type, val, 0, 1)) + return v; + break; + case 'y': + t->tm_isdst = -1; + if (!adjyear(t, type, val, 1)) + return v; + break; + default: + return v; + } + } + } + return 0; +} + +void +vary_destroy(struct vary *v) +{ + struct vary *n; + + while (v) { + n = v->next; + free(v); + v = n; + } +} diff --git a/bin/date/vary.h b/bin/date/vary.h new file mode 100644 index 000000000000..a63ee64db055 --- /dev/null +++ b/bin/date/vary.h @@ -0,0 +1,34 @@ +/*- + * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +struct vary { + char *arg; + struct vary *next; +}; + +extern struct vary *vary_append(struct vary *v, char *arg); +extern const struct vary *vary_apply(const struct vary *v, struct tm *t); +extern void vary_destroy(struct vary *v); |
