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

Side by Side Diff: chrome/browser/sync/notifier/base/win32/time_win32.cc

Issue 194065: Initial commit of sync engine code to browser/sync.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Fixes to gtest include path, reverted syncapi. Created 11 years, 3 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 | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(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 // Time 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
62 // as it's granularity is 1 ms. Below is the correct implementation.
63 // On the other hand the system clock granularity is 15 ms, so we
64 // are not gaining much by having the timestamp in nano-sec
65 // If we decise to go with ms, divide "time64 time" by 10000
66
67 FILETIME file_time;
68 ::GetSystemTimeAsFileTime(&file_time);
69
70 time64 time = FileTimeToTime64(file_time);
71 return time;
72 }
73
74 time64 TmToTime64(const struct tm& tm) {
75 SYSTEMTIME sys_time;
76 TmTimeToSystemTime(tm, &sys_time);
77
78 FILETIME file_time;
79 SetZero(file_time);
80 if (!::SystemTimeToFileTime(&sys_time, &file_time)) {
81 return 0;
82 }
83
84 return FileTimeToTime64(file_time);
85 }
86
87 bool Time64ToTm(time64 t, struct tm* tm) {
88 ASSERT(tm);
89
90 FILETIME file_time;
91 SetZero(file_time);
92 Time64ToFileTime(t, &file_time);
93
94 SYSTEMTIME sys_time;
95 SetZero(sys_time);
96 if (!::FileTimeToSystemTime(&file_time, &sys_time)) {
97 return false;
98 }
99
100 SystemTimeToTmTime(sys_time, tm);
101
102 return true;
103 }
104
105 bool UtcTimeToLocalTime(struct tm* tm) {
106 ASSERT(tm);
107
108 SYSTEMTIME utc_time;
109 TmTimeToSystemTime(*tm, &utc_time);
110
111 TIME_ZONE_INFORMATION time_zone;
112 if (::GetTimeZoneInformation(&time_zone) == TIME_ZONE_ID_INVALID) {
113 return false;
114 }
115
116 SYSTEMTIME local_time;
117 if (!::SystemTimeToTzSpecificLocalTime(&time_zone, &utc_time, &local_time)) {
118 return false;
119 }
120
121 SystemTimeToTmTime(local_time, tm);
122
123 return true;
124 }
125
126 bool LocalTimeToUtcTime(struct tm* tm) {
127 ASSERT(tm);
128
129 SYSTEMTIME local_time;
130 TmTimeToSystemTime(*tm, &local_time);
131
132 // Get the bias, which when added to local, gives UTC
133 TIME_ZONE_INFORMATION time_zone;
134 if (::GetTimeZoneInformation(&time_zone) == TIME_ZONE_ID_INVALID) {
135 return false;
136 }
137
138 // By negating the biases, we can get translation from UTC to local
139 time_zone.Bias *= -1;
140 time_zone.DaylightBias *= -1;
141 time_zone.StandardBias *= -1; // this is 0 but negating for completness
142
143 // We'll tell SystemTimeToTzSpecificLocalTime that the local time is actually
144 // UTC. With the negated bias, the "local" time that the API returns will
145 // actually be UTC. Casting the const off because
146 // SystemTimeToTzSpecificLocalTime's definition requires it, although the
147 // value is not modified.
148 SYSTEMTIME utc_time;
149 if (!::SystemTimeToTzSpecificLocalTime(&time_zone, &local_time, &utc_time)) {
150 return false;
151 }
152
153 SystemTimeToTmTime(utc_time, tm);
154
155 return true;
156 }
157
158 } // namespace notifier
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698