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:s:r:u[!dr]", TOYFLAG_BIN))
11 
12 config DATE
13   bool "date"
14   default y
15   help
16     usage: date [-u] [-r FILE] [-d DATE] [+DISPLAY_FORMAT] [-s SET_FORMAT] [SET]
17 
18     Set/get the current date/time. With no SET shows the current date.
19 
20     Default SET format is "MMDDhhmm[[CC]YY][.ss]", that's (2 digits each)
21     month, day, hour (0-23), and minute. Optionally century, year, and second.
22 
23     -d	Show DATE instead of current time (convert date format)
24     -r	Use modification time of FILE instead of current date
25     -s	+FORMAT for SET or -d (instead of MMDDhhmm[[CC]YY][.ss])
26     -u	Use UTC instead of current timezone
27 
28     +FORMAT specifies display format string using these escapes:
29 
30     %% literal %             %n newline              %t tab
31     %S seconds (00-60)       %M minute (00-59)       %m month (01-12)
32     %H hour (0-23)           %I hour (01-12)         %p AM/PM
33     %y short year (00-99)    %Y year                 %C century
34     %a short weekday name    %A weekday name         %u day of week (1-7, 1=mon)
35     %b short month name      %B month name           %Z timezone name
36     %j day of year (001-366) %d day of month (01-31) %e day of month ( 1-31)
37 
38     %U Week of year (0-53 start sunday)   %W Week of year (0-53 start monday)
39     %V Week of year (1-53 start monday, week < 4 days not part of this year)
40 
41     %D = "%m/%d/%y"    %r = "%I : %M : %S %p"   %T = "%H:%M:%S"   %h = "%b"
42     %x locale date     %X locale time           %c locale date/time
43 */
44 
45 #define FOR_date
46 #include "toys.h"
47 
GLOBALS(char * file;char * setfmt;char * showdate;)48 GLOBALS(
49   char *file;
50   char *setfmt;
51   char *showdate;
52 )
53 
54 // Handle default posix date format: mmddhhmm[[cc]yy]
55 // returns 0 success, nonzero for error
56 int parse_posixdate(char *str, struct tm *tm)
57 {
58   int len;
59 
60   len = 0;
61   sscanf(str, "%2u%2u%2u%2u%n", &tm->tm_mon, &tm->tm_mday, &tm->tm_hour,
62     &tm->tm_min, &len);
63   if (len != 8) return 1;
64   str += len;
65   tm->tm_mon--;
66 
67   // If year specified, overwrite one we fetched earlier
68   if (*str && *str != '.') {
69     unsigned year, r1 = tm->tm_year % 100, r2 = (tm->tm_year + 50) % 100,
70       century = tm->tm_year - r1;
71 
72     len = 0;
73     sscanf(str, "%u%n", &year, &len);
74     if (len == 4) year -= 1900;
75     else if (len != 2) return 1;
76     str += len;
77 
78     // 2 digit years, next 50 years are "future", last 50 years are "past".
79     // A "future" date in past is a century ahead.
80     // A non-future date in the future is a century behind.
81     if ((r1 < r2) ? (r1 < year && year < r2) : (year < r1 || year > r2)) {
82       if (year < r1) year += 100;
83     } else if (year > r1) year -= 100;
84     tm->tm_year = year + century;
85   }
86   if (*str == '.') {
87     len = 0;
88     sscanf(str, ".%u%n", &tm->tm_sec, &len);
89     str += len;
90   }
91 
92   return *str;
93 }
94 
date_main(void)95 void date_main(void)
96 {
97   char *setdate = *toys.optargs, *format_string = "%a %b %e %H:%M:%S %Z %Y",
98        *tz = 0;
99   struct tm tm;
100 
101   // We can't just pass a timezone to mktime because posix.
102   if (toys.optflags & FLAG_u) {
103     if (CFG_TOYBOX_FREE) tz = getenv("TZ");
104     setenv("TZ", "UTC", 1);
105     tzset();
106   }
107 
108   if (TT.showdate) {
109     setdate = TT.showdate;
110     if (TT.setfmt) {
111       char *s = strptime(TT.showdate, TT.setfmt+(*TT.setfmt=='+'), &tm);
112 
113       if (!s || *s) goto bad_date;
114     } else if (parse_posixdate(TT.showdate, &tm)) goto bad_date;
115   } else {
116     time_t now;
117 
118     if (TT.file) {
119       struct stat st;
120 
121       xstat(TT.file, &st);
122       now = st.st_mtim.tv_sec;
123     } else now = time(0);
124 
125     ((toys.optflags & FLAG_u) ? gmtime_r : localtime_r)(&now, &tm);
126   }
127 
128   setdate = *toys.optargs;
129   // Fall through if no arguments
130   if (!setdate);
131   // Display the date?
132   else if (*setdate == '+') {
133     format_string = toys.optargs[0]+1;
134     setdate = toys.optargs[1];
135 
136   // Set the date
137   } else if (setdate) {
138     struct timeval tv;
139 
140     if (parse_posixdate(setdate, &tm)) goto bad_date;
141 
142     if (toys.optflags & FLAG_u) {
143       char *tz = CFG_TOYBOX_FREE ? getenv("TZ") : 0;
144 
145       // We can't just pass a timezone to mktime because posix.
146       setenv("TZ", "UTC", 1);
147       tzset();
148       tv.tv_sec = mktime(&tm);
149       if (CFG_TOYBOX_FREE) {
150         if (tz) setenv("TZ", tz, 1);
151         else unsetenv("TZ");
152         tzset();
153       }
154     } else tv.tv_sec = mktime(&tm);
155     if (tv.tv_sec == (time_t)-1) goto bad_date;
156 
157     tv.tv_usec = 0;
158     if (settimeofday(&tv, NULL) < 0) perror_msg("cannot set date");
159   }
160 
161   if (toys.optflags & FLAG_u) {
162     if (tz) setenv("TZ", tz, 1);
163     else unsetenv("TZ");
164     tzset();
165   }
166 
167   if (!strftime(toybuf, sizeof(toybuf), format_string, &tm))
168     perror_exit("bad format '%s'", format_string);
169   puts(toybuf);
170 
171   return;
172 
173 bad_date:
174   error_exit("bad date '%s'", setdate);
175 }
176