| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 // | |
| 5 // Windows specific yime functions. | |
| 6 | |
| 7 #include <time.h> | |
| 8 #include <windows.h> | |
| 9 | |
| 10 #include "chrome/browser/sync/notifier/base/time.h" | |
| 11 | |
| 12 #include "chrome/browser/sync/notifier/base/utils.h" | |
| 13 #include "talk/base/common.h" | |
| 14 #include "talk/base/logging.h" | |
| 15 | |
| 16 namespace notifier { | |
| 17 | |
| 18 time64 FileTimeToTime64(const FILETIME& file_time) { | |
| 19 return static_cast<time64>(file_time.dwHighDateTime) << 32 | | |
| 20 file_time.dwLowDateTime; | |
| 21 } | |
| 22 | |
| 23 void Time64ToFileTime(const time64& time, FILETIME* ft) { | |
| 24 ASSERT(ft); | |
| 25 | |
| 26 ft->dwHighDateTime = static_cast<DWORD>(time >> 32); | |
| 27 ft->dwLowDateTime = static_cast<DWORD>(time & 0xffffffff); | |
| 28 } | |
| 29 | |
| 30 void TmTimeToSystemTime(const struct tm& tm, SYSTEMTIME* sys_time) { | |
| 31 ASSERT(sys_time); | |
| 32 | |
| 33 SetZero(*sys_time); | |
| 34 // tm's year is 1900 based, systemtime's year is absolute. | |
| 35 sys_time->wYear = tm.tm_year + 1900; | |
| 36 // tm's month is 0 based, but systemtime's month is 1 based. | |
| 37 sys_time->wMonth = tm.tm_mon + 1; | |
| 38 sys_time->wDay = tm.tm_mday; | |
| 39 sys_time->wDayOfWeek = tm.tm_wday; | |
| 40 sys_time->wHour = tm.tm_hour; | |
| 41 sys_time->wMinute = tm.tm_min; | |
| 42 sys_time->wSecond = tm.tm_sec; | |
| 43 } | |
| 44 | |
| 45 void SystemTimeToTmTime(const SYSTEMTIME& sys_time, struct tm* tm) { | |
| 46 ASSERT(tm); | |
| 47 | |
| 48 SetZero(*tm); | |
| 49 // tm's year is 1900 based, systemtime's year is absolute. | |
| 50 tm->tm_year = sys_time.wYear - 1900; | |
| 51 // tm's month is 0 based, but systemtime's month is 1 based. | |
| 52 tm->tm_mon = sys_time.wMonth - 1; | |
| 53 tm->tm_mday = sys_time.wDay; | |
| 54 tm->tm_wday = sys_time.wDayOfWeek; | |
| 55 tm->tm_hour = sys_time.wHour; | |
| 56 tm->tm_min = sys_time.wMinute; | |
| 57 tm->tm_sec = sys_time.wSecond; | |
| 58 } | |
| 59 | |
| 60 time64 GetCurrent100NSTime() { | |
| 61 // In order to get the 100ns time we shouldn't use SystemTime as it's | |
| 62 // granularity is 1 ms. Below is the correct implementation. On the other | |
| 63 // hand the system clock granularity is 15 ms, so we are not gaining much by | |
| 64 // having the timestamp in nano-sec. If we decide to go with ms, divide | |
| 65 // "time64 time" by 10000. | |
| 66 FILETIME file_time; | |
| 67 ::GetSystemTimeAsFileTime(&file_time); | |
| 68 | |
| 69 time64 time = FileTimeToTime64(file_time); | |
| 70 return time; | |
| 71 } | |
| 72 | |
| 73 time64 TmToTime64(const struct tm& tm) { | |
| 74 SYSTEMTIME sys_time; | |
| 75 TmTimeToSystemTime(tm, &sys_time); | |
| 76 | |
| 77 FILETIME file_time; | |
| 78 SetZero(file_time); | |
| 79 if (!::SystemTimeToFileTime(&sys_time, &file_time)) { | |
| 80 return 0; | |
| 81 } | |
| 82 | |
| 83 return FileTimeToTime64(file_time); | |
| 84 } | |
| 85 | |
| 86 bool Time64ToTm(time64 t, struct tm* tm) { | |
| 87 ASSERT(tm); | |
| 88 | |
| 89 FILETIME file_time; | |
| 90 SetZero(file_time); | |
| 91 Time64ToFileTime(t, &file_time); | |
| 92 | |
| 93 SYSTEMTIME sys_time; | |
| 94 SetZero(sys_time); | |
| 95 if (!::FileTimeToSystemTime(&file_time, &sys_time)) { | |
| 96 return false; | |
| 97 } | |
| 98 | |
| 99 SystemTimeToTmTime(sys_time, tm); | |
| 100 | |
| 101 return true; | |
| 102 } | |
| 103 | |
| 104 bool UtcTimeToLocalTime(struct tm* tm) { | |
| 105 ASSERT(tm); | |
| 106 | |
| 107 SYSTEMTIME utc_time; | |
| 108 TmTimeToSystemTime(*tm, &utc_time); | |
| 109 | |
| 110 TIME_ZONE_INFORMATION time_zone; | |
| 111 if (::GetTimeZoneInformation(&time_zone) == TIME_ZONE_ID_INVALID) { | |
| 112 return false; | |
| 113 } | |
| 114 | |
| 115 SYSTEMTIME local_time; | |
| 116 if (!::SystemTimeToTzSpecificLocalTime(&time_zone, &utc_time, &local_time)) { | |
| 117 return false; | |
| 118 } | |
| 119 | |
| 120 SystemTimeToTmTime(local_time, tm); | |
| 121 | |
| 122 return true; | |
| 123 } | |
| 124 | |
| 125 bool LocalTimeToUtcTime(struct tm* tm) { | |
| 126 ASSERT(tm); | |
| 127 | |
| 128 SYSTEMTIME local_time; | |
| 129 TmTimeToSystemTime(*tm, &local_time); | |
| 130 | |
| 131 // Get the bias, which when added to local, gives UTC. | |
| 132 TIME_ZONE_INFORMATION time_zone; | |
| 133 if (::GetTimeZoneInformation(&time_zone) == TIME_ZONE_ID_INVALID) { | |
| 134 return false; | |
| 135 } | |
| 136 | |
| 137 // By negating the biases, we can get translation from UTC to local. | |
| 138 time_zone.Bias *= -1; | |
| 139 time_zone.DaylightBias *= -1; | |
| 140 time_zone.StandardBias *= -1; // This is 0 but negating for completness. | |
| 141 | |
| 142 // We'll tell SystemTimeToTzSpecificLocalTime that the local time is actually | |
| 143 // UTC. With the negated bias, the "local" time that the API returns will | |
| 144 // actually be UTC. Casting the const off because | |
| 145 // SystemTimeToTzSpecificLocalTime's definition requires it, although the | |
| 146 // value is not modified. | |
| 147 SYSTEMTIME utc_time; | |
| 148 if (!::SystemTimeToTzSpecificLocalTime(&time_zone, &local_time, &utc_time)) { | |
| 149 return false; | |
| 150 } | |
| 151 | |
| 152 SystemTimeToTmTime(utc_time, tm); | |
| 153 | |
| 154 return true; | |
| 155 } | |
| 156 | |
| 157 } // namespace notifier | |
| OLD | NEW |