aboutsummaryrefslogtreecommitdiffstats
path: root/localtime.c
blob: 96bd1e1458a796746edc25c510d18fed00705b82 (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
/* See LICENSE file for copyright and license details. */
#include "common.h"
#include <sys/timex.h>
#ifndef TEST


int
libsimple_localtime(struct tm *tm, struct timespec *ts)
{
	struct timex timex;
	int r;

	memset(&timex, 0, sizeof(timex));
	timex.status = ADJ_NANO;
	r = adjtimex(&timex);
	if (r == -1)
		return -1;
	if (ts) {
		ts->tv_sec = timex.time.tv_sec;
		ts->tv_nsec = timex.time.tv_usec;
	}

	if (timex.time.tv_sec % (24 * 60 * 60) == 0) {
		if (r == TIME_INS) {
			timex.time.tv_sec -= 1;
			if (!localtime_r(&timex.time.tv_sec, tm))
				return -1;
			tm->tm_sec += 1;
			return 1;
		} else if (r == TIME_DEL) {
			timex.time.tv_sec += 1;
			if (!localtime_r(&timex.time.tv_sec, tm))
				return -1;
		} else {
			if (!localtime_r(&timex.time.tv_sec, tm))
				return -1;
		}
	} else if (r == TIME_OOP) {
		if (!localtime_r(&timex.time.tv_sec, tm))
			return -1;
		tm->tm_sec += 1;
	} else {
		if (!localtime_r(&timex.time.tv_sec, tm))
			return -1;
	}

	return 0;
}


#else
#include "test.h"

int
main(void)
{
	/* TODO test */
	return 0;
}

#endif