[Calendar]Remove invalid exception
[platform/core/csapi/tizenfx.git] / src / Tizen.Pims.Calendar / Tizen.Pims.Calendar / CalendarStructs.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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 using System;
18
19 namespace Tizen.Pims.Calendar
20 {
21     /// <summary>
22     /// A class for time to set, get or calculate.
23     /// </summary>
24     /// <since_tizen> 4 </since_tizen>
25     public class CalendarTime:IComparable<CalendarTime>
26     {
27         internal int _type;
28         internal const int milliseconds = 10000000;
29
30         /// <summary>
31         /// Enumeration for the time type.
32         /// </summary>
33         /// <since_tizen> 4 </since_tizen>
34         public enum Type
35         {
36             /// <summary>
37             /// UTC time
38             /// </summary>
39             /// <since_tizen> 4 </since_tizen>
40             Utc,
41             /// <summary>
42             /// Local time
43             /// </summary>
44             /// <since_tizen> 4 </since_tizen>
45             Local
46         }
47
48         /// <summary>
49         /// Create UTC CalendarTime
50         /// </summary>
51         /// <since_tizen> 4 </since_tizen>
52         /// <param name="utcTime">UTC epoch time. 0 is 1971/01/01</param>
53         public CalendarTime(long utcTime)
54         {
55             _type = (int)Type.Utc;
56             utcTime -= utcTime % milliseconds; /* delete millisecond */
57             UtcTime = new DateTime(utcTime);
58         }
59
60         /// <summary>
61         /// Create Local CalendarTime
62         /// </summary>
63         /// <since_tizen> 4 </since_tizen>
64         /// <param name="year">year</param>
65         /// <param name="month">month</param>
66         /// <param name="day">day</param>
67         /// <param name="hour">hour</param>
68         /// <param name="minute">minute</param>
69         /// <param name="second">second</param>
70         public CalendarTime(int year, int month, int day, int hour, int minute, int second)
71         {
72             _type = (int)Type.Local;
73             LocalTime = new DateTime(year, month, day, hour, minute, second);
74         }
75
76         /// <summary>
77         /// Get UtcTime
78         /// </summary>
79         /// <since_tizen> 4 </since_tizen>
80         /// <value>The Utc time</value>
81         public DateTime UtcTime
82         {
83             get;
84         }
85
86         /// <summary>
87         /// Get localTime
88         /// </summary>
89         /// <since_tizen> 4 </since_tizen>
90         /// <value>The Localtime</value>
91         public DateTime LocalTime
92         {
93             get;
94         }
95
96         /// <summary>
97         /// Compare CalendarTime
98         /// </summary>
99         /// <since_tizen> 4 </since_tizen>
100         /// <param name="other">The CalendarTime to be compared</param>
101         /// <returns>
102         /// A 32-bit signed integer that indicates the relative order of the objects being compared.
103         /// </returns>
104         public int CompareTo(CalendarTime other)
105         {
106             if (_type != other._type)
107             {
108                 Log.Error(Globals.LogTag, "Not to compare with different type");
109                 throw CalendarErrorFactory.GetException((int)CalendarError.InvalidParameter);
110             }
111
112             if (_type == (int)Type.Utc)
113                 return UtcTime.CompareTo(other.UtcTime);
114             else
115                 return LocalTime.CompareTo(other.LocalTime);
116         }
117
118         /// <summary>
119         /// Equals CalendarTime
120         /// </summary>
121         /// <since_tizen> 4 </since_tizen>
122         /// <param name="obj">The CalendarTime to be compared</param>
123         /// <returns>
124         /// A 32-bit signed integer that indicates the relative order of the objects being compared.
125         /// </returns>
126         public override bool Equals(object obj)
127         {
128             var other = obj as CalendarTime;
129             if (_type != other._type)
130             {
131                 Log.Error(Globals.LogTag, "Not to compare with different type");
132                 return false;
133             }
134
135             if (_type == (int)Type.Utc)
136                 return UtcTime.Equals(other.UtcTime);
137             else
138                 return LocalTime.Equals(other.LocalTime);
139         }
140
141         /// <summary>
142         /// GetHashCode CalendarTime
143         /// </summary>
144         /// <since_tizen> 4 </since_tizen>
145         /// <returns>
146         /// A hash code for the current object.
147         /// </returns>
148         public override int GetHashCode()
149         {
150             if (_type == (int)Type.Utc)
151                 return this.UtcTime.GetHashCode();
152             else
153                 return this.LocalTime.GetHashCode();
154         }
155     }
156 }
157