1 /*
2 * $Id: wce_timesys.c,v 1.2 2006/04/09 16:48:18 mloskot Exp $
3 *
4 * Defines function to convert time between formats SYSTEMTIME,
5 * FILETIME and time_t value.
6 *
7 * Created by Mateusz Loskot (mateusz@loskot.net)
8 *
9 * Copyright (c) 2006 Taxus SI Ltd.
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining
12 * a copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom
16 * the Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
25 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
27 * THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 *
29 * MIT License:
30 * http://opensource.org/licenses/mit-license.php
31 *
32 * Contact:
33 * Taxus SI Ltd.
34 * http://www.taxussi.com.pl
35 *
36 */
37
38 #include <wce_time.h>
39 #include <wce_timesys.h>
40 #include <windows.h>
41
42 /*******************************************************************************
43 * wceex_filetime_to_time - Convert FILETIME to time as time_t value
44 *
45 * Description:
46 *
47 *
48 * Return:
49 *
50 * This function shall return the specified time since the Epoch
51 * encoded as a value of type time_t.
52 *
53 *******************************************************************************/
wceex_filetime_to_time(const FILETIME * pft)54 time_t wceex_filetime_to_time(const FILETIME * pft)
55 {
56 SYSTEMTIME st;
57 FILETIME lft;
58
59 /* File time as 0 value cannot be represented as Epoch time. */
60
61 if (!pft->dwLowDateTime && !pft->dwHighDateTime)
62 {
63 return (time_t)-1;
64 }
65
66 /* Convert to a broken down local time value */
67 if (!FileTimeToLocalFileTime(pft, &lft) ||
68 !FileTimeToSystemTime(&lft, &st))
69 {
70 return (time_t)-1;
71 }
72
73 return wceex_local_to_time(&st);
74 }
75
76
77 /*******************************************************************************
78 * wceex_local_to_time - Convert broken-down local time to value of type time_t
79 *
80 * Description:
81 *
82 *
83 * Return:
84 *
85 * This function shall return the specified time since the Epoch
86 * encoded as a value of type time_t.
87 *
88 *******************************************************************************/
wceex_local_to_time(const SYSTEMTIME * st)89 time_t wceex_local_to_time(const SYSTEMTIME *st)
90 {
91 if (st == NULL)
92 {
93 return (time_t)-1;
94 }
95
96 return wceex_local_to_time_r(st->wYear - TM_YEAR_BASE,
97 st->wMonth - 1,
98 st->wDay, st->wHour,
99 st->wMinute,
100 st->wSecond);
101 }
102
103 /*******************************************************************************
104 * wceex_local_to_time - Convert broken-down local time to value of type time_t
105 *
106 * Description:
107 *
108 * Date and time are given as a set of separate values.
109 * Parameters:
110 * - year is Epoch-based, year - 1900
111 * - mon is 0 based number of current month
112 * - day is 1 based number of current day
113 * - hour, min and sec represent current local time.
114 *
115 * Return:
116 *
117 * This function shall return the specified time since the Epoch
118 * encoded as a value of type time_t.
119 *
120 *******************************************************************************/
wceex_local_to_time_r(int year,int mon,int day,int hour,int min,int sec)121 time_t wceex_local_to_time_r(int year, int mon, int day, int hour, int min, int sec)
122 {
123 struct tm tmbuff = { 0 };
124
125 tmbuff.tm_year = year;
126 tmbuff.tm_mon = mon;
127 tmbuff.tm_mday = day;
128 tmbuff.tm_hour = hour;
129 tmbuff.tm_min = min;
130 tmbuff.tm_sec = sec;
131 tmbuff.tm_isdst = 0;
132 tmbuff.tm_wday = 0;
133 tmbuff.tm_yday = 0;
134
135 /* Convert tm struct to time_tUTC */
136 return wceex_mktime(&tmbuff);
137 }
138
139