blob: 0f28359ab32ba70598dcdd1bed0c32b872110c7e [file] [log] [blame]
Benny Prijono9cf138e2006-01-19 03:58:29 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono9cf138e2006-01-19 03:58:29 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include <pj/os.h>
21#include <pj/string.h>
22#include <windows.h>
23
24///////////////////////////////////////////////////////////////////////////////
25
26#define SECS_TO_FT_MULT 10000000
27
28static LARGE_INTEGER base_time;
29
30// Find 1st Jan 1970 as a FILETIME
31static void get_base_time(void)
32{
33 SYSTEMTIME st;
34 FILETIME ft;
35
36 memset(&st,0,sizeof(st));
37 st.wYear=1970;
38 st.wMonth=1;
39 st.wDay=1;
40 SystemTimeToFileTime(&st, &ft);
41
42 base_time.LowPart = ft.dwLowDateTime;
43 base_time.HighPart = ft.dwHighDateTime;
44 base_time.QuadPart /= SECS_TO_FT_MULT;
45}
46
47PJ_DEF(pj_status_t) pj_gettimeofday(pj_time_val *tv)
48{
49 SYSTEMTIME st;
50 FILETIME ft;
51 LARGE_INTEGER li;
52
53 if (base_time.QuadPart == 0)
54 get_base_time();
55
56 GetLocalTime(&st);
57 SystemTimeToFileTime(&st, &ft);
58
59 li.LowPart = ft.dwLowDateTime;
60 li.HighPart = ft.dwHighDateTime;
61 li.QuadPart /= SECS_TO_FT_MULT;
62 li.QuadPart -= base_time.QuadPart;
63
64 tv->sec = li.LowPart;
65 tv->msec = st.wMilliseconds;
66
67 return PJ_SUCCESS;
68}
69
70PJ_DEF(pj_status_t) pj_time_decode(const pj_time_val *tv, pj_parsed_time *pt)
71{
72 LARGE_INTEGER li;
73 FILETIME ft;
74 SYSTEMTIME st;
75
76 li.QuadPart = tv->sec;
77 li.QuadPart += base_time.QuadPart;
78 li.QuadPart *= SECS_TO_FT_MULT;
79
80 ft.dwLowDateTime = li.LowPart;
81 ft.dwHighDateTime = li.HighPart;
82 FileTimeToSystemTime(&ft, &st);
83
84 pt->year = st.wYear;
85 pt->mon = st.wMonth-1;
86 pt->day = st.wDay;
87 pt->wday = st.wDayOfWeek;
88
89 pt->hour = st.wHour;
90 pt->min = st.wMinute;
91 pt->sec = st.wSecond;
92 pt->msec = tv->msec;
93
94 return PJ_SUCCESS;
95}
96
97/**
98 * Encode parsed time to time value.
99 */
100PJ_DEF(pj_status_t) pj_time_encode(const pj_parsed_time *pt, pj_time_val *tv)
101{
102 SYSTEMTIME st;
103 FILETIME ft;
104 LARGE_INTEGER li;
105
Benny Prijonoac623b32006-07-03 15:19:31 +0000106 pj_bzero(&st, sizeof(st));
Benny Prijonoedb566c2006-03-09 10:21:30 +0000107 st.wYear = (pj_uint16_t) pt->year;
108 st.wMonth = (pj_uint16_t) (pt->mon + 1);
109 st.wDay = (pj_uint16_t) pt->day;
110 st.wHour = (pj_uint16_t) pt->hour;
111 st.wMinute = (pj_uint16_t) pt->min;
112 st.wSecond = (pj_uint16_t) pt->sec;
113 st.wMilliseconds = (pj_uint16_t) pt->msec;
Benny Prijono9cf138e2006-01-19 03:58:29 +0000114
115 SystemTimeToFileTime(&st, &ft);
116
117 li.LowPart = ft.dwLowDateTime;
118 li.HighPart = ft.dwHighDateTime;
119 li.QuadPart /= SECS_TO_FT_MULT;
120 li.QuadPart -= base_time.QuadPart;
121
122 tv->sec = li.LowPart;
123 tv->msec = st.wMilliseconds;
124
125 return PJ_SUCCESS;
126}
127
128/**
129 * Convert local time to GMT.
130 */
131PJ_DEF(pj_status_t) pj_time_local_to_gmt(pj_time_val *tv);
132
133/**
134 * Convert GMT to local time.
135 */
136PJ_DEF(pj_status_t) pj_time_gmt_to_local(pj_time_val *tv);
137
138