summaryrefslogtreecommitdiff
path: root/src/time/gmtime.c
blob: c57ceb4b5d66260b2b7afe9916a0684701102479 (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
#include <time.h>
#include "_safety.h"
#include "_time.h"

/** convert arithmetic time to broken down time **/

struct tm * gmtime(const time_t * timer)
{
	static struct tm tm = {0};
	time_t seconds = 0;
	int days = 0;

	SIGNAL_SAFE(0);
	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++;
	}

	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_IN(tm.tm_year, tm.tm_mon)) {
		days -= DAYS_IN(tm.tm_year, 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;
}

CHECK_1(struct tm *, 0, gmtime, const time_t *)

/***
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)
*/