-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate.cpp
More file actions
77 lines (68 loc) · 2.33 KB
/
date.cpp
File metadata and controls
77 lines (68 loc) · 2.33 KB
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
#include <cstdio>
#include <cstring>
#include <ctime>
#include <string>
#include <cfbox/args.hpp>
#include <cfbox/help.hpp>
#include <cfbox/error.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "date",
.version = CFBOX_VERSION_STRING,
.one_line = "print or set the system date and time",
.usage = "date [+FORMAT]",
.options = " -u print or set Coordinated Universal Time (UTC)\n"
" -d STR display time described by STR",
.extra = "FORMAT supports: %Y %m %d %H %M %S %a %A %b %B %s %Z %j %W",
};
} // namespace
auto date_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {
cfbox::args::OptSpec{'u', false},
cfbox::args::OptSpec{'d', true, "date"},
});
if (parsed.has_long("help")) { cfbox::help::print_help(HELP); return 0; }
if (parsed.has_long("version")) { cfbox::help::print_version(HELP); return 0; }
bool utc = parsed.has('u');
const auto& pos = parsed.positional();
std::string format;
for (auto p : pos) {
if (!p.empty() && p[0] == '+') {
format = std::string{p.substr(1)};
}
}
if (format.empty()) {
format = "%a %b %d %H:%M:%S %Z %Y";
}
time_t now = time(nullptr);
if (auto d = parsed.get_any('d', "date")) {
struct tm tm_val{};
auto* result = strptime(std::string{*d}.c_str(), "%Y-%m-%d %H:%M:%S", &tm_val);
if (!result) {
result = strptime(std::string{*d}.c_str(), "%Y-%m-%d", &tm_val);
}
if (!result) {
result = strptime(std::string{*d}.c_str(), "%H:%M:%S", &tm_val);
if (result) {
auto* now_tm = localtime(&now);
tm_val.tm_year = now_tm->tm_year;
tm_val.tm_mon = now_tm->tm_mon;
tm_val.tm_mday = now_tm->tm_mday;
}
}
if (result) {
now = mktime(&tm_val);
} else {
CFBOX_ERR("date", "invalid date '%.*s'", static_cast<int>(d->size()), d->data());
return 1;
}
}
auto* tm = utc ? gmtime(&now) : localtime(&now);
char buf[256];
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
strftime(buf, sizeof(buf), format.c_str(), tm);
std::puts(buf);
#pragma GCC diagnostic pop
return 0;
}