From 26ea12a873d0e7c1467ee6b52c9559dc5f456bd3 Mon Sep 17 00:00:00 2001 From: est31 Date: Sat, 6 Jun 2015 03:23:34 +0200 Subject: Use local time for both time and date on win On unix and nacl, both date and time are expressed in local time. --- platform/windows/os_windows.cpp | 2 +- platform/winrt/os_winrt.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'platform') diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index 4e8f9fcd9b..4bf4fea845 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -1832,7 +1832,7 @@ String OS_Windows::get_name() { OS::Date OS_Windows::get_date() const { SYSTEMTIME systemtime; - GetSystemTime(&systemtime); + GetLocalTime(&systemtime); Date date; date.day=systemtime.wDay; date.month=Month(systemtime.wMonth); diff --git a/platform/winrt/os_winrt.cpp b/platform/winrt/os_winrt.cpp index 21a77b89cb..74909c6dd7 100644 --- a/platform/winrt/os_winrt.cpp +++ b/platform/winrt/os_winrt.cpp @@ -445,7 +445,7 @@ String OSWinrt::get_name() { OS::Date OSWinrt::get_date() const { SYSTEMTIME systemtime; - GetSystemTime(&systemtime); + GetLocalTime(&systemtime); Date date; date.day=systemtime.wDay; date.month=Month(systemtime.wMonth); @@ -457,7 +457,7 @@ OS::Date OSWinrt::get_date() const { OS::Time OSWinrt::get_time() const { SYSTEMTIME systemtime; - GetSystemTime(&systemtime); + GetLocalTime(&systemtime); Time time; time.hour=systemtime.wHour; -- cgit v1.2.3 From 803069886ebca492c0d5f47133ccf7833c716e5a Mon Sep 17 00:00:00 2001 From: est31 Date: Sat, 6 Jun 2015 03:40:56 +0200 Subject: Add utc param to get_time and get_date methods If utc == false, we return the local time, like before. Otherwise, we return UTC time. utc defaults to false to not break behaviour. --- platform/nacl/os_nacl.cpp | 16 ++++++++++++---- platform/nacl/os_nacl.h | 4 ++-- platform/windows/os_windows.cpp | 14 ++++++++++---- platform/windows/os_windows.h | 4 ++-- platform/winrt/os_winrt.cpp | 14 ++++++++++---- platform/winrt/os_winrt.h | 4 ++-- 6 files changed, 38 insertions(+), 18 deletions(-) (limited to 'platform') diff --git a/platform/nacl/os_nacl.cpp b/platform/nacl/os_nacl.cpp index ffa9915256..ecace58801 100644 --- a/platform/nacl/os_nacl.cpp +++ b/platform/nacl/os_nacl.cpp @@ -240,10 +240,14 @@ MainLoop *OSNacl::get_main_loop() const { return main_loop; }; -OS::Date OSNacl::get_date() const { +OS::Date OSNacl::get_date(bool utc) const { time_t t=time(NULL); - struct tm *lt=localtime(&t); + struct tm *lt; + if (utc) + lt=gmtime(&t); + else + lt=localtime(&t); Date ret; ret.year=lt->tm_year; ret.month=(Month)lt->tm_mon; @@ -254,10 +258,14 @@ OS::Date OSNacl::get_date() const { return ret; }; -OS::Time OSNacl::get_time() const { +OS::Time OSNacl::get_time(bool utc) const { time_t t=time(NULL); - struct tm *lt=localtime(&t); + struct tm *lt; + if (utc) + lt=gmtime(&t); + else + lt=localtime(&t); Time ret; ret.hour=lt->tm_hour; ret.min=lt->tm_min; diff --git a/platform/nacl/os_nacl.h b/platform/nacl/os_nacl.h index 1150b12edd..e033abb68d 100644 --- a/platform/nacl/os_nacl.h +++ b/platform/nacl/os_nacl.h @@ -137,8 +137,8 @@ public: virtual MainLoop *get_main_loop() const; - virtual Date get_date() const; - virtual Time get_time() const; + virtual Date get_date(bool utc) const; + virtual Time get_time(bool utc) const; virtual void delay_usec(uint32_t p_usec) const; virtual uint64_t get_ticks_usec() const; diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index 4bf4fea845..16b9e9b327 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -1832,7 +1832,10 @@ String OS_Windows::get_name() { OS::Date OS_Windows::get_date() const { SYSTEMTIME systemtime; - GetLocalTime(&systemtime); + if (local) + GetSystemTime(&systemtime); + else + GetLocalTime(&systemtime); Date date; date.day=systemtime.wDay; date.month=Month(systemtime.wMonth); @@ -1841,10 +1844,13 @@ OS::Date OS_Windows::get_date() const { date.dst=false; return date; } -OS::Time OS_Windows::get_time() const { +OS::Time OS_Windows::get_time(bool utc) const { SYSTEMTIME systemtime; - GetLocalTime(&systemtime); + if (utc) + GetSystemTime(&systemtime); + else + GetLocalTime(&systemtime); Time time; time.hour=systemtime.wHour; @@ -1853,7 +1859,7 @@ OS::Time OS_Windows::get_time() const { return time; } -uint64_t OS_Windows::get_unix_time() const { +uint64_t OS_Windows::get_unix_time(bool local) const { FILETIME ft; SYSTEMTIME st; diff --git a/platform/windows/os_windows.h b/platform/windows/os_windows.h index 64fbbf23c0..6b2eb19a76 100644 --- a/platform/windows/os_windows.h +++ b/platform/windows/os_windows.h @@ -259,8 +259,8 @@ public: virtual String get_name(); - virtual Date get_date() const; - virtual Time get_time() const; + virtual Date get_date(bool utc) const; + virtual Time get_time(bool utc) const; virtual uint64_t get_unix_time() const; virtual bool can_draw() const; diff --git a/platform/winrt/os_winrt.cpp b/platform/winrt/os_winrt.cpp index 74909c6dd7..a84e54ee0e 100644 --- a/platform/winrt/os_winrt.cpp +++ b/platform/winrt/os_winrt.cpp @@ -454,10 +454,13 @@ OS::Date OSWinrt::get_date() const { date.dst=false; return date; } -OS::Time OSWinrt::get_time() const { +OS::Time OSWinrt::get_time(bool utc) const { SYSTEMTIME systemtime; - GetLocalTime(&systemtime); + if (utc) + GetSystemTime(&systemtime); + else + GetLocalTime(&systemtime); Time time; time.hour=systemtime.wHour; @@ -466,11 +469,14 @@ OS::Time OSWinrt::get_time() const { return time; } -uint64_t OSWinrt::get_unix_time() const { +uint64_t OSWinrt::get_unix_time(bool utc) const { FILETIME ft; SYSTEMTIME st; - GetSystemTime(&st); + if (utc) + GetSystemTime(&systemtime); + else + GetLocalTime(&systemtime); SystemTimeToFileTime(&st, &ft); SYSTEMTIME ep; diff --git a/platform/winrt/os_winrt.h b/platform/winrt/os_winrt.h index 68236309a9..af44bd338e 100644 --- a/platform/winrt/os_winrt.h +++ b/platform/winrt/os_winrt.h @@ -198,8 +198,8 @@ public: virtual String get_name(); - virtual Date get_date() const; - virtual Time get_time() const; + virtual Date get_date(bool utc) const; + virtual Time get_time(bool utc) const; virtual uint64_t get_unix_time() const; virtual bool can_draw() const; -- cgit v1.2.3 From c5338fd6c40d08472b680809cfa04d47826bdcf5 Mon Sep 17 00:00:00 2001 From: est31 Date: Sat, 6 Jun 2015 05:35:38 +0200 Subject: Add OS.get_time_zone_info function The returned dictionary maps "name" to the name of the current time zone, and "bias" to a bias from UTC in minutes. --- platform/nacl/os_nacl.cpp | 26 ++++++++++++++++++++++++++ platform/windows/os_windows.cpp | 30 ++++++++++++++++++++++++++++++ platform/winrt/os_winrt.cpp | 16 ++++++++++++++++ 3 files changed, 72 insertions(+) (limited to 'platform') diff --git a/platform/nacl/os_nacl.cpp b/platform/nacl/os_nacl.cpp index ecace58801..b282209d72 100644 --- a/platform/nacl/os_nacl.cpp +++ b/platform/nacl/os_nacl.cpp @@ -273,6 +273,32 @@ OS::Time OSNacl::get_time(bool utc) const { return ret; }; +OS::TimeZoneInfo OS_Unix::get_time_zone_info() const { + time_t t = time(NULL); + struct tm *lt = localtime(&t); + char name[16]; + strftime(name, 16, "%Z", lt); + name[15] = 0; + TimeZoneInfo ret; + ret.name = name; + + char bias_buf[16]; + strftime(bias_buf, 16, "%z", lt); + int bias; + bias_buf[15] = 0; + sscanf(bias_buf, "%d", &bias); + + // convert from ISO 8601 (1 minute=1, 1 hour=100) to minutes + int hour = (int)bias / 100; + int minutes = bias % 100; + if (bias < 0) + ret.bias = hour * 60 - minutes; + else + ret.bias = hour * 60 + minutes; + + return ret; +}; + void OSNacl::delay_usec(uint32_t p_usec) const { //usleep(p_usec); diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index 16b9e9b327..cdf9b007d1 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -1858,6 +1858,36 @@ OS::Time OS_Windows::get_time(bool utc) const { time.sec=systemtime.wSecond; return time; } +OS::Time OS_Windows::get_time(bool utc) const { + + SYSTEMTIME systemtime; + if (utc) + GetSystemTime(&systemtime); + else + GetLocalTime(&systemtime); + + Time time; + time.hour=systemtime.wHour; + time.min=systemtime.wMinute; + time.sec=systemtime.wSecond; + return time; +} + +OS::TimeZoneInfo OS_Windows::get_time_zone_info() const { + TIME_ZONE_INFORMATION info; + bool daylight = false; + if (GetTimeZoneInformation(info) == TIME_ZONE_ID_DAYLIGHT) + daylight = true; + + if (daylight) { + ret.name = info.DaylightName; + } else { + ret.name = info.StandardName; + } + + ret.bias = info.Bias; + return ret; +} uint64_t OS_Windows::get_unix_time(bool local) const { diff --git a/platform/winrt/os_winrt.cpp b/platform/winrt/os_winrt.cpp index a84e54ee0e..24037f05d7 100644 --- a/platform/winrt/os_winrt.cpp +++ b/platform/winrt/os_winrt.cpp @@ -469,6 +469,22 @@ OS::Time OSWinrt::get_time(bool utc) const { return time; } +OS::TimeZoneInfo OS_Windows::get_time_zone_info() const { + TIME_ZONE_INFORMATION info; + bool daylight = false; + if (GetTimeZoneInformation(info) == TIME_ZONE_ID_DAYLIGHT) + daylight = true; + + if (daylight) { + ret.name = info.DaylightName; + } else { + ret.name = info.StandardName; + } + + ret.bias = info.Bias; + return ret; +} + uint64_t OSWinrt::get_unix_time(bool utc) const { FILETIME ft; -- cgit v1.2.3