002ecba4a51e112d9e36143d8d3fa920d59e7230
[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     public class CalendarTime:IComparable<CalendarTime>
25     {
26         internal int _type;
27         internal const int milliseconds = 10000000;
28
29         /// <summary>
30         /// Enumeration for the time type.
31         /// </summary>
32         public enum Type
33         {
34             /// <summary>
35             /// UTC time
36             /// </summary>
37             Utc,
38             /// <summary>
39             /// Local time
40             /// </summary>
41             Local
42         }
43
44         /// <summary>
45         /// Create UTC CalendarTime
46         /// </summary>
47         /// <param name="utcTime">UTC epoch time. 0 is 1971/01/01</param>
48         public CalendarTime(long utcTime)
49         {
50             _type = (int)Type.Utc;
51             utcTime -= utcTime % milliseconds; /* delete millisecond */
52             UtcTime = new DateTime(utcTime);
53         }
54
55         /// <summary>
56         /// Create Local CalendarTime
57         /// </summary>
58         /// <param name="year">year</param>
59         /// <param name="month">month</param>
60         /// <param name="day">day</param>
61         /// <param name="hour">hour</param>
62         /// <param name="minute">minute</param>
63         /// <param name="second">second</param>
64         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
65         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
66         public CalendarTime(int year, int month, int day, int hour, int minute, int second)
67         {
68             _type = (int)Type.Local;
69             LocalTime = new DateTime(year, month, day, hour, minute, second);
70         }
71
72         /// <summary>
73         /// Get UtcTime
74         /// </summary>
75         /// <value>The Utc time</value>
76         public DateTime UtcTime
77         {
78             get;
79         }
80
81         /// <summary>
82         /// Get localTime
83         /// </summary>
84         /// <value>The Localtime</value>
85         public DateTime LocalTime
86         {
87             get;
88         }
89
90         /// <summary>
91         /// Compare CalendarTime
92         /// </summary>
93         /// <param name="other">The CalendarTime to be compared</param>
94         /// <returns>
95         /// A 32-bit signed integer that indicates the relative order of the objects being compared.
96         /// </returns>
97         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
98         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
99         public int CompareTo(CalendarTime other)
100         {
101             if (_type != other._type)
102             {
103                 Log.Error(Globals.LogTag, "Not to compare with different type");
104                 throw CalendarErrorFactory.GetException((int)CalendarError.InvalidParameter);
105             }
106
107             if (_type == (int)Type.Utc)
108                 return UtcTime.CompareTo(other.UtcTime);
109             else
110                 return LocalTime.CompareTo(other.LocalTime);
111         }
112
113         /// <summary>
114         /// Equals CalendarTime
115         /// </summary>
116         /// <param name="obj">The CalendarTime to be compared</param>
117         /// <returns>
118         /// A 32-bit signed integer that indicates the relative order of the objects being compared.
119         /// </returns>
120         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
121         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
122         public override bool Equals(object obj)
123         {
124             var other = obj as CalendarTime;
125             if (_type != other._type)
126             {
127                 Log.Error(Globals.LogTag, "Not to compare with different type");
128                 throw CalendarErrorFactory.GetException((int)CalendarError.InvalidParameter);
129             }
130
131             if (_type == (int)Type.Utc)
132                 return UtcTime.Equals(other.UtcTime);
133             else
134                 return LocalTime.Equals(other.LocalTime);
135         }
136     }
137 }
138