Separate monitoring function plugin
[platform/core/connectivity/stc-manager.git] / plugin / monitor / stc-plugin-monitor-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         if (!res)
30                 return 0;
31
32         curr.tm_sec = 0;
33         curr.tm_min = 0;
34         curr.tm_hour = 0;
35
36         return mktime(&curr);
37 }
38
39 time_t stc_time_get_week_start(time_t now)
40 {
41         struct tm curr;
42         struct tm *res;
43         int days;
44
45         res = localtime_r(&now, &curr);
46         if (!res)
47                 return 0;
48
49         curr.tm_sec = 0;
50         curr.tm_min = 0;
51         curr.tm_hour = 0;
52
53         if (curr.tm_wday > 1)
54                 days = curr.tm_wday - 1;
55         else
56                 days = 1 - curr.tm_wday;
57
58         return (mktime(&curr) - (days * SEC_IN_DAY));
59 }
60
61 time_t stc_time_get_month_start(time_t now, int month_start_date)
62 {
63         struct tm curr;
64         struct tm *res;
65         bool is_leap_year;
66
67         res = localtime_r(&now, &curr);
68         if (!res)
69                 return 0;
70
71         curr.tm_sec = 0;
72         curr.tm_min = 0;
73         curr.tm_hour = 0;
74
75         if (curr.tm_mday < month_start_date) {
76                 curr.tm_mon--;
77                 if (curr.tm_mon < 0) {
78                         curr.tm_mon = 11;
79                         curr.tm_year--;
80                 }
81         }
82
83         is_leap_year = ((curr.tm_year + 1900) % 4 ? 0 : 1);
84         curr.tm_mday = month_start_date;
85
86         switch (month_start_date) {
87         case 29:
88         case 30:
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                 break;
96         case 31:
97                 if (curr.tm_mon == 1 && !is_leap_year)
98                         curr.tm_mday = 28;
99
100                 else if (curr.tm_mon == 1 && is_leap_year)
101                         curr.tm_mday = 29;
102
103                 else if (curr.tm_mon == 3 || curr.tm_mon == 5 ||
104                          curr.tm_mon == 8 || curr.tm_mon == 10)
105                         curr.tm_mday = 30;
106
107                 break;
108         default:
109                 ;//Do Nothing
110         };
111
112         return mktime(&curr);
113 }