[Fix] Use localtime_r() instead of localtime()
[platform/core/connectivity/stc-manager.git] / src / monitor / stc-time.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 #include <time.h>
18 #include <stdbool.h>
19
20 #define SEC_IN_DAY 24 * 60 * 60
21 #define SEC_IN_WEEK 7 * SEC_IN_DAY
22
23 time_t stc_time_get_day_start(time_t now)
24 {
25         struct tm curr;
26         struct tm *res;
27
28         res = localtime_r(&now, &curr);
29
30         curr.tm_sec = 0;
31         curr.tm_min = 0;
32         curr.tm_hour = 0;
33
34         return mktime(&curr);
35 }
36
37 time_t stc_time_get_week_start(time_t now)
38 {
39         struct tm *curr;
40         int days;
41
42         curr = localtime(&now);
43
44         curr->tm_sec = 0;
45         curr->tm_min = 0;
46         curr->tm_hour = 0;
47
48         if (curr->tm_wday > 1)
49                 days = curr->tm_wday - 1;
50         else
51                 days = 1 - curr->tm_wday;
52
53         return (mktime(curr) - (days * SEC_IN_DAY));
54 }
55
56 time_t stc_time_get_month_start(time_t now, int month_start_date)
57 {
58         struct tm *curr;
59         bool is_leap_year;
60
61         curr = localtime(&now);
62
63         curr->tm_sec = 0;
64         curr->tm_min = 0;
65         curr->tm_hour = 0;
66
67         if (curr->tm_mday < month_start_date) {
68                 curr->tm_mon--;
69                 if (curr->tm_mon < 0) {
70                         curr->tm_mon = 11;
71                         curr->tm_year--;
72                 }
73         }
74
75         is_leap_year = ((curr->tm_year + 1900) % 4 ? 0 : 1);
76         curr->tm_mday = month_start_date;
77
78         switch (month_start_date) {
79         case 29:
80         case 30:
81                 if (curr->tm_mon == 1 && !is_leap_year)
82                         curr->tm_mday = 28;
83
84                 else if (curr->tm_mon == 1 && is_leap_year)
85                         curr->tm_mday = 29;
86
87                 break;
88         case 31:
89                 if (curr->tm_mon == 1 && !is_leap_year)
90                         curr->tm_mday = 28;
91
92                 else if (curr->tm_mon == 1 && is_leap_year)
93                         curr->tm_mday = 29;
94
95                 else if (curr->tm_mon == 3 || curr->tm_mon == 5 ||
96                          curr->tm_mon == 8 || curr->tm_mon == 10)
97                         curr->tm_mday = 30;
98
99                 break;
100         default:
101                 ;//Do Nothing
102         };
103
104         return mktime(curr);
105 }