1/*
2 * Copyright 2015, Axel D��rfler, axeld@pinc-software.de.
3 * Copyright 2004, J��r��me Duval, jerome.duval@free.fr.
4 * Copyright 2010, 2012, Oliver Tappe <zooey@hirschkaefer.de>
5 * Distributed under the terms of the MIT License.
6 */
7
8
9//! Initialize real time clock, and time zone offset
10
11
12#include "InitRealTimeClockJob.h"
13
14#include <stdio.h>
15
16#include <File.h>
17#include <FindDirectory.h>
18#include <Message.h>
19#include <TimeZone.h>
20
21#include <syscalls.h>
22
23
24using BSupportKit::BJob;
25
26
27InitRealTimeClockJob::InitRealTimeClockJob()
28	:
29	BJob("init real time clock")
30{
31}
32
33
34status_t
35InitRealTimeClockJob::Execute()
36{
37	BPath path;
38	status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
39	if (status == B_OK) {
40		_SetRealTimeClockIsGMT(path);
41		_SetTimeZoneOffset(path);
42	}
43	return status;
44}
45
46
47void
48InitRealTimeClockJob::_SetRealTimeClockIsGMT(BPath path) const
49{
50	path.Append("RTC_time_settings");
51	BFile file;
52	status_t status = file.SetTo(path.Path(), B_READ_ONLY);
53	if (status != B_OK) {
54		fprintf(stderr, "Can't open RTC settings file\n");
55		return;
56	}
57
58	char buffer[10];
59	ssize_t bytesRead = file.Read(buffer, sizeof(buffer));
60	if (bytesRead < 0) {
61		fprintf(stderr, "Unable to read RTC settings file\n");
62		return;
63	}
64	bool isGMT = strncmp(buffer, "local", 5) != 0;
65
66	_kern_set_real_time_clock_is_gmt(isGMT);
67	printf("RTC stores %s time.\n", isGMT ? "GMT" : "local" );
68}
69
70
71void
72InitRealTimeClockJob::_SetTimeZoneOffset(BPath path) const
73{
74	path.Append("Time settings");
75	BFile file;
76	status_t status = file.SetTo(path.Path(), B_READ_ONLY);
77	if (status != B_OK) {
78		fprintf(stderr, "Can't open Time settings file\n");
79		return;
80	}
81
82	BMessage settings;
83	status = settings.Unflatten(&file);
84	if (status != B_OK) {
85		fprintf(stderr, "Unable to parse Time settings file\n");
86		return;
87	}
88	BString timeZoneID;
89	if (settings.FindString("timezone", &timeZoneID) != B_OK) {
90		fprintf(stderr, "No timezone found\n");
91		return;
92	}
93	int32 timeZoneOffset = BTimeZone(timeZoneID.String()).OffsetFromGMT();
94
95	_kern_set_timezone(timeZoneOffset, timeZoneID.String(),
96		timeZoneID.Length());
97	printf("timezone is %s, offset is %" B_PRId32 " seconds from GMT.\n",
98		timeZoneID.String(), timeZoneOffset);
99}
100