Coverage Report

Created: 2025-08-03 06:03

/src/pjsip/pjlib/src/pj/os_time_common.c
Line
Count
Source (jump to first uncovered line)
1
/* 
2
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3
 * Copyright (C) 2003-2008 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/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
11
{
30
11
    struct tm local_time;
31
11
    time_t sec = tv->sec; // time_t might be 64 bit even when long int is 32 bit, (time_t*) dangerous
32
33
11
    PJ_CHECK_STACK();
34
35
11
#if defined(PJ_HAS_LOCALTIME_R) && PJ_HAS_LOCALTIME_R != 0
36
11
    localtime_r(&sec, &local_time);
37
#else
38
    /* localtime() is NOT thread-safe. */
39
    local_time = *localtime(&sec);
40
#endif
41
42
11
    pt->year = local_time.tm_year+1900;
43
11
    pt->mon = local_time.tm_mon;
44
11
    pt->day = local_time.tm_mday;
45
11
    pt->hour = local_time.tm_hour;
46
11
    pt->min = local_time.tm_min;
47
11
    pt->sec = local_time.tm_sec;
48
11
    pt->wday = local_time.tm_wday;
49
11
    pt->msec = tv->msec;
50
51
11
    return PJ_SUCCESS;
52
11
}
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
0
{
59
0
    struct tm local_time;
60
61
0
    local_time.tm_year = pt->year-1900;
62
0
    local_time.tm_mon = pt->mon;
63
0
    local_time.tm_mday = pt->day;
64
0
    local_time.tm_hour = pt->hour;
65
0
    local_time.tm_min = pt->min;
66
0
    local_time.tm_sec = pt->sec;
67
0
    local_time.tm_isdst = 0;
68
    
69
0
    tv->sec = mktime(&local_time);
70
0
    tv->msec = pt->msec;
71
72
0
    return PJ_SUCCESS;
73
0
}
74
75
static int get_tz_offset_secs()
76
0
{
77
0
    time_t epoch_plus_11h = 60 * 60 * 11;
78
0
    struct tm ltime, gtime;
79
0
    int offset_min;
80
81
0
#if defined(PJ_HAS_LOCALTIME_R) && PJ_HAS_LOCALTIME_R != 0
82
0
    localtime_r(&epoch_plus_11h, &ltime);
83
0
    gmtime_r(&epoch_plus_11h, &gtime);
84
#else
85
    ltime = *localtime(&epoch_plus_11h);
86
    gtime = *gmtime(&epoch_plus_11h);
87
#endif
88
89
0
    offset_min = (ltime.tm_hour*60+ltime.tm_min) - (gtime.tm_hour*60+gtime.tm_min);
90
0
    return offset_min*60;
91
0
}
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
0
{
98
0
    tv->sec -= get_tz_offset_secs();
99
0
    return PJ_SUCCESS;
100
0
}
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
0
{
107
0
    tv->sec += get_tz_offset_secs();
108
0
    return PJ_SUCCESS;
109
0
}
110
111
#endif /* !PJ_WIN32 */