This repository was archived by the owner on Apr 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecondtime.c
More file actions
306 lines (279 loc) · 9.13 KB
/
secondtime.c
File metadata and controls
306 lines (279 loc) · 9.13 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <math.h> /* NAN, fmodl, isfinite, isnan */
#include <float.h> /* DECIMAL_DIG */
#include <stdio.h> /* stderr, snprintf, sprintf, fprintf, printf */
#include <errno.h> /* errno, ERANGE */
#include <stdlib.h> /* exit, EXIT_FAILURE, EXIT_SUCCESS, realloc, free */
#include <stdint.h> /* uint_least32_t, uint_least8_t, uint_fast8_t,
* uintmax_t, int_fast8_t */
#include <ctype.h> /* isspace */
#include "sbomga.h" /* github.com/a-p-jo/darc/blob/main/sbomga/sbomga.h */
SBOMGA_IMPL(str, realloc, free, 0, char) /* Dynamic SSO string */
#include "config.h" /* Exports SELECTED_YEAR, HOW_TO_PRINT_FLOATS */
#if SELECTED_YEAR == NORMAL_YEAR
#define SECS_IN_YR 31536000.0L
#elif SELECTED_YEAR == LEAP_YEAR
#define SECS_IN_YR 31622400.0L
#elif SELECTED_YEAR == JULIAN_YEAR
#define SECS_IN_YR 31557600.0L
#elif SELECTED_YEAR == GREGORIAN_YEAR
#define SECS_IN_YR 31556952.0L
#elif SELECTED_YEAR == TROPICAL_YEAR
#define SECS_IN_YR 31556925.216L
#elif SELECTED_YEAR == SIDEREAL_YEAR
#define SECS_IN_YR 31558149.7635L
#else
#error "Invalid value for SELECTED_YEAR in config.h"
#endif
typedef struct tm_unit {
long double secs;
char sfx;
} tm_unit;
static const tm_unit tm_units[] = {
{SECS_IN_YR , 'y'},
{SECS_IN_YR/12, 'M'},
{604800 , 'w'},
{86400 , 'd'},
{3600 , 'h'},
{60 , 'm'},
{1 , 's'}
};
/* s2str() format flag bits: Bit i corresponds to tm_units[i]. */
typedef uint_least8_t fmtflags;
static const fmtflags fmtflags_all0 = 0;
static const fmtflags fmtflags_all1 = ~fmtflags_all0;
/* i < bits in fmtflags */
static inline bool fmtflags_get(fmtflags x, uint_fast8_t i)
{
return x & (1 << i);
}
/* x is never NULL */
static inline void fmtflags_set(fmtflags *x, uint_fast8_t i)
{
*x = *x | (1 << i);
}
static inline bool fmtflags_anysetafter(fmtflags x, uint_fast8_t i)
{
/* EXPLANATION:
* Say the low 7 bits of x are 0010100.
* We want to check if any bits after the 3rd bit are set (i=2).
* (1 << 3) gives 0001000.
* (1 << 3)-1 gives 0000111.
* ~((1 << 3)-1) gives 1111000.
* when we bitwise AND this with x:
* All bits until (and including) i are set to 0 (1&0 = 0, 0&0 = 0).
* All bits thereafter are unchanged because (0&1 = 0, 1&1 = 1).
* The resulting value, if any bits were after i were set,
* will evaluate to true when converted to bool.
*/
return x & ~((1 << (i+1)) - 1);
}
#define LEN(x) (sizeof(x)/sizeof(x[0]))
/* https://stackoverflow.com/a/24487623 */
#define STUPID_TOSTR(X) #X
#define TOSTR(X) STUPID_TOSTR(X)
/* About %g: https://stackoverflow.com/a/54162153/13651625
* About DECIMAL_DIG: https://stackoverflow.com/a/19897395
*/
#if HOW_TO_PRINT_FLOATS == READABLY_PRINT_FLOATS
#define LDBL_FMTSPEC "%Lg"
#elif HOW_TO_PRINT_FLOATS == ACCURATELY_PRINT_FLOATS
#define LDBL_FMTSPEC "%."TOSTR(DECIMAL_DIG)"Lg"
#else
#error "Invalid value for HOW_TO_PRINT_FLOATS in config.h"
#endif
/* Convert s seconds to str in specified format.
*
* If bit i of format is set, the tm_units[i] unit is converted to,
* else not.
* Conversion begins with the largest unit,
* converting the seconds to the greatest natural number of units,
* then doing the same with the remaining seconds for the next largest unit,
* and for the last unit convert remaining seconds to fractional number.
*
* dst is cleared and may be realloc'd. Caller to handle deallocation.
*
* Returns true on success or false for formatting or reallocation errors
* (in which case dst is indeterminate).
*/
static bool s2str(long double s, fmtflags format, str *dst)
{
dst->len = 0;
bool atleastone = false;
for (uint_fast8_t i = 0; i < LEN(tm_units); i++) {
if (!fmtflags_get(format, i))
continue; /* Ignore this unit */
if (fmtflags_anysetafter(format, i)) {
uintmax_t x = s/tm_units[i].secs; /* truncates */
s = fmodl(s, tm_units[i].secs);
if (x) {
atleastone = true;
#define WRITE_UNIT(fmtstr) do { \
/* Find space needed to print to dst */ \
int n = snprintf( \
NULL, 0, \
(fmtstr), \
x, tm_units[i].sfx \
) + 1; \
if (n < 0 || !str_reserve(dst,dst->len+n)) \
return false; \
/* Write to dst and update it's length */ \
dst->len += sprintf( \
str_arr(dst)+dst->len, (fmtstr), \
x, tm_units[i].sfx \
); \
} while (0)
WRITE_UNIT("%ju%c ");
}
} else { /* This is the last unit, convert to fractional. */
long double x = s/tm_units[i].secs;
/* If x is 0, write iff no units previously written */
if (fpclassify(x) == FP_ZERO && atleastone)
break;
else
WRITE_UNIT(LDBL_FMTSPEC"%c");
#undef WRITE_UNIT
}
}
return true;
}
/* If c is a suffix in tm_units, return its index, else return -1 */
static inline int_fast8_t isfx(char c)
{
for (uint_fast8_t i = 0; i < LEN(tm_units); i++)
if (c == tm_units[i].sfx)
return i;
return -1;
}
/* Converts src upto len chars to seconds, returning NAN on error */
static long double str2s(const char *restrict src, size_t len)
{
long double s = 0;
/* As unit is designated by suffix, iterate backwards */
while (len --> 0) {
int_fast8_t ci = isfx(src[len]);
if (ci < 0) /* prev char is non-suffix, input is invalid */
return NAN;
else {
const char *restrict cp = src+len;
/* Rewind to start of value */
while (len && isfx(src[len-1]) < 0)
len--;
char *ep;
int olderrno = errno; errno = 0;
/* Convert units to seconds and inc s */
s += strtold(src+len, &ep) * tm_units[ci].secs;
int newerrno = errno; errno = olderrno;
if (
ep != cp || signbit(s)
|| !isfinite(s) || newerrno == ERANGE
)
return NAN;
}
}
return s;
}
/* Converts cstring to fmtflags, ignoring whitespace
* but disallowing other non-suffix chars.
* If src is NULL, enable all.
*/
static bool str2fmtflags(fmtflags *dst, const char *restrict src)
{
if (!src) {
*dst = fmtflags_all1;
return true;
}
*dst = fmtflags_all0;
for (char c; (c = *src); src++) {
if (isspace(c))
continue;
int_fast8_t i = isfx(c);
if (i < 0)
return false;
else
fmtflags_set(dst, i);
}
return true;
}
int main(int argc, char **argv)
{
int ret = EXIT_FAILURE;
if (argc >= 2) {
char *e;
/* Assume argument is numerical */
long double s = strtold(argv[1], &e);
if (*e) { /* Not numerical, may be in time units */
if (argc > 2)
goto badargs;
s = str2s(argv[1], strlen(argv[1]));
if (isnan(s))
fprintf(stderr, "Error: Invalid argument.\n");
else {
printf(LDBL_FMTSPEC"s\n", s);
ret = EXIT_SUCCESS;
}
} else if (signbit(s) || !isfinite(s) || errno == ERANGE)
fprintf(stderr, "Error: Argument out of range.\n");
else { /* Is numerical, convert seconds to time units */
fmtflags fmt;
if (!str2fmtflags(&fmt, argv[2]))
goto badargs;
str buf = {0};
if (!s2str(s, fmt, &buf))
fprintf(stderr, "Error: Couldn't convert.\n");
else {
printf("%s\n", str_arr(&buf));
ret = EXIT_SUCCESS;
}
str_destroy(&buf);
}
} else badargs:
fprintf(stderr,
"Error : Incorrect argument(s).\n"
"Usage : %s <time> [format]\n"
"\n"
"Help : This program lets you use seconds as your unit of time.\n"
" It has two basic functions:\n"
" (1) Convert seconds to time units.\n"
" (2) Convert time units to seconds.\n"
"\n"
" (1) To convert seconds, use it like so:\n"
"\n"
" \t%s <number of seconds>\n"
"\n"
" The number of seconds may be any +ve real number.\n"
" You can also express it in exponent form.\n"
" Example: Convert 1.5 x 10^6 seconds.\n"
"\n"
" $ %s 1.5e6\n"
" 2w 3d 8h 40m\n"
"\n"
" (2 weeks, 3 days, 8 hours 40 minutes)\n"
"\n"
" (1.1) To convert to specific unit(s), use it like so:\n"
"\n"
" \t%s <number of seconds> [unit(s)]\n"
"\n"
" To specify unit(s), just specify their suffix:\n"
" years -> y, months -> M, weeks -> w, days -> d\n"
" hours -> h, minutes -> m, seconds -> s\n"
" Example: Convert 1.5 x 10^6 seconds to weeks and days.\n"
"\n"
" $ %s 1.5e6 wd\n"
" 2w 3.36111d\n"
"\n"
" (2 weeks, 3.36111 days)\n"
"\n"
" (2) To convert time to seconds, use it like so:\n"
"\n"
" \t%s <time units with suffix>\n"
"\n"
" Write it like variables and coefficients in algebra.\n"
" Example: Convert 2 weeks, 3 days, 8 hours and 40 minutes.\n"
"\n"
" $ %s 2w3d8h40m\n"
" 1.5e+06s\n"
"\n"
" (1.5 x 10^6 seconds)\n"
, argv[0], argv[0], argv[0], argv[0], argv[0], argv[0], argv[0]);
return ret;
}