forked from akjmicro/dclang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_ops.c
103 lines (95 loc) · 2.66 KB
/
time_ops.c
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
#include <sys/time.h>
#include <time.h>
struct timeval tval;
void clockfunc()
{
gettimeofday(&tval, NULL);
DCLANG_FLT now = ((DCLANG_FLT) tval.tv_sec) + (((DCLANG_FLT) tval.tv_usec) / 1000000);
push(now);
}
void epochfunc()
{
gettimeofday(&tval, NULL);
DCLANG_FLT now = (tval.tv_sec);
push(now);
}
void sleepfunc() {
if (data_stack_ptr < 1) {
printf("sleep -- need a time amount in seconds on the stack!\n");
return;
}
DCLANG_FLT sleeptime = dclang_pop();
struct timespec t1, t2;
t1.tv_sec = floor(sleeptime);
t1.tv_nsec = round(fmod(sleeptime, 1) * 1000000000);
nanosleep(&t1, &t2);
}
// date functions
void dt_to_epochfunc()
{
if (data_stack_ptr < 2)
{
printf("dt->epoch: need a <date> like \"2020-01-01 12:14:13\" and a <input_format> on the stack.\n");
return;
}
// input string setup
DCLANG_UINT fmt = (DCLANG_UINT) dclang_pop();
DCLANG_UINT to_conv = (DCLANG_UINT) dclang_pop();
if (fmt < MIN_STR || fmt > MAX_STR)
{
printf("dt->epoch -- <input_format> string address out-of-range.\n");
return;
}
if (to_conv < MIN_STR || to_conv > MAX_STR)
{
printf("dt->epoch -- <date> string address out-of-range.\n");
return;
}
// memset the dt_conv_tm
struct tm dt_epoch_tm;
memset(&dt_epoch_tm, 0, sizeof(dt_epoch_tm));
// convert to broken time
if (strptime((char *)to_conv, (char *) fmt, &dt_epoch_tm) == NULL)
{
printf("Conversion to broken time failed in 'dt->epoch'\n");
return;
}
// do the conversion to seconds since epoch
time_t res_time = mktime(&dt_epoch_tm);
push((DCLANG_UINT) res_time);
}
void epoch_to_dtfunc()
{
if (data_stack_ptr < 1)
{
printf("epoch->dt: need a <epoch_int> and an <output_format> on the stack.\n");
return;
}
// input string setup
DCLANG_UINT fmt = (DCLANG_UINT) dclang_pop();
if (fmt < MIN_STR || fmt > MAX_STR)
{
printf("epoch->dt -- <output_format> string address out-of-range.\n");
return;
}
DCLANG_UINT in_epoch_uint = (DCLANG_UINT) dclang_pop();
time_t in_epoch = (time_t) in_epoch_uint;
char tmbuf[256];
memset(&tmbuf[0], 0, 256);
struct tm *loctime = localtime(&in_epoch);
if (strftime(tmbuf, 256, (char *) fmt, loctime) == 0)
{
printf("'strftime', a low-level call of 'epoch->dt', returned an error.\n");
return;
}
DCLANG_PTR bufaddr = (DCLANG_PTR) tmbuf;
if (bufaddr > MAX_STR || MAX_STR == 0)
{
MAX_STR = bufaddr;
}
if (bufaddr < MIN_STR || MIN_STR == 0)
{
MIN_STR = bufaddr;
}
push(bufaddr);
}