xref: /haiku/src/system/libroot/os/time.cpp (revision 03187b607b2b5eec7ee059f1ead09bdba14991fb)
1 /*
2  * Copyright 2002-2009, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <errno.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <syslog.h>
11 
12 #include <FindDirectory.h>
13 #include <OS.h>
14 
15 #include <commpage_defs.h>
16 #include <libroot_private.h>
17 #include <real_time_data.h>
18 #include <syscalls.h>
19 
20 
21 static struct real_time_data* sRealTimeData;
22 
23 
24 void
25 __init_time(void)
26 {
27 	sRealTimeData = (struct real_time_data*)
28 		USER_COMMPAGE_TABLE[COMMPAGE_ENTRY_REAL_TIME_DATA];
29 
30 	__arch_init_time(sRealTimeData, false);
31 }
32 
33 
34 //	#pragma mark - public API
35 
36 
37 uint32
38 real_time_clock(void)
39 {
40 	return (__arch_get_system_time_offset(sRealTimeData) + system_time())
41 		/ 1000000;
42 }
43 
44 
45 bigtime_t
46 real_time_clock_usecs(void)
47 {
48 	return __arch_get_system_time_offset(sRealTimeData) + system_time();
49 }
50 
51 
52 void
53 set_real_time_clock(uint32 secs)
54 {
55 	_kern_set_real_time_clock(secs);
56 }
57 
58 
59 status_t
60 set_timezone(const char* timezone)
61 {
62 	char path[B_PATH_NAME_LENGTH];
63 	status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, -1, true, path,
64 		B_PATH_NAME_LENGTH);
65 	if (status != B_OK) {
66 		syslog(LOG_ERR, "can't find settings directory: %s\n", strerror(status));
67 		return status;
68 	}
69 
70 	strlcat(path, "/timezone", sizeof(path));
71 
72 	if (unlink(path) != 0 && errno != ENOENT) {
73 		syslog(LOG_ERR, "can't unlink: %s\n", strerror(errno));
74 		return errno;
75 	}
76 
77 	if (symlink(timezone, path) != 0) {
78 		syslog(LOG_ERR, "can't symlink: %s\n", strerror(errno));
79 		return errno;
80 	}
81 
82 	bool isGMT;
83 	_kern_get_tzfilename(NULL, 0, &isGMT);
84 	_kern_set_tzfilename(timezone, strlen(timezone), isGMT);
85 
86 	tzset();
87 
88 	time_t seconds;
89 	time(&seconds);
90 	struct tm tm;
91 	localtime_r(&seconds, &tm);
92 
93 	return _kern_set_timezone(tm.tm_gmtoff, tm.tm_isdst);
94 }
95 
96 
97 bigtime_t
98 set_alarm(bigtime_t when, uint32 mode)
99 {
100 	return _kern_set_alarm(when, mode);
101 }
102