aboutsummaryrefslogtreecommitdiff
path: root/src/tm.c
blob: 99c7ccc57064866e61f3e4f17987ec33cc7973b5 (plain) (blame)
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "date.h"
#include "janet.h"

// wrappers around struct tm

static JanetMethod jd_tm_methods[] = {
	// raw mktime, returning time_t
	{"mktime",     jd_mktime},
	{"mktime!",    jd_mktime_inplace},
	// shortcut for localtime(mktime)
	{"localtime",  jd_time_localtime},
	{"localtime!", jd_time_localtime_inplace},
	// shortcut for gmtime(mktime)
	{"utc",        jd_time_utc},
	{"utc!",       jd_time_utc_inplace},
	{"strftime",   jd_strftime},
	{NULL, NULL},
};

static int jd_tm_compare(void *lhs, void *rhs) {
	struct tm lhp = (*(struct tm*)lhs);
	struct tm rhp = (*(struct tm*)rhs);
	time_t lhv = mktime(&lhp);
	time_t rhv = mktime(&rhp);
	return difftime(lhv, rhv);
}

// C99 specifies ALL fields to be int
struct jd_tm_key {
	const char  *key;
	const size_t off;
};
static const struct jd_tm_key jd_tm_keys[] = {
	{"sec",   offsetof(struct tm, tm_sec)},
	{"min",   offsetof(struct tm, tm_min)},
	{"hour",  offsetof(struct tm, tm_hour)},
	{"mday",  offsetof(struct tm, tm_mday)},
	{"mon",   offsetof(struct tm, tm_mon)},
	{"year",  offsetof(struct tm, tm_year)},
	{"wday",  offsetof(struct tm, tm_wday)},
	{"yday",  offsetof(struct tm, tm_yday)},
	{"isdst", offsetof(struct tm, tm_isdst)},
#ifdef TM_GMTOFF
	{"gmtoff", offsetof(struct tm, TM_GMTOFF)},
#endif
#ifdef TM_ZONE
	{"zone", offsetof(struct tm, TM_ZONE)},
#endif
	{NULL, 0},
};

static int jd_tm_get(void *p, Janet key, Janet *out) {
	if (!janet_checktype(key, JANET_KEYWORD)) {
		return 0;
	}

	// is it a method?
	if(janet_getmethod(janet_unwrap_keyword(key), jd_tm_methods, out)) {
		return 1;
	}

	const struct jd_tm_key *ptr = jd_tm_keys;
	while (ptr->key) {
		if (janet_keyeq(key, ptr->key)) {
#ifdef TM_GMTOFF
			if (janet_keyeq(key, "gmtoff")) {
				long val = *((long*)(p + ptr->off));
				*out = janet_wrap_s64(val);
				return 1;
			}
#endif
#ifdef TM_ZONE
			if (janet_keyeq(key, "zone")) {
				const char *val = *((const char **)(p + ptr->off));
				*out = janet_ckeywordv(val);
				return 1;
			}
#endif

			int val = *((int*)(p + ptr->off));

			// exceptional values
			if (janet_keyeq(key, "year")) {
				val += 1900;
			} else if (janet_keyeq(key, "isdst")) {
				*out = val ? (val > 0 ? janet_wrap_true() : janet_ckeywordv("detect")) : janet_wrap_false();
				return 1;
			}

			*out = janet_wrap_integer(val);
			return 1;
		}
		ptr++;
	}

	return janet_checktype(*out, JANET_NIL);
}
static Janet jd_tm_next(void *p, Janet key) {
	(void) p;
	const struct jd_tm_key *ptr = jd_tm_keys;
	while (ptr->key) {
		if (janet_keyeq(key, ptr->key)) {
			return (++ptr)->key ? janet_ckeywordv(ptr->key) : janet_wrap_nil();
		}
		ptr++;
	}
	return janet_ckeywordv(jd_tm_keys[0].key);
}

