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