Mercurial > hg > toybox
annotate toys/posix/date.c @ 674:7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 08 Oct 2012 00:02:30 -0500 |
parents | 6df4ccc0acbe |
children | 786841fdb1e0 |
rev | line source |
---|---|
602
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
1 /* vi: set sw=4 ts=4: |
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
2 * |
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
3 * date.c - set/get the date |
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
4 * |
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
5 * Copyright 2012 Andre Renaud <andre@bluewatersys.com> |
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
6 * |
656
6df4ccc0acbe
Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/date.html |
602
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
8 |
628
3041521db5d0
Largely cosmetic code cleanups.
Rob Landley <rob@landley.net>
parents:
602
diff
changeset
|
9 USE_DATE(NEWTOY(date, "r:u", TOYFLAG_BIN)) |
602
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
10 |
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
11 config DATE |
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
12 bool "date" |
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
13 default y |
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
14 help |
636
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
15 usage: date [-u] [-r file] [+format] | mmddhhmm[[cc]yy] |
602
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
16 |
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
17 Set/get the current date/time |
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
18 */ |
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
19 |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
20 #define FOR_date |
602
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
21 #include "toys.h" |
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
22 |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
23 GLOBALS( |
636
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
24 char *file; |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
25 ) |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
26 |
602
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
27 void date_main(void) |
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
28 { |
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
29 const char *format_string = "%a %b %e %H:%M:%S %Z %Y"; |
636
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
30 time_t now = time(NULL); |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
31 struct tm tm; |
602
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
32 |
636
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
33 if (TT.file) { |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
34 struct stat st; |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
35 |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
36 xstat(TT.file, &st); |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
37 now = st.st_mtim.tv_sec; |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
38 } |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
39 ((toys.optflags & FLAG_u) ? gmtime_r : localtime_r)(&now, &tm); |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
40 |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
41 // Display the date? |
602
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
42 if (!toys.optargs[0] || toys.optargs[0][0] == '+') { |
628
3041521db5d0
Largely cosmetic code cleanups.
Rob Landley <rob@landley.net>
parents:
602
diff
changeset
|
43 if (toys.optargs[0]) format_string = toys.optargs[0]+1; |
636
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
44 if (!strftime(toybuf, sizeof(toybuf), format_string, &tm)) |
628
3041521db5d0
Largely cosmetic code cleanups.
Rob Landley <rob@landley.net>
parents:
602
diff
changeset
|
45 perror_msg("bad format `%s'", format_string); |
636
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
46 |
602
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
47 puts(toybuf); |
636
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
48 |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
49 // Set the date |
602
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
50 } else { |
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
51 struct timeval tv; |
636
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
52 char *s = *toys.optargs; |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
53 int len = strlen(s); |
602
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
54 |
636
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
55 if (len < 8 || len > 12 || (len & 1)) error_msg("bad date `%s'", s); |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
56 |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
57 // Date format: mmddhhmm[[cc]yy] |
602
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
58 memset(&tm, 0, sizeof(tm)); |
636
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
59 len = sscanf(s, "%2u%2u%2u%2u", &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
60 &tm.tm_min); |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
61 tm.tm_mon--; |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
62 |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
63 // If year specified, overwrite one we fetched earlier |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
64 if (len > 8) { |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
65 sscanf(s, "%u", &tm.tm_year); |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
66 if (len == 12) tm.tm_year -= 1900; |
602
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
67 /* 69-99 = 1969-1999, 0 - 68 = 2000-2068 */ |
636
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
68 else if (tm.tm_year < 69) tm.tm_year += 100; |
602
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
69 } |
636
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
70 |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
71 if (toys.optflags & FLAG_u) { |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
72 // Get the UTC version of a struct tm |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
73 char *tz = CFG_TOYBOX_FREE ? getenv("TZ") : 0; |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
74 setenv("TZ", "UTC", 1); |
602
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
75 tzset(); |
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
76 tv.tv_sec = mktime(&tm); |
636
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
77 if (CFG_TOYBOX_FREE) { |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
78 if (tz) setenv("TZ", tz, 1); |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
79 else unsetenv("TZ"); |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
80 tzset(); |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
81 } |
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
82 } else tv.tv_sec = mktime(&tm); |
602
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
83 |
636
42ae383499c5
Add date -r, make -u work even when /etc/localtime set, and lots of cleanup/refactoring.
Rob Landley <rob@landley.net>
parents:
628
diff
changeset
|
84 if (tv.tv_sec == (time_t)-1) error_msg("bad `%s'", toys.optargs[0]); |
602
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
85 tv.tv_usec = 0; |
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
86 if (!strftime(toybuf, sizeof(toybuf), format_string, &tm)) |
628
3041521db5d0
Largely cosmetic code cleanups.
Rob Landley <rob@landley.net>
parents:
602
diff
changeset
|
87 perror_msg("bad format `%s'", format_string); |
602
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
88 puts(toybuf); |
628
3041521db5d0
Largely cosmetic code cleanups.
Rob Landley <rob@landley.net>
parents:
602
diff
changeset
|
89 if (settimeofday(&tv, NULL) < 0) perror_msg("cannot set date"); |
602
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
90 } |
12eddd9a1fe4
Add date command by Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
91 } |