7ae50aee033b7d9029974aab63e6e78623243411
[platform/upstream/iotivity.git] / service / things-manager / sdk / java / src / org / iotivity / service / tm / Time.java
1 /* *****************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 package org.iotivity.service.tm;
22
23 import android.util.Log;
24
25 /**
26  * This class provides time-related information used for scheduled/recursive
27  * group action features. Along with time-related variables, it also provides
28  * various useful functionality including translating time to second unit
29  */
30 public class Time {
31
32     public enum ActionSetType {
33         NONE, SCHEDULED, RECURSIVE
34     }
35
36     public int            mYear      = 0;
37     public int            mMonth     = 0;
38     public int            mDay       = 0;
39     public int            mHour      = 0;
40     public int            mMin       = 0;
41     public int            mSec       = 0;
42     public int            mDayOfWeek = 0;
43
44     private ActionSetType mType      = ActionSetType.NONE;
45     private long          mDelay     = 0;
46
47     private final String  LOG_TAG    = this.getClass().getSimpleName();
48
49     /**
50      * Set the time for executing ActionSet.
51      *
52      * @param year
53      *            Year to be set
54      * @param month
55      *            Month of the year to be set
56      * @param day
57      *            Day of the month to be set
58      * @param hour
59      *            Hour to be set
60      * @param min
61      *            Minute to be set
62      * @param sec
63      *            Second to be set
64      * @param dayOfTheWeek
65      *            Day of the week to be set
66      */
67     public void setTime(int year, int month, int day, int hour, int min,
68             int sec, int dayOfTheWeek) {
69         if (year < 0 || month < 0 || day < 0 || hour < 0 || min < 0 || sec < 0
70                 || dayOfTheWeek < 0) {
71             Log.e(LOG_TAG, "Input time is invalid");
72             return;
73         }
74
75         year -= 1900;
76         month -= 1;
77
78         mDelay = 0;
79         mYear = year;
80         mMonth = month;
81         mDay = day;
82         mHour = hour;
83         mMin = month;
84         mSec = sec;
85         mDayOfWeek = dayOfTheWeek;
86         mType = ActionSetType.NONE;
87     }
88
89     /**
90      * Set type of ActionSet.
91      *
92      * @param type
93      *            Type of ActionSet
94      */
95     public void setType(ActionSetType type) {
96         mType = type;
97     }
98
99     /**
100      * Set day of the week for recursively executing ActionSet.
101      *
102      * @param day
103      *            day of the week
104      */
105     public void setDayOfWeekForRecursive(int day) {
106         if (day != -1) {
107             mType = ActionSetType.RECURSIVE;
108             setTime(0, 0, 0, 0, 0, 0, day);
109         }
110     }
111
112     /**
113      * Set the time delay in seconds for executing ActionSet.
114      *
115      * @param seconds
116      *            time delay in seconds
117      */
118     public void setDelay(long seconds) {
119         if (mType != ActionSetType.NONE) {
120             mDelay = seconds;
121         }
122     }
123
124     /**
125      * Get absolute time in seconds.
126      *
127      * @return long - Absolute time in seconds.
128      */
129     public long getSecAbsTime() {
130         long interval;
131
132         interval = (mHour * 60 * 60);
133         interval += (mMin * 60);
134         interval += (mSec * 1);
135
136         return interval;
137     }
138
139     /**
140      * Get the type of ActionSet.
141      *
142      * @return ActionSetType - Type of ActionSet.
143      */
144     public ActionSetType getType() {
145         return mType;
146     }
147
148     /**
149      * Get the time delay in seconds set in the ActionSet.
150      *
151      * @return long - Delay in seconds.
152      */
153     public long getDelay() {
154         return mDelay;
155     }
156 }