Rob Landley | 26d35be | 2012-06-18 23:22:08 -0500 | [diff] [blame^] | 1 | /* vi: set sw=4 ts=4: |
| 2 | * |
| 3 | * date.c - set/get the date |
| 4 | * |
| 5 | * Copyright 2012 Andre Renaud <andre@bluewatersys.com> |
| 6 | * |
| 7 | * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html |
| 8 | |
| 9 | USE_DATE(NEWTOY(date, "u", TOYFLAG_BIN)) |
| 10 | |
| 11 | config DATE |
| 12 | bool "date" |
| 13 | default y |
| 14 | help |
| 15 | usage: date [-u] [+format] | mmddhhmm[[cc]yy] |
| 16 | |
| 17 | Set/get the current date/time |
| 18 | */ |
| 19 | |
| 20 | #include "toys.h" |
| 21 | |
| 22 | /* Convert a string of decimal numbers to their integer equivalent */ |
| 23 | static int fromdec(const char *buf, int len) |
| 24 | { |
| 25 | int result = 0; |
| 26 | while (len--) |
| 27 | result=result * 10 + (*buf++ - '0'); |
| 28 | return result; |
| 29 | } |
| 30 | |
| 31 | void date_main(void) |
| 32 | { |
| 33 | const char *format_string = "%a %b %e %H:%M:%S %Z %Y"; |
| 34 | |
| 35 | /* Check if we should be displaying the date */ |
| 36 | if (!toys.optargs[0] || toys.optargs[0][0] == '+') { |
| 37 | time_t now = time(NULL); |
| 38 | struct tm *tm; |
| 39 | if (toys.optargs[0]) |
| 40 | format_string = &toys.optargs[0][1]; |
| 41 | if (toys.optflags) |
| 42 | tm = gmtime(&now); |
| 43 | else |
| 44 | tm = localtime(&now); |
| 45 | if (!tm) |
| 46 | perror_msg("Unable to retrieve current time"); |
| 47 | if (!strftime(toybuf, sizeof(toybuf), format_string, tm)) |
| 48 | perror_msg("invalid format string `%s'", format_string); |
| 49 | puts(toybuf); |
| 50 | } else { |
| 51 | int len = strlen(toys.optargs[0]); |
| 52 | struct tm tm; |
| 53 | struct timeval tv; |
| 54 | |
| 55 | if (len < 8 || len > 12 || len & 1) |
| 56 | error_msg("invalid date `%s'", toys.optargs[0]); |
| 57 | memset(&tm, 0, sizeof(tm)); |
| 58 | /* Date format: mmddhhmm[[cc]yy] */ |
| 59 | tm.tm_mon = fromdec(toys.optargs[0], 2) - 1; |
| 60 | tm.tm_mday = fromdec(&toys.optargs[0][2], 2); |
| 61 | tm.tm_hour = fromdec(&toys.optargs[0][4], 2); |
| 62 | tm.tm_min = fromdec(&toys.optargs[0][6], 2); |
| 63 | if (len == 12) |
| 64 | tm.tm_year = fromdec(&toys.optargs[0][8], 4) - 1900; |
| 65 | else if (len == 10) { |
| 66 | tm.tm_year = fromdec(&toys.optargs[0][8], 2); |
| 67 | /* 69-99 = 1969-1999, 0 - 68 = 2000-2068 */ |
| 68 | if (tm.tm_year < 69) |
| 69 | tm.tm_year += 100; |
| 70 | } else { |
| 71 | /* Year not specified, so retrieve current year */ |
| 72 | time_t now = time(NULL); |
| 73 | struct tm *now_tm = localtime(&now); |
| 74 | if (!now_tm) |
| 75 | perror_msg("Unable to retrieve current time"); |
| 76 | tm.tm_year = now_tm->tm_year; |
| 77 | } |
| 78 | if (!toys.optflags) |
| 79 | tv.tv_sec = mktime(&tm); |
| 80 | else { |
| 81 | /* Get the UTC version of a struct tm */ |
| 82 | char *tz = NULL; |
| 83 | tz = getenv("TZ"); |
| 84 | setenv("TZ", "", 1); |
| 85 | tzset(); |
| 86 | tv.tv_sec = mktime(&tm); |
| 87 | if (tz) |
| 88 | setenv("TZ", tz, 1); |
| 89 | else |
| 90 | unsetenv("TZ"); |
| 91 | tzset(); |
| 92 | } |
| 93 | |
| 94 | if (tv.tv_sec == (time_t)-1) |
| 95 | error_msg("invalid date `%s'", toys.optargs[0]); |
| 96 | tv.tv_usec = 0; |
| 97 | if (!strftime(toybuf, sizeof(toybuf), format_string, &tm)) |
| 98 | perror_msg("invalid format string `%s'", format_string); |
| 99 | puts(toybuf); |
| 100 | if (settimeofday(&tv, NULL) < 0) |
| 101 | perror_msg("cannot set date"); |
| 102 | } |
| 103 | } |