e0f86e07532bd7d19750f3c26aa19474469598db
[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         struct tm *res;
61         bool is_leap_year;
62
63         res = localtime_r(&now, &curr);
64
65         curr.tm_sec = 0;
66         curr.tm_min = 0;
67         curr.tm_hour = 0;
68
69         if (curr.tm_mday < month_start_date) {
70                 curr.tm_mon--;
71                 if (curr.tm_mon < 0) {
72                         curr.tm_mon = 11;
73                         curr.tm_year--;
74                 }
75         }
76
77         is_leap_year = ((curr.tm_year + 1900) % 4 ? 0 : 1);
78         curr.tm_mday = month_start_date;
79
80         switch (month_start_date) {
81         case 29:
82         case 30:
83                 if (curr.tm_mon == 1 && !is_leap_year)
84                         curr.tm_mday = 28;
85
86                 else if (curr.tm_mon == 1 && is_leap_year)
87                         curr.tm_mday = 29;
88
89                 break;
90         case 31:
91                 if (curr.tm_mon == 1 && !is_leap_year)
92                         curr.tm_mday = 28;
93
94                 else if (curr.tm_mon == 1 && is_leap_year)
95                         curr.tm_mday = 29;
96
97                 else if (curr.tm_mon == 3 || curr.tm_mon == 5 ||
98                          curr.tm_mon == 8 || curr.tm_mon == 10)
99                         curr.tm_mday = 30;
100
101                 break;
102         default:
103                 ;//Do Nothing
104         };
105
106         return mktime(&curr);
107 }