74e8f9a15aa8f1ed86377db3814351620aa91b65
[platform/core/security/drm-service-core-tizen.git] / tappsd / src / util / DTapps2Time.cpp
1 /*
2  * Copyright (c) 2000-2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.1 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file        DTapps2Time.cpp
19  * @brief       This file includes functions relating to TIME APIs.
20  */
21
22 #include "DTapps2Time.h"
23
24 BOOL DTappsDtTmStr2Sec(unsigned char *time_str,time_t *TotalSec)
25 {
26         char str[32] = {0};
27         char short_str[5] = {0};
28         struct tm time_fmt;
29
30         memset(&time_fmt, 0x00, sizeof(time_fmt));
31
32         TAPPS_GSTRLCPY(str, (const char*)time_str, sizeof(str));
33
34         DRM_TAPPS_LOG("str = %s",str);
35
36         if(str[4] != '-' || str[7] != '-' || str[10] != 'T' || str[13] != ':' || str[16] != ':')
37         {
38                 DRM_TAPPS_EXCEPTION("Format is incorrect:str=%s", str);
39
40                 return FALSE;
41         }
42
43         DTAPPS_MEMCPY(short_str, str, 4);
44         time_fmt.tm_year = DTAPPS_ATOI(short_str) - 1900;
45         DRM_TAPPS_FRQ_LOG("tm_year = %d", time_fmt.tm_year);
46
47         DTAPPS_MEMSET(short_str, 0x0,5);
48         DTAPPS_MEMCPY(short_str, str + 5, 2);
49         time_fmt.tm_mon = DTAPPS_ATOI(short_str) - 1;
50         DRM_TAPPS_FRQ_LOG("tm_mon = %d", time_fmt.tm_mon);
51
52         DTAPPS_MEMCPY(short_str, str + 8, 2);
53         time_fmt.tm_mday = DTAPPS_ATOI(short_str);
54         DRM_TAPPS_FRQ_LOG("tm_mday = %d", time_fmt.tm_mday);
55         DTAPPS_MEMCPY(short_str, str + 11, 2);
56         time_fmt.tm_hour = DTAPPS_ATOI(short_str);
57         DRM_TAPPS_FRQ_LOG("tm_hour = %d", time_fmt.tm_hour);
58
59         DTAPPS_MEMCPY(short_str, str + 14, 2);
60         time_fmt.tm_min = DTAPPS_ATOI(short_str);
61         DRM_TAPPS_FRQ_LOG("tm_min = %d", time_fmt.tm_min);
62
63         DTAPPS_MEMCPY(short_str, str + 17, 2);
64         time_fmt.tm_sec = DTAPPS_ATOI(short_str);
65         DRM_TAPPS_FRQ_LOG("tm_sec = %d", time_fmt.tm_sec);
66
67         /* Convert into Seconds */
68         *TotalSec = DTAPPS_MKTIME(&time_fmt);
69         DRM_TAPPS_LOG("TotalSec = %lu", *TotalSec);
70
71         return TRUE;
72 }
73
74 BOOL DTappsDtTmStr2StrucTm(unsigned char *time_str,struct tm *time_fmt)
75 {
76         char str[32] = {0};
77         char short_str[5] = {0};
78
79         TAPPS_GSTRLCPY(str, (const char*)time_str, sizeof(str));
80
81         DRM_TAPPS_LOG("str = %s", str);
82
83         if(str[4]!='-' || str[7]!='-' || str[10]!='T' || str[13]!=':' || str[16]!=':')
84         {
85                 DRM_TAPPS_EXCEPTION("Format is incorrect:str=%s", str);
86
87                 return FALSE;
88         }
89
90         DTAPPS_MEMCPY(short_str, str, 4);
91         time_fmt->tm_year = DTAPPS_ATOI(short_str) - 1900;
92         DRM_TAPPS_FRQ_LOG("tm_year = %d", time_fmt->tm_year);
93
94         DTAPPS_MEMSET(short_str, 0x0, 5);
95         DTAPPS_MEMCPY(short_str, str + 5, 2);
96         time_fmt->tm_mon = DTAPPS_ATOI(short_str) - 1;
97         DRM_TAPPS_FRQ_LOG("tm_mon = %d", time_fmt->tm_mon);
98
99         DTAPPS_MEMCPY(short_str, str + 8, 2);
100         time_fmt->tm_mday = DTAPPS_ATOI(short_str);
101         DRM_TAPPS_FRQ_LOG("tm_mday = %d", time_fmt->tm_mday);
102
103         DTAPPS_MEMCPY(short_str, str + 11, 2);
104         time_fmt->tm_hour = DTAPPS_ATOI(short_str);
105         DRM_TAPPS_FRQ_LOG("tm_hour = %d", time_fmt->tm_hour);
106
107         DTAPPS_MEMCPY(short_str, str + 14, 2);
108         time_fmt->tm_min = DTAPPS_ATOI(short_str);
109         DRM_TAPPS_FRQ_LOG("tm_min = %d", time_fmt->tm_min);
110
111         DTAPPS_MEMCPY(short_str, str + 17, 2);
112         time_fmt->tm_sec = DTAPPS_ATOI(short_str);
113         DRM_TAPPS_FRQ_LOG("tm_sec = %d", time_fmt->tm_sec);
114
115         return TRUE;
116 }
117
118 BOOL DTappsGetSecureTime(time_t* seconds)
119 {
120         time_t now = 0, secure_sec = 0;
121         int Delta = 0;
122         struct tm gm_fmt;
123
124         memset(&gm_fmt, 0x00, sizeof(gm_fmt));
125
126         now = DTAPPS_TIME(NULL);
127
128         /* TODO: Read the Delta from VCONF */
129
130         secure_sec = now + Delta;
131
132         /* Operations to be done in GMT - Convert this to GM Time */
133     DTAPPS_GMTIME_THREAD_SAFE(&secure_sec, &gm_fmt);
134         *seconds = DTAPPS_MKTIME(&gm_fmt);
135
136         DRM_TAPPS_LOG("now = %d, secure_sec = %d, seconds(SecureTime in GMT) = %d", now, secure_sec, *seconds);
137
138         return TRUE;
139 }