Release 4.0.0-preview1-00051
[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 using System.Runtime.InteropServices;
19
20 namespace Tizen.Pims.Calendar
21 {
22     /// <summary>
23     /// A class for time to set, get or calcurate.
24     /// </summary>
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         public enum Type
34         {
35             /// <summary>
36             /// UTC time
37             /// </summary>
38             Utc,
39             /// <summary>
40             /// Local time
41             /// </summary>
42             Local
43         }
44
45         /// <summary>
46         /// Create UTC CalendarTime
47         /// </summary>
48         /// <param name="utcTime">UTC epoch time. 0 is 1971/01/01</param>
49         public CalendarTime(long utcTime)
50         {
51             _type = (int)Type.Utc;
52             utcTime -= utcTime % milliseconds; /* delete millisecond */
53             UtcTime = new DateTime(utcTime);
54         }
55
56         /// <summary>
57         /// Create Local CalendarTime
58         /// </summary>
59         /// <param name="year">year</param>
60         /// <param name="month">month</param>
61         /// <param name="day">day</param>
62         /// <param name="hour">hour</param>
63         /// <param name="minute">minute</param>
64         /// <param name="second">second</param>
65         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
66         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
67         public CalendarTime(int year, int month, int day, int hour, int minute, int second)
68         {
69             _type = (int)Type.Local;
70             LocalTime = new DateTime(year, month, day, hour, minute, second);
71         }
72
73         /// <summary>
74         /// Get utcTime
75         /// </summary>
76         public DateTime UtcTime
77         {
78             get;
79         }
80
81         /// <summary>
82         /// Get localTime
83         /// </summary>
84         public DateTime LocalTime
85         {
86             get;
87         }
88
89         /// <summary>
90         /// Compare CalendarTime
91         /// </summary>
92         /// <param name="t">The CalendarTime to be compared</param>
93         /// <returns>
94         /// A 32-bit signed integer that indicates the relative order of the objects being compared.
95         /// </returns>
96         /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid</exception>
97         /// <exception cref="OutOfMemoryException">Thrown when failed due to out of memory</exception>
98         public int CompareTo(CalendarTime t)
99         {
100             if (_type != t._type)
101             {
102                 Log.Error(Globals.LogTag, "Not to compare with different type");
103                 throw CalendarErrorFactory.GetException((int)CalendarError.InvalidParameter);
104             }
105
106             if (_type == (int)Type.Utc)
107                 return UtcTime.CompareTo(t.UtcTime);
108             else
109                 return LocalTime.CompareTo(t.LocalTime);
110         }
111     }
112 }
113