This source file includes following definitions.
- log_time_period
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <crm_internal.h>
20 #include <crm/crm.h>
21 #include <crm/common/iso8601.h>
22 #include <unistd.h>
23
24 char command = 0;
25
26
27 static struct crm_option long_options[] = {
28
29 {"help", 0, 0, '?', "\tThis text"},
30 {"version", 0, 0, '$', "\tVersion information" },
31 {"verbose", 0, 0, 'V', "\tIncrease debug output"},
32
33 {"-spacer-", 0, 0, '-', "\nCommands:"},
34 {"now", 0, 0, 'n', "\tDisplay the current date/time"},
35 {"date", 1, 0, 'd', "Parse an ISO8601 date/time. Eg. '2005-01-20 00:30:00 +01:00' or '2005-040'"},
36 {"period", 1, 0, 'p', "Parse an ISO8601 date/time with interval/period (wth start time). Eg. '2005-040/2005-043'"},
37 {"duration", 1, 0, 'D', "Parse an ISO8601 date/time with duration (wth start time). Eg. '2005-040/P1M'"},
38 {"expected", 1, 0, 'E', "Parse an ISO8601 date/time with duration (wth start time). Eg. '2005-040/P1M'"},
39
40 {"-spacer-",0, 0, '-', "\nOutput Modifiers:"},
41 {"seconds", 0, 0, 's', "\tShow result as a seconds since 0000-001 00:00:00Z"},
42 {"epoch", 0, 0, 'S', "\tShow result as a seconds since EPOCH (1970-001 00:00:00Z)"},
43 {"local", 0, 0, 'L', "\tShow result as a 'local' date/time"},
44 {"ordinal", 0, 0, 'O', "\tShow result as an 'ordinal' date/time"},
45 {"week", 0, 0, 'W', "\tShow result as an 'calendar week' date/time"},
46 {"-spacer-",0, 0, '-', "\nFor more information on the ISO8601 standard, see: http://en.wikipedia.org/wiki/ISO_8601"},
47
48 {0, 0, 0, 0}
49 };
50
51
52 static void
53 log_time_period(int log_level, crm_time_period_t * dtp, int flags)
54 {
55 char *end = NULL;
56 char *start = NULL;
57
58 if(dtp) {
59 start = crm_time_as_string(dtp->start, flags);
60 end = crm_time_as_string(dtp->end, flags);
61 }
62
63 if (log_level < LOG_CRIT) {
64 printf("Period: %s to %s\n", start, end);
65 } else {
66 do_crm_log(log_level, "Period: %s to %s", start, end);
67 }
68 free(start);
69 free(end);
70 }
71
72 int
73 main(int argc, char **argv)
74 {
75 int rc = 0;
76 int argerr = 0;
77 int flag;
78 int index = 0;
79 int print_options = 0;
80 crm_time_t *duration = NULL;
81 crm_time_t *date_time = NULL;
82 crm_time_period_t *interval = NULL;
83
84 const char *period_s = NULL;
85 const char *duration_s = NULL;
86 const char *date_time_s = NULL;
87 const char *expected_s = NULL;
88
89 crm_log_cli_init("iso8601");
90 crm_set_options(NULL, "command [output modifier] ", long_options,
91 "Display and parse ISO8601 dates and times");
92
93 if (argc < 2) {
94 argerr++;
95 }
96
97 while (1) {
98 flag = crm_get_option(argc, argv, &index);
99 if (flag == -1)
100 break;
101
102 switch (flag) {
103 case 'V':
104 crm_bump_log_level(argc, argv);
105 break;
106 case '?':
107 case '$':
108 crm_help(flag, 0);
109 break;
110 case 'n':
111 date_time_s = "now";
112 break;
113 case 'd':
114 date_time_s = optarg;
115 break;
116 case 'p':
117 period_s = optarg;
118 break;
119 case 'D':
120 duration_s = optarg;
121 break;
122 case 'E':
123 expected_s = optarg;
124 break;
125 case 'S':
126 print_options |= crm_time_epoch;
127 break;
128 case 's':
129 print_options |= crm_time_seconds;
130 break;
131 case 'W':
132 print_options |= crm_time_weeks;
133 break;
134 case 'O':
135 print_options |= crm_time_ordinal;
136 break;
137 case 'L':
138 print_options |= crm_time_log_with_timezone;
139 break;
140 break;
141 }
142 }
143
144 if (safe_str_eq("now", date_time_s)) {
145 date_time = crm_time_new(NULL);
146
147 if (date_time == NULL) {
148 fprintf(stderr, "Internal error: couldn't determine 'now'!\n");
149 crm_help('?', 1);
150 }
151 crm_time_log(LOG_TRACE, "Current date/time", date_time,
152 crm_time_ordinal | crm_time_log_date | crm_time_log_timeofday);
153 crm_time_log(-1, "Current date/time", date_time,
154 print_options | crm_time_log_date | crm_time_log_timeofday);
155
156 } else if (date_time_s) {
157 date_time = crm_time_new(date_time_s);
158
159 if (date_time == NULL) {
160 fprintf(stderr, "Invalid date/time specified: %s\n", optarg);
161 crm_help('?', 1);
162 }
163 crm_time_log(LOG_TRACE, "Date", date_time,
164 crm_time_ordinal | crm_time_log_date | crm_time_log_timeofday);
165 crm_time_log(-1, "Date", date_time,
166 print_options | crm_time_log_date | crm_time_log_timeofday);
167 }
168
169 if (duration_s) {
170 duration = crm_time_parse_duration(duration_s);
171
172 if (duration == NULL) {
173 fprintf(stderr, "Invalid duration specified: %s\n", duration_s);
174 crm_help('?', 1);
175 }
176 crm_time_log(LOG_TRACE, "Duration", duration, crm_time_log_duration);
177 crm_time_log(-1, "Duration", duration, print_options | crm_time_log_duration);
178 }
179
180 if (period_s) {
181 interval = crm_time_parse_period(period_s);
182
183 if (interval == NULL) {
184 fprintf(stderr, "Invalid interval specified: %s\n", optarg);
185 crm_help('?', 1);
186 }
187 log_time_period(LOG_TRACE, interval,
188 print_options | crm_time_log_date | crm_time_log_timeofday);
189 log_time_period(-1, interval, print_options | crm_time_log_date | crm_time_log_timeofday);
190 }
191
192 if (date_time && duration) {
193 crm_time_t *later = crm_time_add(date_time, duration);
194
195 crm_time_log(LOG_TRACE, "Duration ends at", later,
196 crm_time_ordinal | crm_time_log_date | crm_time_log_timeofday);
197 crm_time_log(-1, "Duration ends at", later,
198 print_options | crm_time_log_date | crm_time_log_timeofday |
199 crm_time_log_with_timezone);
200 if (expected_s) {
201 char *dt_s = crm_time_as_string(later,
202 print_options | crm_time_log_date |
203 crm_time_log_timeofday);
204 if (safe_str_neq(expected_s, dt_s)) {
205 rc = 1;
206 }
207 free(dt_s);
208 }
209 crm_time_free(later);
210
211 } else if (date_time && expected_s) {
212 char *dt_s = crm_time_as_string(date_time,
213 print_options | crm_time_log_date | crm_time_log_timeofday);
214
215 if (safe_str_neq(expected_s, dt_s)) {
216 rc = 1;
217 }
218 free(dt_s);
219 }
220
221
222
223
224 crm_time_free(date_time);
225 crm_time_free(duration);
226 if (interval) {
227 crm_time_free(interval->start);
228 crm_time_free(interval->end);
229 crm_time_free(interval->diff);
230 free(interval);
231 }
232
233 return crm_exit(rc);
234 }