aboutsummaryrefslogtreecommitdiff
path: root/toys/date.c
blob: 925565a274fbdb61c764627f54b7b899d750560a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* vi: set sw=4 ts=4:
 *
 * date.c - set/get the date
 *
 * Copyright 2012 Andre Renaud <andre@bluewatersys.com>
 *
 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html

USE_DATE(NEWTOY(date, "r:u", TOYFLAG_BIN))

config DATE
	bool "date"
	default y
	help
	  usage: date [-u] [-r file] [+format] | mmddhhmm[[cc]yy]

	  Set/get the current date/time
*/

#include "toys.h"

DEFINE_GLOBALS(
    char *file;
)

#define TT this.date

#define FLAG_u 1
#define FLAG_r 2

void date_main(void)
{
    const char *format_string = "%a %b %e %H:%M:%S %Z %Y";
    time_t now = time(NULL);
    struct tm tm;

    if (TT.file) {
        struct stat st;

        xstat(TT.file, &st);
        now = st.st_mtim.tv_sec;
    }
    ((toys.optflags & FLAG_u) ? gmtime_r : localtime_r)(&now, &tm);

    // Display the date?
    if (!toys.optargs[0] || toys.optargs[0][0] == '+') {
        if (toys.optargs[0]) format_string = toys.optargs[0]+1;
        if (!strftime(toybuf, sizeof(toybuf), format_string, &tm))
            perror_msg("bad format `%s'", format_string);

        puts(toybuf);

    // Set the date
    } else {
        struct timeval tv;
        char *s = *toys.optargs;
        int len = strlen(s);

        if (len < 8 || len > 12 || (len & 1)) error_msg("bad date `%s'", s);

        // Date format: mmddhhmm[[cc]yy]
        memset(&tm, 0, sizeof(tm));
        len = sscanf(s, "%2u%2u%2u%2u", &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
               &tm.tm_min);
        tm.tm_mon--;

        // If year specified, overwrite one we fetched earlier
        if (len > 8) {
            sscanf(s, "%u", &tm.tm_year);
            if (len == 12) tm.tm_year -= 1900;
            /* 69-99 = 1969-1999, 0 - 68 = 2000-2068 */
            else if (tm.tm_year < 69) tm.tm_year += 100;
        }

        if (toys.optflags & FLAG_u) {
            // Get the UTC version of a struct tm
            char *tz = CFG_TOYBOX_FREE ? getenv("TZ") : 0;
            setenv("TZ", "UTC", 1);
            tzset();
            tv.tv_sec = mktime(&tm);
            if (CFG_TOYBOX_FREE) {
                if (tz) setenv("TZ", tz, 1);
                else unsetenv("TZ");
                tzset();
            }
        } else tv.tv_sec = mktime(&tm);

        if (tv.tv_sec == (time_t)-1) error_msg("bad `%s'", toys.optargs[0]);
        tv.tv_usec = 0;
        if (!strftime(toybuf, sizeof(toybuf), format_string, &tm))
            perror_msg("bad format `%s'", format_string);
        puts(toybuf);
        if (settimeofday(&tv, NULL) < 0) perror_msg("cannot set date");
    }
}