Merge "[Fix] Use localtime_r() instead of localtime()" into tizen
[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         struct tm *res;
41         int days;
42
43         res = localtime_r(&now, &curr);
44
45         curr.tm_sec = 0;
46         curr.tm_min = 0;
47         curr.tm_hour = 0;
48
49         if (curr.tm_wday > 1)
50                 days = curr.tm_wday - 1;
51         else
52                 days = 1 - curr.tm_wday;
53
54         return (mktime(&curr) - (days * SEC_IN_DAY));
55 }
56
57 time_t stc_time_get_month_start(time_t now, int month_start_date)
58 {
59         struct tm *curr;
60         bool is_leap_year;
61
62         curr = localtime(&now);
63
64         curr->tm_sec = 0;
65         curr->tm_min = 0;
66         curr->tm_hour = 0;
67
68         if (curr->tm_mday < month_start_date) {
69                 curr->tm_mon--;
70                 if (curr->tm_mon < 0) {
71                         curr->tm_mon = 11;
72                         curr->tm_year--;
73                 }
74         }
75
76         is_leap_year = ((curr->tm_year + 1900) % 4 ? 0 : 1);
77         curr->tm_mday = month_start_date;
78
79         switch (month_start_date) {
80         case 29:
81         case 30:
82                 if (curr->tm_mon == 1 && !is_leap_year)
83                         curr->tm_mday = 28;
84
85                 else if (curr->tm_mon == 1 && is_leap_year)
86                         curr->tm_mday = 29;
87
88                 break;
89         case 31:
90                 if (curr->tm_mon == 1 && !is_leap_year)
91                         curr->tm_mday = 28;
92
93                 else if (curr->tm_mon == 1 && is_leap_year)
94                         curr->tm_mday = 29;
95
96                 else if (curr->tm_mon == 3 || curr->tm_mon == 5 ||
97                          curr->tm_mon == 8 || curr->tm_mon == 10)
98                         curr->tm_mday = 30;
99
100                 break;
101         default:
102                 ;//Do Nothing
103         };
104
105         return mktime(curr);
106 }