File: cxxtz.cpp

package info (click to toggle)
cxxtools 3.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,960 kB
  • sloc: cpp: 68,550; ansic: 15,641; sh: 4,239; makefile: 841
file content (109 lines) | stat: -rw-r--r-- 4,065 bytes parent folder | download | duplicates (2)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <cxxtools/tz.h>
#include <cxxtools/log.h>
#include <cxxtools/arg.h>

#include <iostream>

int main(int argc, char* argv[])
{
    try
    {
        log_init();

        cxxtools::Arg<bool> help(argc, argv, '?');
        if (help || argc == 1)
        {
            std::cerr << "Usage: " << argv[0] << " {options} {times}\n"
                         "\n"
                         "Synopsis:\n"
                         "current time in time zone Pacific/Nauru\n"
                         " " << argv[0] << " -c -z Pacific/Nauru\n"
                         "dump information about the current time zone\n"
                         " " << argv[0] << " -d\n"
                         "output some GMT times in the time zone\n"
                         " " << argv[0] << " '2017-03-26 01:00:00' '2017-03-26 00:59:59'\n"
                         "\n"
                         "Options:\n"
                         " -a             list all valid time zones\n"
                         " -z <timezone>  specify time zone (default: system time zone)\n"
                         " -Z             output time zone of the system\n"
                         " -d             dump time zone informations\n"
                         " -c             output current time in the specified time zone\n"
                         " -u             print current utc time\n"
                         " -r             reverse\n"
                         " -e             early flag (with -r take earlier time if ambiguous)\n"
                         " -l             late flag (with -r take later time if ambiguous)\n"
                         " -v             output previous and next change of dst\n";
        }

        cxxtools::Arg<std::string> timeZone(argc, argv, 'z', cxxtools::Tz::currentZone());
        cxxtools::Tz tz(timeZone);

        cxxtools::Arg<bool> dump(argc, argv, 'd');
        cxxtools::Arg<bool> listAll(argc, argv, 'a');
        cxxtools::Arg<bool> currentTime(argc, argv, 'c');
        cxxtools::Arg<bool> utc(argc, argv, 'u');
        cxxtools::Arg<bool> currentTimeZone(argc, argv, 'Z');
        cxxtools::Arg<bool> reverse(argc, argv, 'r');
        cxxtools::Arg<bool> early(argc, argv, 'e');
        cxxtools::Arg<bool> late(argc, argv, 'l');
        cxxtools::Arg<bool> verbose(argc, argv, 'v');  // show previous and next change

        if (dump)
        {
            std::cout << timeZone.getValue() << '\n';
            tz.dump(std::cout);
        }

        if (listAll)
        {
            auto l = cxxtools::Tz::getTimeZones();
            for (auto z: l)
                std::cout << z << '\n';
        }

        if (currentTime)
        {
            std::cout << tz.toLocal(cxxtools::DateTime::gmtime()).toString() << '\n';
        }

        if (utc)
        {
            std::cout << cxxtools::DateTime::gmtime().toString() << '\n';
        }

        if (currentTimeZone)
        {
            std::cout << cxxtools::Tz::currentZone() << '\n';
        }

        for (int a = 1; a < argc; ++a)
        {
            cxxtools::DateTime dt(argv[a]);
            if (reverse)
            {
                cxxtools::UtcDateTime dtz;
                if (early)
                    dtz = tz.toUtc(cxxtools::LocalDateTime(dt), 1);
                else if (late)
                    dtz = tz.toUtc(cxxtools::LocalDateTime(dt), 0);
                else
                    dtz = tz.toUtc(cxxtools::LocalDateTime(dt));
                std::cout << dtz.toString() << '\n';
            }
            else
            {
                auto dtz = tz.toLocal(cxxtools::UtcDateTime(dt));
                std::cout << dtz.toString() << ' ' << dtz.tzName() << ' ' << dtz.isdst();
                if (verbose)
                    std::cout << " previous change: " << tz.previousChange(dtz).toString()
                              << " next change: " << tz.nextChange(dtz).toString();
                std::cout << '\n';
            }
        }
    }
    catch (const std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
}