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