blob: a193fb993fef57087a8d9d90ba783afe35167871 (
plain)
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
|
#include <time.h>
#include "../_assert.h"
# define SEC_PER_MIN (60L)
# define MIN_PER_HR (60L)
# define SEC_PER_HR ((SEC_PER_MIN) * (MIN_PER_HR))
# define HR_PER_DAY (24L)
# define SEC_PER_DAY ((SEC_PER_HR) * (HR_PER_DAY))
# define DAY_PER_YEAR (365L) /* not counting leap year */
# define SEC_PER_YEAR ((SEC_PER_DAY) * (DAY_PER_YEAR))
# define ISLEAPYEAR(y) ((y) % 4L == 0 && ((y) % 100L != 0 || (y) % 400L == 0))
# define EPOCH_YEAR (70)
/** convert arithmetic time to borken down time **/
struct tm * gmtime(const time_t * timer)
{
static struct tm tm = {0};
time_t seconds = 0;
int days = 0;
int days_per_mon[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
ASSERT_NONNULL(timer);
seconds = *timer;
tm.tm_year = EPOCH_YEAR;
while (seconds > SEC_PER_YEAR) {
/* TODO: Will this mess up Dec 31 of leap year? */
/* TODO: Or Jan 1 of year after leap year? */
seconds -= SEC_PER_YEAR;
if (ISLEAPYEAR(tm.tm_year + 1900)) {
seconds -= SEC_PER_DAY;
}
tm.tm_year++;
}
if (ISLEAPYEAR(tm.tm_year + 1900)) {
days_per_mon[1] = 29;
}
tm.tm_yday = (int)(seconds / SEC_PER_DAY);
seconds = seconds % SEC_PER_DAY;
tm.tm_hour = (int)(seconds / SEC_PER_HR);
seconds = seconds % SEC_PER_HR;
tm.tm_min = (int)(seconds / SEC_PER_MIN);
tm.tm_sec = (int)(seconds % SEC_PER_MIN);
days = tm.tm_yday;
tm.tm_mon = 0;
while (days > days_per_mon[tm.tm_mon]) {
days -= days_per_mon[tm.tm_mon];
tm.tm_mon++;
}
tm.tm_mday = days;
/* TODO: tm_wday */
tm.tm_wday = 0;
/* TODO: tm_isdst */
tm.tm_isdst = 0;
return &tm;
}
/***
converts the UTC time at ARGUMENT(timer) to a filled out STRUCTDEF(tm).
***/
/*
RETURN_FAILURE(CONSTANT(NULL))
RETURN_SUCCESS(a pointer to the converted time)
STDC(1)
*/
|