summaryrefslogtreecommitdiff
path: root/src/time/mktime.c
blob: 3b415b4c2373910c627d552fbdb775db1210d036 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <time.h>
#include "_time.h"
#include "_assert.h"

/** convert time structure to arithmetic type **/

time_t mktime(struct tm * timeptr)
{
	int i;

	ASSERT_NONNULL(timeptr);

	#ifdef _POSIX_SOURCE
	tzset();
	#endif

	while (timeptr->tm_sec < 0) {
		timeptr->tm_min--;
		timeptr->tm_sec += SEC_PER_MIN;
	}

	if (timeptr->tm_sec >= SEC_PER_MIN) {
		timeptr->tm_min += timeptr->tm_sec / SEC_PER_MIN;
		timeptr->tm_sec %= SEC_PER_MIN;
	}

	while (timeptr->tm_min < 0) {
		timeptr->tm_hour--;
		timeptr->tm_min += MIN_PER_HR;
	}

	if (timeptr->tm_min >= MIN_PER_HR) {
		timeptr->tm_hour += timeptr->tm_min / MIN_PER_HR;
		timeptr->tm_sec %= MIN_PER_HR;
	}

	while (timeptr->tm_hour < 0) {
		timeptr->tm_mday--;
		timeptr->tm_hour += HR_PER_DAY;
	}

	if (timeptr->tm_hour >= HR_PER_DAY) {
		timeptr->tm_mday += timeptr->tm_hour / HR_PER_DAY;
		timeptr->tm_hour %= HR_PER_DAY;
	}

	/* tm_mday is 1 based */
	while (timeptr->tm_mday < 1) {
		timeptr->tm_mon--;
		timeptr->tm_mday += DAYS_IN(timeptr->tm_year, timeptr->tm_mon);
	}

	while (timeptr->tm_mday > DAYS_IN(timeptr->tm_year, timeptr->tm_mon)) {
		timeptr->tm_mday -= DAYS_IN(timeptr->tm_year, timeptr->tm_mon);
		timeptr->tm_mon++;
	}

	/* negative years are OK, it just means before 1900 */

	if (timeptr->tm_mon >= MON_PER_YEAR) {
		timeptr->tm_year += timeptr->tm_mon / MON_PER_YEAR;
		timeptr->tm_mon %= MON_PER_YEAR;
	}

	timeptr->tm_yday = 0;
	for (i = 1; i < timeptr->tm_mon; i++) {
		timeptr->tm_yday += DAYS_IN(timeptr->tm_year, timeptr->tm_mon);
	}
	timeptr->tm_yday += timeptr->tm_mday;

	/* TODO: timeptr->tm_wday */
	/* TODO: timeptr->tm_isdst */

	return ((timeptr->tm_year - EPOCH_YEAR) * SEC_PER_YEAR)
		+ (timeptr->tm_yday * SEC_PER_DAY)
		+ (timeptr->tm_hour * SEC_PER_HR)
		+ (timeptr->tm_min * SEC_PER_MIN)
		+ timeptr->tm_sec;
}

/***
converts the local time pointed to by ARGUMENT(timeptr) to
an arithmetic value of type TYPEDEF(time_t). It also normalizes the values in
the STRUCTDEF(tm) at ARGUMENT(timeptr) to fit the defined limits, as well as
correcting the members MEMBER(tm_wday) and MEMBER(tm_yday) if they were
previously incorrect.
***/

/*
RETURN_FAILURE(CAST(TYPEDEF(time_t), -1))
RETURN_SUCCESS(the converted time)
STDC(1)
*/