Debian Codegraph @

Xpjproject/pjlib/src/pj/os_time_common.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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/compat/time.h>
21 #include <pj/errno.h>
22
23
24 ///////////////////////////////////////////////////////////////////////////////
25
26 #if !defined(PJ_WIN32) || PJ_WIN32==0
27
28 PJ_DEF(pj_status_t) pj_time_decode(const pj_time_val *tv, pj_parsed_time *pt)
29 {
30 struct tm local_time;
31 time_t sec = tv->sec; // time_t might be 64 bit even when long int is 32 bit, (time_t*) dangerous
32
33 PJ_CHECK_STACK();
34
35 #if defined(PJ_HAS_LOCALTIME_R) && PJ_HAS_LOCALTIME_R != 0
36 localtime_r(&sec, &local_time);
37 #else
38 /* localtime() is NOT thread-safe. */
39 local_time = *localtime(&sec);
40 #endif
41
42 pt->year = local_time.tm_year+1900;
43 pt->mon = local_time.tm_mon;
44 pt->day = local_time.tm_mday;
45 pt->hour = local_time.tm_hour;
46 pt->min = local_time.tm_min;
47 pt->sec = local_time.tm_sec;
48 pt->wday = local_time.tm_wday;
49 pt->msec = tv->msec;
50
51 return PJ_SUCCESS;
52 }
53
54 /**
55 * Encode parsed time to time value.
56 */
57 PJ_DEF(pj_status_t) pj_time_encode(const pj_parsed_time *pt, pj_time_val *tv)
58 {
59 struct tm local_time;
60
61 local_time.tm_year = pt->year-1900;
62 local_time.tm_mon = pt->mon;
63 local_time.tm_mday = pt->day;
64 local_time.tm_hour = pt->hour;
65 local_time.tm_min = pt->min;
66 local_time.tm_sec = pt->sec;
67 local_time.tm_isdst = 0;
68
69 tv->sec = mktime(&local_time);
70 tv->msec = pt->msec;
71
72 return PJ_SUCCESS;
73 }
74
75 static int get_tz_offset_secs()
76 {
77 time_t epoch_plus_11h = 60 * 60 * 11;
78 struct tm ltime, gtime;
79 int offset_min;
80
81 #if defined(PJ_HAS_LOCALTIME_R) && PJ_HAS_LOCALTIME_R != 0
82 localtime_r(&epoch_plus_11h, &ltime);
83 gmtime_r(&epoch_plus_11h, &gtime);
84 #else
85 ltime = *localtime(&epoch_plus_11h);
86 gtime = *gmtime(&epoch_plus_11h);
87 #endif
88
89 offset_min = (ltime.tm_hour*60+ltime.tm_min) - (gtime.tm_hour*60+gtime.tm_min);
90 return offset_min*60;
91 }
92
93 /**
94 * Convert local time to GMT.
95 */
96 PJ_DEF(pj_status_t) pj_time_local_to_gmt(pj_time_val *tv)
97 {
98 tv->sec -= get_tz_offset_secs();
99 return PJ_SUCCESS;
100 }
101
102 /**
103 * Convert GMT to local time.
104 */
105 PJ_DEF(pj_status_t) pj_time_gmt_to_local(pj_time_val *tv)
106 {
107 tv->sec += get_tz_offset_secs();
108 return PJ_SUCCESS;
109 }
110
111 #endif /* !PJ_WIN32 */