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