blob: 15aafb0bc2c9bc9bc229495d319b32f4a4d9bb8f [file] [log] [blame]
Rob Landley7aa651a2012-11-13 17:14:08 -06001/* date.c - set/get the date
Rob Landley26d35be2012-06-18 23:22:08 -05002 *
3 * Copyright 2012 Andre Renaud <andre@bluewatersys.com>
4 *
Rob Landleyf91b7c82012-08-25 18:08:51 -05005 * See http://opengroup.org/onlinepubs/9699919799/utilities/date.html
Rob Landley669f3322014-04-10 19:40:14 -05006 *
7 * Note: setting a 2 year date is 50 years back/forward from today,
8 * not posix's hardwired magic dates.
Rob Landley26d35be2012-06-18 23:22:08 -05009
Rob Landley0369ba52014-05-22 21:33:10 -050010USE_DATE(NEWTOY(date, "d:s:r:u", TOYFLAG_BIN))
Rob Landley26d35be2012-06-18 23:22:08 -050011
12config DATE
Rob Landley7aa651a2012-11-13 17:14:08 -060013 bool "date"
14 default y
15 help
Rob Landley0369ba52014-05-22 21:33:10 -050016 usage: date [-u] [-r FILE] [-d DATE] [+DISPLAY_FORMAT] [-s SET_FORMAT] [SET]
Rob Landley26d35be2012-06-18 23:22:08 -050017
Rob Landley0369ba52014-05-22 21:33:10 -050018 Set/get the current date/time. With no SET shows the current date.
Rob Landley669f3322014-04-10 19:40:14 -050019
Rob Landley0369ba52014-05-22 21:33:10 -050020 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.
Rob Landley669f3322014-04-10 19:40:14 -050022
Rob Landley0369ba52014-05-22 21:33:10 -050023 -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
Rob Landley26d35be2012-06-18 23:22:08 -050043*/
44
Rob Landleyc0e56ed2012-10-08 00:02:30 -050045#define FOR_date
Rob Landley26d35be2012-06-18 23:22:08 -050046#include "toys.h"
47
Rob Landleyc0e56ed2012-10-08 00:02:30 -050048GLOBALS(
Rob Landley7aa651a2012-11-13 17:14:08 -060049 char *file;
Rob Landley0369ba52014-05-22 21:33:10 -050050 char *setfmt;
51 char *showdate;
Rob Landley6d878192012-07-21 18:37:26 -050052)
53
Rob Landley0369ba52014-05-22 21:33:10 -050054// Handle default posix date format: mmddhhmm[[cc]yy]
55// returns 0 success, nonzero for error
56int 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
Rob Landley26d35be2012-06-18 23:22:08 -050095void date_main(void)
96{
Rob Landley0369ba52014-05-22 21:33:10 -050097 char *setdate = *toys.optargs, *format_string = "%a %b %e %H:%M:%S %Z %Y",
Rob Landley7dfee8e2014-05-24 12:51:53 -050098 *tz = 0;
Rob Landley7aa651a2012-11-13 17:14:08 -060099 time_t now = time(NULL);
100 struct tm tm;
Rob Landley26d35be2012-06-18 23:22:08 -0500101
Rob Landley0369ba52014-05-22 21:33:10 -0500102 // We can't just pass a timezone to mktime because posix.
103 if (toys.optflags & FLAG_u) {
Rob Landley7dfee8e2014-05-24 12:51:53 -0500104 if (CFG_TOYBOX_FREE) tz = getenv("TZ");
Rob Landley0369ba52014-05-22 21:33:10 -0500105 setenv("TZ", "UTC", 1);
106 tzset();
107 }
108
Rob Landley7aa651a2012-11-13 17:14:08 -0600109 if (TT.file) {
110 struct stat st;
Rob Landley6d878192012-07-21 18:37:26 -0500111
Rob Landley7aa651a2012-11-13 17:14:08 -0600112 xstat(TT.file, &st);
113 now = st.st_mtim.tv_sec;
Rob Landley0369ba52014-05-22 21:33:10 -0500114 } else if (TT.showdate) {
115 setdate = TT.showdate;
116 if (TT.setfmt) {
Rob Landley0369ba52014-05-22 21:33:10 -0500117 char *s = strptime(TT.showdate, TT.setfmt+(*TT.setfmt=='+'), &tm);
Rob Landley7aa651a2012-11-13 17:14:08 -0600118
Rob Landley0369ba52014-05-22 21:33:10 -0500119 if (!s || *s) goto bad_date;
120 } else if (parse_posixdate(TT.showdate, &tm)) goto bad_date;
121 } else localtime_r(&now, &tm);
122
123 setdate = *toys.optargs;
124 // Fall through if no arguments
125 if (!setdate);
Rob Landley7aa651a2012-11-13 17:14:08 -0600126 // Display the date?
Rob Landley0369ba52014-05-22 21:33:10 -0500127 else if (*setdate == '+') {
128 format_string = toys.optargs[0]+1;
129 setdate = toys.optargs[1];
Rob Landley7aa651a2012-11-13 17:14:08 -0600130
131 // Set the date
Rob Landley0369ba52014-05-22 21:33:10 -0500132 } else if (setdate) {
Rob Landley6c64f5f2014-04-16 08:54:19 -0500133 struct timeval tv;
Rob Landley6c64f5f2014-04-16 08:54:19 -0500134
Rob Landley0369ba52014-05-22 21:33:10 -0500135 if (parse_posixdate(setdate, &tm)) goto bad_date;
Rob Landley6d878192012-07-21 18:37:26 -0500136
Rob Landley7aa651a2012-11-13 17:14:08 -0600137 if (toys.optflags & FLAG_u) {
Rob Landley7aa651a2012-11-13 17:14:08 -0600138 char *tz = CFG_TOYBOX_FREE ? getenv("TZ") : 0;
Rob Landley0369ba52014-05-22 21:33:10 -0500139
140 // We can't just pass a timezone to mktime because posix.
Rob Landley7aa651a2012-11-13 17:14:08 -0600141 setenv("TZ", "UTC", 1);
142 tzset();
143 tv.tv_sec = mktime(&tm);
144 if (CFG_TOYBOX_FREE) {
145 if (tz) setenv("TZ", tz, 1);
146 else unsetenv("TZ");
147 tzset();
148 }
149 } else tv.tv_sec = mktime(&tm);
Rob Landley669f3322014-04-10 19:40:14 -0500150 if (tv.tv_sec == (time_t)-1) goto bad_date;
Rob Landley26d35be2012-06-18 23:22:08 -0500151
Rob Landley7aa651a2012-11-13 17:14:08 -0600152 tv.tv_usec = 0;
Rob Landley7aa651a2012-11-13 17:14:08 -0600153 if (settimeofday(&tv, NULL) < 0) perror_msg("cannot set date");
154 }
Rob Landley669f3322014-04-10 19:40:14 -0500155
Rob Landley0369ba52014-05-22 21:33:10 -0500156 if (toys.optflags & FLAG_u) {
157 if (tz) setenv("TZ", tz, 1);
158 else unsetenv("TZ");
159 tzset();
160 }
161
162 if (!strftime(toybuf, sizeof(toybuf), format_string, &tm))
163 perror_exit("bad format '%s'", format_string);
164 puts(toybuf);
165
Rob Landley669f3322014-04-10 19:40:14 -0500166 return;
167
168bad_date:
Rob Landley0369ba52014-05-22 21:33:10 -0500169 error_exit("bad date '%s'", setdate);
Rob Landley26d35be2012-06-18 23:22:08 -0500170}