static void jd_tm_put(void *data, Janet key, Janet value) {
	if (0
#ifdef TM_GMTOFF
		|| janet_keyeq(key, "gmtoff")
#endif
#ifdef TM_ZONE
		|| janet_keyeq(key, "zone")
#endif
	   ) {
		janet_panicf("%s is read-only", key);
	}
	// note that keyword, boolean are only valid for isdst
	if (!janet_checktypes(value, JANET_TFLAG_NUMBER | JANET_TFLAG_KEYWORD | JANET_TFLAG_BOOLEAN)) {
		janet_panicf("expected function or number, got %t", value);
	}
	const struct jd_tm_key *ptr = jd_tm_keys;
	while (ptr->key) {
		if (janet_keyeq(key, ptr->key)) {
			int *loc = (int*)(data + ptr->off);
			if (janet_keyeq(key, "year")) {
				*loc = janet_unwrap_integer(value) - 1900;
			} else if (janet_keyeq(key, "isdst")) {
				*loc = janet_keyeq(value, "detect") ? -1 : (janet_truthy(value) ? 1 : 0);
			} else {
				*loc = janet_unwrap_integer(value);
			}
			return;
		}
		ptr++;
	}

	janet_panicf("tried to write to invalid field: %v", key);
}

#define MAX_INT_STRLEN 1024
static void jd_tm_tostring(void *p, JanetBuffer *buffer) {
	janet_buffer_push_cstring(buffer, "{");
	char buf[MAX_INT_STRLEN];

	const struct jd_tm_key *ptr = jd_tm_keys;
	while (ptr->key) {
		void *loc = (void*)(p + ptr->off);
		janet_buffer_push_cstring(buffer, ":");
		janet_buffer_push_cstring(buffer, ptr->key);
		janet_buffer_push_cstring(buffer, " ");

		// exceptional values
		if (!strcmp(ptr->key, "year")) {
			snprintf(buf, MAX_INT_STRLEN, "%d", *(int*)loc + 1900);
		} else if (!strcmp(ptr->key, "isdst")) {
			strcpy(buf, *(int*)loc ? (*(int*)loc > 0 ? "true" : ":detect") : "false");
#ifdef TM_ZONE
		} else if (!strcmp(ptr->key, "zone")) {
			char *zone = *(char**)loc;
			if (zone) {
				buf[0] = ':';
				strcpy(buf+1, zone);
			} else {
				strcpy(buf, "\"\"");
			}
#endif
#ifdef TM_GMTOFF
		} else if (!strcmp(ptr->key, "gmtoff")) {
			snprintf(buf, MAX_INT_STRLEN, "%ld", *(long*)loc);
#endif
		} else {
			snprintf(buf, MAX_INT_STRLEN, "%d", *(int*)loc);
		}
		janet_buffer_push_cstring(buffer, buf);

		if ((++ptr)->key) janet_buffer_push_cstring(buffer, " ");
	}

	janet_buffer_push_cstring(buffer, "}");
}

static const JanetAbstractType jd_tm_t = {
	"tm",
	NULL,
	NULL,
	jd_tm_get,
	jd_tm_put,
	NULL,
	NULL,
	jd_tm_tostring,
	jd_tm_compare,
	NULL,
	jd_tm_next,
	JANET_ATEND_NEXT
};

struct tm *jd_gettm(Janet *argv, int32_t n) {
	return (struct tm*)janet_getabstract(argv, n, &jd_tm_t);
}

struct tm *jd_maketm(void) {
	return janet_abstract(&jd_tm_t, sizeof(struct tm));
}

struct tm *jd_opttm(Janet *argv, int32_t argc, int32_t n) {
    if (n >= argc || janet_checktype(argv[n], JANET_NIL)) {
		return NULL;
	}
	return jd_gettm(argv, n);
}

