Release 4.0.0-preview1-00253
[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         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
71         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
72         public CalendarTime(int year, int month, int day, int hour, int minute, int second)
73         {
74             _type = (int)Type.Local;
75             LocalTime = new DateTime(year, month, day, hour, minute, second);
76         }
77
78         /// <summary>
79         /// Get UtcTime
80         /// </summary>
81         /// <since_tizen> 4 </since_tizen>
82         /// <value>The Utc time</value>
83         public DateTime UtcTime
84         {
85             get;
86         }
87
88         /// <summary>
89         /// Get localTime
90         /// </summary>
91         /// <since_tizen> 4 </since_tizen>
92         /// <value>The Localtime</value>
93         public DateTime LocalTime
94         {
95             get;
96         }
97
98         /// <summary>
99         /// Compare CalendarTime
100         /// </summary>
101         /// <since_tizen> 4 </since_tizen>
102         /// <param name="other">The CalendarTime to be compared</param>
103         /// <returns>
104         /// A 32-bit signed integer that indicates the relative order of the objects being compared.
105         /// </returns>
106         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
107         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
108         public int CompareTo(CalendarTime other)
109         {
110             if (_type != other._type)
111             {
112                 Log.Error(Globals.LogTag, "Not to compare with different type");
113                 throw CalendarErrorFactory.GetException((int)CalendarError.InvalidParameter);
114             }
115
116             if (_type == (int)Type.Utc)
117                 return UtcTime.CompareTo(other.UtcTime);
118             else
119                 return LocalTime.CompareTo(other.LocalTime);
120         }
121
122         /// <summary>
123         /// Equals CalendarTime
124         /// </summary>
125         /// <since_tizen> 4 </since_tizen>
126         /// <param name="obj">The CalendarTime to be compared</param>
127         /// <returns>
128         /// A 32-bit signed integer that indicates the relative order of the objects being compared.
129         /// </returns>
130         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
131         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
132         public override bool Equals(object obj)
133         {
134             var other = obj as CalendarTime;
135             if (_type != other._type)
136             {
137                 Log.Error(Globals.LogTag, "Not to compare with different type");
138                 return false;
139             }
140
141             if (_type == (int)Type.Utc)
142                 return UtcTime.Equals(other.UtcTime);
143             else
144                 return LocalTime.Equals(other.LocalTime);
145         }
146
147         /// <summary>
148         /// GetHashCode CalendarTime
149         /// </summary>
150         /// <since_tizen> 4 </since_tizen>
151         /// <returns>
152         /// A hash code for the current object.
153         /// </returns>
154         public override int GetHashCode()
155         {
156             if (_type == (int)Type.Utc)
157                 return this.UtcTime.GetHashCode();
158             else
159                 return this.LocalTime.GetHashCode();
160         }
161     }
162 }
163