1 krisbash 1.1 /*
2 **==============================================================================
3 **
4 ** Open Management Infrastructure (OMI)
5 **
6 ** Copyright (c) Microsoft Corporation
7 **
8 ** Licensed under the Apache License, Version 2.0 (the "License"); you may not
9 ** use this file except in compliance with the License. You may obtain a copy
10 ** of the License at
11 **
12 ** http://www.apache.org/licenses/LICENSE-2.0
13 **
14 ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 ** MERCHANTABLITY OR NON-INFRINGEMENT.
18 **
19 ** See the Apache 2 License for the specific language governing permissions
20 ** and limitations under the License.
21 **
22 krisbash 1.1 **==============================================================================
23 */
24
25 #include "cpu.h"
26
27 _Success_(return == 0)
28 int CPU_GetLocalTimestamp(
29 _Out_ PAL_Datetime* current)
30 {
31 #ifdef _MSC_VER
32 SYSTEMTIME st;
33 TIME_ZONE_INFORMATION zone;
34 DWORD tz;
35
36 GetLocalTime(&st);
37 tz = GetTimeZoneInformation(&zone);
38 if (tz == TIME_ZONE_ID_INVALID)
39 return -1;
40
41 current->isTimestamp = PAL_TRUE;
42 /* TODO: Valid year ranges for SYSTEMTIME and MI_Timestamp are different! */
43 krisbash 1.1 // this is probably not a big problem for actual time.The range of SystemTime.wYear (1601-30827) is not a subset of
44 // MI_Timestamp.year (0-9999 according to DMTF + verified by asserts in Mint + assumed in various integer overflow
45 // considerations for arithmetic/comparison built-ins)
46 current->u.timestamp.year = st.wYear;
47 current->u.timestamp.month = st.wMonth;
48 current->u.timestamp.day = st.wDay;
49 current->u.timestamp.hour = st.wHour;
50 current->u.timestamp.minute = st.wMinute;
51 current->u.timestamp.second = st.wSecond;
52 current->u.timestamp.microseconds = st.wMilliseconds * 1000;
53 current->u.timestamp.utc = -zone.Bias;
54
55 if (tz == TIME_ZONE_ID_DAYLIGHT)
56 current->u.timestamp.utc -= zone.DaylightBias;
57 else
58 current->u.timestamp.utc -= zone.StandardBias;
59
60 return 0;
61 #else
62 struct timeval tv;
63 struct tm tm;
64 krisbash 1.1
65 gettimeofday(&tv, NULL);
66 localtime_r(&tv.tv_sec, &tm);
67
68 current->isTimestamp = PAL_TRUE;
69 current->u.timestamp.year = 1900 + tm.tm_year; // tm.tm_year is the number of years since 1900.
70 current->u.timestamp.month = tm.tm_mon + 1; // tm.tm_mon is the number of months since January, in the range 0 to 11.
71 current->u.timestamp.day = tm.tm_mday;
72 current->u.timestamp.hour = tm.tm_hour;
73 current->u.timestamp.minute = tm.tm_min;
74 current->u.timestamp.second = tm.tm_sec;
75 current->u.timestamp.microseconds = tv.tv_usec;
76 current->u.timestamp.utc = -timezone / 60;
77 return 0;
78 #endif
79 }
80
81 _Success_(return == 0)
82 int CPU_GetUtcTimestamp(
83 _Out_ PAL_Datetime* current)
84 {
85 krisbash 1.1 #ifdef _MSC_VER
86 SYSTEMTIME st;
87
88 GetSystemTime(&st);
89
90 current->isTimestamp = PAL_TRUE;
91 /* TODO: Valid year ranges for SYSTEMTIME and MI_Timestamp are different! */
92 // this is probably not a big problem for actual time.The range of SystemTime.wYear (1601-30827) is not a subset of
93 // MI_Timestamp.year (0-9999 according to DMTF + verified by asserts in Mint + assumed in various integer overflow
94 // considerations for arithmetic/comparison built-ins)
95 current->u.timestamp.year = st.wYear;
96 current->u.timestamp.month = st.wMonth;
97 current->u.timestamp.day = st.wDay;
98 current->u.timestamp.hour = st.wHour;
99 current->u.timestamp.minute = st.wMinute;
100 current->u.timestamp.second = st.wSecond;
101 current->u.timestamp.microseconds = st.wMilliseconds * 1000;
102 current->u.timestamp.utc = 0;
103 return 0;
104 #else
105 struct timeval tv;
106 krisbash 1.1 struct tm tm;
107
108 gettimeofday(&tv, NULL);
109 gmtime_r(&tv.tv_sec, &tm);
110
111 current->isTimestamp = PAL_TRUE;
112 current->u.timestamp.year = tm.tm_year;
113 current->u.timestamp.month = tm.tm_mon + 1; // tm.tm_mon is the number of months since January, in the range 0 to 11.
114 current->u.timestamp.day = tm.tm_mday;
115 current->u.timestamp.hour = tm.tm_hour;
116 current->u.timestamp.minute = tm.tm_min;
117 current->u.timestamp.second = tm.tm_sec;
118 current->u.timestamp.microseconds = tv.tv_usec;
119 current->u.timestamp.utc = 0;
120 return 0;
121 #endif
122 }
123
|