Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Side by Side Diff: runtime/vm/os_win.cc

Issue 2069783002: Fix Windows time zone name extraction. Update API docs (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Take daylight savings from call return. Restore previous failure return. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/globals.h" 5 #include "vm/globals.h"
6 #if defined(TARGET_OS_WINDOWS) 6 #if defined(TARGET_OS_WINDOWS)
7 7
8 #include "vm/os.h" 8 #include "vm/os.h"
9 9
10 #include <malloc.h> // NOLINT 10 #include <malloc.h> // NOLINT
(...skipping 16 matching lines...) Expand all
27 27
28 28
29 intptr_t OS::ProcessId() { 29 intptr_t OS::ProcessId() {
30 return static_cast<intptr_t>(GetCurrentProcessId()); 30 return static_cast<intptr_t>(GetCurrentProcessId());
31 } 31 }
32 32
33 33
34 // As a side-effect sets the globals _timezone, _daylight and _tzname. 34 // As a side-effect sets the globals _timezone, _daylight and _tzname.
35 static bool LocalTime(int64_t seconds_since_epoch, tm* tm_result) { 35 static bool LocalTime(int64_t seconds_since_epoch, tm* tm_result) {
36 time_t seconds = static_cast<time_t>(seconds_since_epoch); 36 time_t seconds = static_cast<time_t>(seconds_since_epoch);
37 if (seconds != seconds_since_epoch) return false; 37 if (seconds != seconds_since_epoch) {
38 return false;
39 }
38 // localtime_s implicitly sets _timezone, _daylight and _tzname. 40 // localtime_s implicitly sets _timezone, _daylight and _tzname.
39 errno_t error_code = localtime_s(tm_result, &seconds); 41 errno_t error_code = localtime_s(tm_result, &seconds);
40 return error_code == 0; 42 return error_code == 0;
41 } 43 }
42 44
43 45
44 static int GetDaylightSavingBiasInSeconds() { 46 static int GetDaylightSavingBiasInSeconds() {
45 TIME_ZONE_INFORMATION zone_information; 47 TIME_ZONE_INFORMATION zone_information;
46 memset(&zone_information, 0, sizeof(zone_information)); 48 memset(&zone_information, 0, sizeof(zone_information));
47 if (GetTimeZoneInformation(&zone_information) == TIME_ZONE_ID_INVALID) { 49 if (GetTimeZoneInformation(&zone_information) == TIME_ZONE_ID_INVALID) {
48 // By default the daylight saving offset is an hour. 50 // By default the daylight saving offset is an hour.
49 return -60 * 60; 51 return -60 * 60;
50 } else { 52 } else {
51 return static_cast<int>(zone_information.DaylightBias * 60); 53 return static_cast<int>(zone_information.DaylightBias * 60);
52 } 54 }
53 } 55 }
54 56
55 57
56 const char* OS::GetTimeZoneName(int64_t seconds_since_epoch) { 58 const char* OS::GetTimeZoneName(int64_t seconds_since_epoch) {
57 tm decomposed; 59 TIME_ZONE_INFORMATION zone_information;
58 // LocalTime will set _tzname. 60 memset(&zone_information, 0, sizeof(zone_information));
59 bool succeeded = LocalTime(seconds_since_epoch, &decomposed); 61
60 if (succeeded) { 62 // Initialize and grab the time zone data.
61 int inDaylightSavingsTime = decomposed.tm_isdst; 63 _tzset();
62 ASSERT(inDaylightSavingsTime == 0 || inDaylightSavingsTime == 1); 64 DWORD status = GetTimeZoneInformation(&zone_information);
63 return _tzname[inDaylightSavingsTime]; 65 if (GetTimeZoneInformation(&zone_information) == TIME_ZONE_ID_INVALID) {
64 } else { 66 // If we can't get the time zone data, the Windows docs indicate that we
65 // Return an empty string like V8 does. 67 // are probably out of memory. Return an empty string.
66 return ""; 68 return "";
67 } 69 }
70
71 // Figure out whether we're in standard or daylight.
72 bool daylight_savings = (status == TIME_ZONE_ID_DAYLIGHT);
73 if (status == TIME_ZONE_ID_UNKNOWN) {
74 tm local_time;
75 if (LocalTime(seconds_since_epoch, &local_time)) {
76 daylight_savings = (local_time.tm_isdst == 1);
77 }
78 }
79
80 // Convert the wchar string to a null-terminated utf8 string.
81 wchar_t* wchar_name = daylight_savings
82 ? zone_information.DaylightName
83 : zone_information.StandardName;
84 intptr_t utf8_len = WideCharToMultiByte(
85 CP_UTF8, 0, wchar_name, -1, NULL, 0, NULL, NULL);
86 char* name = Thread::Current()->zone()->Alloc<char>(utf8_len + 1);
87 WideCharToMultiByte(
88 CP_UTF8, 0, wchar_name, -1, name, utf8_len, NULL, NULL);
89 name[utf8_len] = '\0';
90 return name;
68 } 91 }
69 92
70 93
71 int OS::GetTimeZoneOffsetInSeconds(int64_t seconds_since_epoch) { 94 int OS::GetTimeZoneOffsetInSeconds(int64_t seconds_since_epoch) {
72 tm decomposed; 95 tm decomposed;
73 // LocalTime will set _timezone. 96 // LocalTime will set _timezone.
74 bool succeeded = LocalTime(seconds_since_epoch, &decomposed); 97 bool succeeded = LocalTime(seconds_since_epoch, &decomposed);
75 if (succeeded) { 98 if (succeeded) {
76 int inDaylightSavingsTime = decomposed.tm_isdst; 99 int inDaylightSavingsTime = decomposed.tm_isdst;
77 ASSERT(inDaylightSavingsTime == 0 || inDaylightSavingsTime == 1); 100 ASSERT(inDaylightSavingsTime == 0 || inDaylightSavingsTime == 1);
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 // TODO(zra): Remove once VM shuts down cleanly. 447 // TODO(zra): Remove once VM shuts down cleanly.
425 private_flag_windows_run_tls_destructors = false; 448 private_flag_windows_run_tls_destructors = false;
426 // On Windows we use ExitProcess so that threads can't clobber the exit_code. 449 // On Windows we use ExitProcess so that threads can't clobber the exit_code.
427 // See: https://code.google.com/p/nativeclient/issues/detail?id=2870 450 // See: https://code.google.com/p/nativeclient/issues/detail?id=2870
428 ::ExitProcess(code); 451 ::ExitProcess(code);
429 } 452 }
430 453
431 } // namespace dart 454 } // namespace dart
432 455
433 #endif // defined(TARGET_OS_WINDOWS) 456 #endif // defined(TARGET_OS_WINDOWS)
OLDNEW
« no previous file with comments | « runtime/vm/os.h ('k') | sdk/lib/core/date_time.dart » ('j') | sdk/lib/core/date_time.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698