1 /* date.c - set/get the date
2 *
3 * Copyright 2012 Andre Renaud <andre@bluewatersys.com>
4 *
5 * See http://opengroup.org/onlinepubs/9699919799/utilities/date.html
6 *
7 * Note: setting a 2 year date is 50 years back/forward from today,
8 * not posix's hardwired magic dates.
9
10 USE_DATE(NEWTOY(date, "d:D:r:u[!dr]", TOYFLAG_BIN))
11
12 config DATE
13 bool "date"
14 default y
15 help
16 usage: date [-u]
17
18 Set/get the current date/time. With no SET shows the current date.
19
20 -u Use UTC instead of current timezone
21
22 Supported input formats:
23
24 MMDDhhmm[[CC]YY][.ss] POSIX
25 @UNIXTIME[.FRACTION] seconds since midnight 1970-01-01
26 YYYY-MM-DD [hh:mm[:ss]] ISO 8601
27 hh:mm[:ss] 24-hour time today
28
29 All input formats can be preceded by TZ="id" to set the input time zone
30 separately from the output time zone. Otherwise $TZ sets both.
31
32 +FORMAT specifies display format string using strftime(3) syntax:
33
34 %% literal % %n newline %t tab
35 %S seconds (00-60) %M minute (00-59) %m month (01-12)
36 %H hour (0-23) %I hour (01-12) %p AM/PM
37 %y short year (00-99) %Y year %C century
38 %a short weekday name %A weekday name %u day of week (1-7, 1=mon)
39 %b short month name %B month name %Z timezone name
40 %j day of year (001-366) %d day of month (01-31) %e day of month ( 1-31)
41 %N nanosec (output only)
42
43 %U Week of year (0-53 start sunday) %W Week of year (0-53 start monday)
44 %V Week of year (1-53 start monday, week < 4 days not part of this year)
45
46 %D = "%m/%d/%y" %r = "%I : %M : %S %p" %T = "%H:%M:%S" %h = "%b"
47 %x locale date %X locale time %c locale date/time
48 */
49
50 #define FOR_date
51 #include "toys.h"
52
53 GLOBALS(
54 char *r, *D, *d;
55
56 unsigned nano;
57 )
58
59 // Handles any leading `TZ="blah" ` in the input string.
parse_date(char * str,time_t * t)60 static void parse_date(char *str, time_t *t)
61 {
62 char *new_tz = NULL, *old_tz, *s = str;
63
64 if (!strncmp(str, "TZ=\"", 4)) {
65 // Extract the time zone and skip any whitespace.
66 new_tz = str+4;
67 if (!(str = strchr(new_tz, '"'))) xvali_date(0, s);
68 *str++ = 0;
69 while (isspace(*str)) str++;
70
71 // Switch $TZ.
72 old_tz = getenv("TZ");
73 setenv("TZ", new_tz, 1);
74 tzset();
75 }
76 time(t);
77 xparsedate(str, t, &TT.nano, 1);
78 if (new_tz) {
79 if (old_tz) setenv("TZ", old_tz, 1);
80 else unsetenv("TZ");
81 }
82 }
83
84 // Print strftime plus %N escape(s). note: modifies fmt for %N
puts_time(char * fmt,struct tm * tm)85 static void puts_time(char *fmt, struct tm *tm)
86 {
87 char *s, *snap;
88 long width = width;
89
90 for (s = fmt;;s++) {
91
92 // Find next %N or end
93 if (*(snap = s) == '%') {
94 width = isdigit(*++s) ? *(s++)-'0' : 9;
95 if (*s && *s != 'N') continue;
96 } else if (*s) continue;
97
98 // Don't modify input string if no %N (default format is constant string).
99 if (*s) *snap = 0;
100 if (!strftime(toybuf, sizeof(toybuf)-10, fmt, tm))
101 perror_exit("bad format '%s'", fmt);
102 if (*s) {
103 snap = toybuf+strlen(toybuf);
104 sprintf(snap, "%09u", TT.nano);
105 snap[width] = 0;
106 }
107 fputs(toybuf, stdout);
108 if (!*s || !*(fmt = s+1)) break;
109 }
110 xputc('\n');
111 }
112
date_main(void)113 void date_main(void)
114 {
115 char *setdate = *toys.optargs, *format_string = "%a %b %e %H:%M:%S %Z %Y",
116 *tz = NULL;
117 time_t t;
118
119 if (FLAG(u)) {
120 tz = getenv("TZ");
121 setenv("TZ", "UTC", 1);
122 tzset();
123 }
124
125 if (TT.d) {
126 if (TT.D) {
127 struct tm tm = {};
128 char *s = strptime(TT.d, TT.D+(*TT.D=='+'), &tm);
129
130 t = (s && *s) ? xvali_date(&tm, s) : xvali_date(0, TT.d);
131 } else parse_date(TT.d, &t);
132 } else {
133 struct timespec ts;
134 struct stat st;
135
136 if (TT.r) {
137 xstat(TT.r, &st);
138 ts = st.st_mtim;
139 } else clock_gettime(CLOCK_REALTIME, &ts);
140
141 t = ts.tv_sec;
142 TT.nano = ts.tv_nsec;
143 }
144
145 // Fall through if no arguments
146 if (!setdate);
147 // Display the date?
148 else if (*setdate == '+') {
149 format_string = toys.optargs[0]+1;
150 setdate = toys.optargs[1];
151
152 // Set the date
153 } else if (setdate) {
154 struct timeval tv;
155
156 parse_date(setdate, &t);
157 tv.tv_sec = t;
158 tv.tv_usec = TT.nano/1000;
159 if (settimeofday(&tv, NULL) < 0) perror_msg("cannot set date");
160 }
161
162 puts_time(format_string, localtime(&t));
163
164 if (FLAG(u)) {
165 if (tz) setenv("TZ", tz, 1);
166 else unsetenv("TZ");
167 tzset();
168 }
169
170 return;
171 }
172