Mercurial > hg > toybox
comparison toys/posix/date.c @ 694:786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
The actual code should be the same afterward, this is just cosmetic refactoring.
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 13 Nov 2012 17:14:08 -0600 |
parents | 7e846e281e38 |
children | 22f02dfb33ab |
comparison
equal
deleted
inserted
replaced
693:4a5a250e0633 | 694:786841fdb1e0 |
---|---|
1 /* vi: set sw=4 ts=4: | 1 /* date.c - set/get the date |
2 * | |
3 * date.c - set/get the date | |
4 * | 2 * |
5 * Copyright 2012 Andre Renaud <andre@bluewatersys.com> | 3 * Copyright 2012 Andre Renaud <andre@bluewatersys.com> |
6 * | 4 * |
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/date.html | 5 * See http://opengroup.org/onlinepubs/9699919799/utilities/date.html |
8 | 6 |
9 USE_DATE(NEWTOY(date, "r:u", TOYFLAG_BIN)) | 7 USE_DATE(NEWTOY(date, "r:u", TOYFLAG_BIN)) |
10 | 8 |
11 config DATE | 9 config DATE |
12 bool "date" | 10 bool "date" |
13 default y | 11 default y |
14 help | 12 help |
15 usage: date [-u] [-r file] [+format] | mmddhhmm[[cc]yy] | 13 usage: date [-u] [-r file] [+format] | mmddhhmm[[cc]yy] |
16 | 14 |
17 Set/get the current date/time | 15 Set/get the current date/time |
18 */ | 16 */ |
19 | 17 |
20 #define FOR_date | 18 #define FOR_date |
21 #include "toys.h" | 19 #include "toys.h" |
22 | 20 |
23 GLOBALS( | 21 GLOBALS( |
24 char *file; | 22 char *file; |
25 ) | 23 ) |
26 | 24 |
27 void date_main(void) | 25 void date_main(void) |
28 { | 26 { |
29 const char *format_string = "%a %b %e %H:%M:%S %Z %Y"; | 27 const char *format_string = "%a %b %e %H:%M:%S %Z %Y"; |
30 time_t now = time(NULL); | 28 time_t now = time(NULL); |
31 struct tm tm; | 29 struct tm tm; |
32 | 30 |
33 if (TT.file) { | 31 if (TT.file) { |
34 struct stat st; | 32 struct stat st; |
35 | 33 |
36 xstat(TT.file, &st); | 34 xstat(TT.file, &st); |
37 now = st.st_mtim.tv_sec; | 35 now = st.st_mtim.tv_sec; |
36 } | |
37 ((toys.optflags & FLAG_u) ? gmtime_r : localtime_r)(&now, &tm); | |
38 | |
39 // Display the date? | |
40 if (!toys.optargs[0] || toys.optargs[0][0] == '+') { | |
41 if (toys.optargs[0]) format_string = toys.optargs[0]+1; | |
42 if (!strftime(toybuf, sizeof(toybuf), format_string, &tm)) | |
43 perror_msg("bad format `%s'", format_string); | |
44 | |
45 puts(toybuf); | |
46 | |
47 // Set the date | |
48 } else { | |
49 struct timeval tv; | |
50 char *s = *toys.optargs; | |
51 int len = strlen(s); | |
52 | |
53 if (len < 8 || len > 12 || (len & 1)) error_msg("bad date `%s'", s); | |
54 | |
55 // Date format: mmddhhmm[[cc]yy] | |
56 memset(&tm, 0, sizeof(tm)); | |
57 len = sscanf(s, "%2u%2u%2u%2u", &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, | |
58 &tm.tm_min); | |
59 tm.tm_mon--; | |
60 | |
61 // If year specified, overwrite one we fetched earlier | |
62 if (len > 8) { | |
63 sscanf(s, "%u", &tm.tm_year); | |
64 if (len == 12) tm.tm_year -= 1900; | |
65 /* 69-99 = 1969-1999, 0 - 68 = 2000-2068 */ | |
66 else if (tm.tm_year < 69) tm.tm_year += 100; | |
38 } | 67 } |
39 ((toys.optflags & FLAG_u) ? gmtime_r : localtime_r)(&now, &tm); | |
40 | 68 |
41 // Display the date? | 69 if (toys.optflags & FLAG_u) { |
42 if (!toys.optargs[0] || toys.optargs[0][0] == '+') { | 70 // Get the UTC version of a struct tm |
43 if (toys.optargs[0]) format_string = toys.optargs[0]+1; | 71 char *tz = CFG_TOYBOX_FREE ? getenv("TZ") : 0; |
44 if (!strftime(toybuf, sizeof(toybuf), format_string, &tm)) | 72 setenv("TZ", "UTC", 1); |
45 perror_msg("bad format `%s'", format_string); | 73 tzset(); |
74 tv.tv_sec = mktime(&tm); | |
75 if (CFG_TOYBOX_FREE) { | |
76 if (tz) setenv("TZ", tz, 1); | |
77 else unsetenv("TZ"); | |
78 tzset(); | |
79 } | |
80 } else tv.tv_sec = mktime(&tm); | |
46 | 81 |
47 puts(toybuf); | 82 if (tv.tv_sec == (time_t)-1) error_msg("bad `%s'", toys.optargs[0]); |
48 | 83 tv.tv_usec = 0; |
49 // Set the date | 84 if (!strftime(toybuf, sizeof(toybuf), format_string, &tm)) |
50 } else { | 85 perror_msg("bad format `%s'", format_string); |
51 struct timeval tv; | 86 puts(toybuf); |
52 char *s = *toys.optargs; | 87 if (settimeofday(&tv, NULL) < 0) perror_msg("cannot set date"); |
53 int len = strlen(s); | 88 } |
54 | |
55 if (len < 8 || len > 12 || (len & 1)) error_msg("bad date `%s'", s); | |
56 | |
57 // Date format: mmddhhmm[[cc]yy] | |
58 memset(&tm, 0, sizeof(tm)); | |
59 len = sscanf(s, "%2u%2u%2u%2u", &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, | |
60 &tm.tm_min); | |
61 tm.tm_mon--; | |
62 | |
63 // If year specified, overwrite one we fetched earlier | |
64 if (len > 8) { | |
65 sscanf(s, "%u", &tm.tm_year); | |
66 if (len == 12) tm.tm_year -= 1900; | |
67 /* 69-99 = 1969-1999, 0 - 68 = 2000-2068 */ | |
68 else if (tm.tm_year < 69) tm.tm_year += 100; | |
69 } | |
70 | |
71 if (toys.optflags & FLAG_u) { | |
72 // Get the UTC version of a struct tm | |
73 char *tz = CFG_TOYBOX_FREE ? getenv("TZ") : 0; | |
74 setenv("TZ", "UTC", 1); | |
75 tzset(); | |
76 tv.tv_sec = mktime(&tm); | |
77 if (CFG_TOYBOX_FREE) { | |
78 if (tz) setenv("TZ", tz, 1); | |
79 else unsetenv("TZ"); | |
80 tzset(); | |
81 } | |
82 } else tv.tv_sec = mktime(&tm); | |
83 | |
84 if (tv.tv_sec == (time_t)-1) error_msg("bad `%s'", toys.optargs[0]); | |
85 tv.tv_usec = 0; | |
86 if (!strftime(toybuf, sizeof(toybuf), format_string, &tm)) | |
87 perror_msg("bad format `%s'", format_string); | |
88 puts(toybuf); | |
89 if (settimeofday(&tv, NULL) < 0) perror_msg("cannot set date"); | |
90 } | |
91 } | 89 } |