JANET_FN(jd_tm,
		"",
		"") {
	janet_fixarity(argc, 1);
	JanetDictView view = janet_getdictionary(argv, 0);

	struct tm *out = jd_maketm();
	memset(out, 0, sizeof(struct tm));
#ifdef TM_GMTOFF
	out->tm_gmtoff = 0;
#endif

	for (int32_t i = 0; i < view.cap; i++) {
		const JanetKV e = view.kvs[i];
		if (!janet_truthy(e.key)) continue;
		const struct jd_tm_key *ptr = jd_tm_keys;
		while (ptr->key) {
			if (janet_keyeq(e.key, ptr->key)) {
				int *target  = (int*)((void*)out + ptr->off);

				// exceptional values
				if (!strcmp(ptr->key, "year")) {
					*target = janet_unwrap_integer(e.value) - 1900;
#ifdef TM_ZONE
				} else if (!strcmp(ptr->key, "zone")) {
					janet_panic("cannot set zone name");
#endif
#ifdef TM_GMTOFF
				} else if (!strcmp(ptr->key, "gmtoff")) {
					janet_panic("cannot set gmt offset");
#endif
				} else if (!strcmp(ptr->key, "isdst")) {
					*target = janet_keyeq(e.value, "detect") ? -1 : (janet_truthy(e.value) ? 1 : 0);
				} else {
					*target = janet_unwrap_integer(e.value);
				}
				break;
			}
			ptr++;
		}
	}
	return janet_wrap_abstract(out);
}

JANET_FN(jd_mktime,
		"",
		"") {
	janet_fixarity(argc, 1);
	struct tm *tm = jd_gettm(argv, 0);
	struct tm *nw = jd_maketm();
	*nw = *tm;
	time_t *time = jd_maketime();
	*time = mktime(nw);
	return janet_wrap_abstract(time);
}

JANET_FN(jd_mktime_inplace,
		"",
		"") {
	janet_fixarity(argc, 1);
	struct tm *tm = jd_gettm(argv, 0);
	time_t *time = jd_maketime();
	*time = mktime(tm);
	return janet_wrap_abstract(time);
}

JANET_FN(jd_time_utc,
		"",
		"") {
	janet_arity(argc, 0, 1);
	struct tm *tm = jd_opttm(argv, argc, 0);
	struct tm *nw = jd_maketm();
	time_t t;
	if (tm) {
		*nw = *tm;
		t = mktime(nw);
	} else {
		t = time(NULL);
	}
	*nw = *(gmtime(&t));
	return janet_wrap_abstract(nw);
}

JANET_FN(jd_time_utc_inplace,
		"",
		"") {
	janet_fixarity(argc, 1);
	struct tm *tm = jd_gettm(argv, 0);
	time_t t = mktime(tm);
	*tm = *(gmtime(&t));
	return janet_wrap_abstract(tm);
}

JANET_FN(jd_time_localtime,
		"",
		"") {
	janet_arity(argc, 0, 1);
	struct tm *tm = jd_opttm(argv, argc, 0);
	struct tm *nw = jd_maketm();
	time_t t;
	if (tm) {
		*nw = *tm;
		t = mktime(nw);
	} else {
		t = time(NULL);
	}
	*nw = *(localtime(&t));
	return janet_wrap_abstract(nw);
}

JANET_FN(jd_time_localtime_inplace,
		"",
		"") {
	janet_fixarity(argc, 1);
	struct tm *tm = jd_gettm(argv, 0);
	time_t t = mktime(tm);
	*tm = *(localtime(&t));
	return janet_wrap_abstract(tm);
}

// strftime
struct strftime_format {
	const char *keyword;
	const char *format;
};
const static struct strftime_format strftime_formats[] = {
	{NULL, NULL},
};
JANET_FN(jd_strftime,
		"",
		"") {
	janet_fixarity(argc, 2);
	// tm is first for pseudo-OO
	struct tm *tm = jd_gettm(argv, 0);

	// determine format
	const char *format = NULL;

	// is it a preset?
	if (janet_checktype(argv[1], JANET_KEYWORD)) {
		const struct strftime_format *ptr = strftime_formats;
		while (ptr->keyword) {
			if (janet_keyeq(argv[1], ptr->keyword)) {
				format = ptr->format;
				break;
			}
			ptr++;
		}
	}

	// preset not found
	if (!format) format = janet_getcbytes(argv, 1);
	return janet_wrap_buffer(strftime_buffer(format, tm, NULL));
}

const JanetRegExt jd_tm_cfuns[] = {
	JANET_REG("tm",       jd_tm),
	JANET_REG("mktime",   jd_mktime),
	JANET_REG("mktime!",  jd_mktime_inplace),
	JANET_REG("strftime", jd_strftime),
	JANET_REG_END
};