| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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; |
| 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 |
|
| 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 |
|
| 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, <ime); |
| 83 |
gmtime_r(&epoch_plus_11h, >ime); |
| 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 |
|
| 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 |
|
| 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 |