Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / ElmSharp / Interop / Interop.Libc.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 internal static partial class Interop
21 {
22     internal static partial class Libc
23     {
24         [DllImport(Libraries.Libc, EntryPoint = "free", CallingConvention = CallingConvention.Cdecl)]
25         internal static extern int Free(IntPtr ptr);
26
27         // Broken-down time is stored in the structure tm which is defined in <time.h> as follows:
28         [StructLayout(LayoutKind.Sequential)]
29         internal struct SystemTime
30         {
31             public int tm_sec;
32             public int tm_min;
33             public int tm_hour;
34             public int tm_mday;
35             public int tm_mon;
36             public int tm_year;
37             public int tm_wday;
38             public int tm_yday;
39             public int tm_isdst;
40
41             // The glibc version of struct tm has additional fields
42             public long tm_gmtoff;
43             public IntPtr tm_zone;
44
45             public static implicit operator SystemTime(DateTime value)
46             {
47                 SystemTime tm = new SystemTime();
48                 tm.tm_sec = value.Second;
49                 tm.tm_min = value.Minute;
50                 tm.tm_hour = value.Hour;
51                 tm.tm_mday = value.Day;
52                 tm.tm_mon = value.Month - 1;
53                 tm.tm_year = value.Year - 1900;
54                 tm.tm_wday = (int)value.DayOfWeek;
55                 tm.tm_yday = value.DayOfYear;
56                 tm.tm_isdst = value.IsDaylightSavingTime() ? 1 : 0;
57                 return tm;
58             }
59
60             public static implicit operator DateTime(SystemTime value)
61             {
62                 DateTime date = new DateTime(value.tm_year + 1900, value.tm_mon + 1, value.tm_mday, value.tm_hour, value.tm_min, value.tm_sec);
63                 return date;
64             }
65         }
66     }
67 }