From 7d0a8476eea58bac470da4f2356ad4a94feed886 Mon Sep 17 00:00:00 2001 From: WonYoung Choi Date: Mon, 26 Sep 2016 11:23:33 +0900 Subject: [PATCH] Add additional fields to structure SystemTime representing struct tm Change-Id: Ib44aa8ada3be508edbe9dbbfb8b94ff9087e7d6e --- src/ElmSharp/ElmSharp/Calendar.cs | 29 ++----------- src/ElmSharp/ElmSharp/DateTimeSelector.cs | 41 +++++------------- .../Interop.Elementary.CalendarView.cs | 8 ++-- .../Interop.Elementary.DateTimePicker.cs | 25 +++-------- src/ElmSharp/Interop/Interop.Libc.cs | 42 ++++++++++++++++++- 5 files changed, 65 insertions(+), 80 deletions(-) diff --git a/src/ElmSharp/ElmSharp/Calendar.cs b/src/ElmSharp/ElmSharp/Calendar.cs index e57715922..b7fa77bfa 100644 --- a/src/ElmSharp/ElmSharp/Calendar.cs +++ b/src/ElmSharp/ElmSharp/Calendar.cs @@ -105,13 +105,13 @@ namespace ElmSharp { get { - var tm = new Interop.Elementary.tm(); - Interop.Elementary.elm_calendar_selected_time_get(Handle, out tm); - return ConvertToDateTime(tm); + var tm = new Interop.Libc.SystemTime(); + Interop.Elementary.elm_calendar_selected_time_get(Handle, ref tm); + return tm; } set { - var tm = ConvertToTM(value); + Interop.Libc.SystemTime tm = value; Interop.Elementary.elm_calendar_selected_time_set(Handle, ref tm); _cacheSelectedDate = value; } @@ -134,27 +134,6 @@ namespace ElmSharp return Interop.Elementary.elm_calendar_add(parent.Handle); } - private static DateTime ConvertToDateTime(Interop.Elementary.tm tm) - { - DateTime date = new DateTime(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); - return date; - } - - private static Interop.Elementary.tm ConvertToTM(DateTime date) - { - Interop.Elementary.tm tm = new Interop.Elementary.tm(); - tm.tm_sec = date.Second; - tm.tm_min = date.Minute; - tm.tm_hour = date.Hour; - tm.tm_mday = date.Day; - tm.tm_mon = date.Month - 1; - tm.tm_year = date.Year - 1900; - tm.tm_wday = (int)date.DayOfWeek; - tm.tm_yday = date.DayOfYear; - tm.tm_isdst = date.IsDaylightSavingTime() ? 1 : 0; - return tm; - } - static private void IntPtrToStringArray(IntPtr unmanagedArray, int size, out string[] managedArray) { managedArray = new string[size]; diff --git a/src/ElmSharp/ElmSharp/DateTimeSelector.cs b/src/ElmSharp/ElmSharp/DateTimeSelector.cs index 9327c7503..5cb1cb4d9 100644 --- a/src/ElmSharp/ElmSharp/DateTimeSelector.cs +++ b/src/ElmSharp/ElmSharp/DateTimeSelector.cs @@ -46,13 +46,13 @@ namespace ElmSharp { get { - var tm = new Interop.Elementary.tm(); + var tm = new Interop.Libc.SystemTime(); Interop.Elementary.elm_datetime_value_max_get(Handle, ref tm); - return ConvertToDateTime(tm); + return tm; } set { - var tm = ConvertToTM(value); + Interop.Libc.SystemTime tm = value; Interop.Elementary.elm_datetime_value_max_set(Handle, ref tm); } } @@ -61,13 +61,13 @@ namespace ElmSharp { get { - var tm = new Interop.Elementary.tm(); + var tm = new Interop.Libc.SystemTime(); Interop.Elementary.elm_datetime_value_min_get(Handle, ref tm); - return ConvertToDateTime(tm); + return tm; } set { - var tm = ConvertToTM(value); + Interop.Libc.SystemTime tm = value; Interop.Elementary.elm_datetime_value_min_set(Handle, ref tm); } } @@ -76,13 +76,13 @@ namespace ElmSharp { get { - var tm = new Interop.Elementary.tm(); + var tm = new Interop.Libc.SystemTime(); Interop.Elementary.elm_datetime_value_get(Handle, ref tm); - return ConvertToDateTime(tm); + return tm; } set { - var tm = ConvertToTM(value); + Interop.Libc.SystemTime tm = value; Interop.Elementary.elm_datetime_value_set(Handle, ref tm); _cacheDateTime = value; } @@ -90,7 +90,7 @@ namespace ElmSharp public bool IsFieldVisible(DateTimeFieldType type) { - return Interop.Elementary.elm_datetime_field_visible_get(Handle, (int) type); + return Interop.Elementary.elm_datetime_field_visible_get(Handle, (int)type); } public void SetFieldLimit(DateTimeFieldType type, int minimum, int maximum) @@ -107,26 +107,5 @@ namespace ElmSharp { return Interop.Elementary.elm_datetime_add(parent.Handle); } - - DateTime ConvertToDateTime(Interop.Elementary.tm tm) - { - DateTime date = new DateTime(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); - return date; - } - - Interop.Elementary.tm ConvertToTM(DateTime date) - { - Interop.Elementary.tm tm = new Interop.Elementary.tm(); - tm.tm_sec = date.Second; - tm.tm_min = date.Minute; - tm.tm_hour = date.Hour; - tm.tm_mday = date.Day; - tm.tm_mon = date.Month - 1; - tm.tm_year = date.Year - 1900; - tm.tm_wday = (int)date.DayOfWeek; - tm.tm_yday = date.DayOfYear; - tm.tm_isdst = date.IsDaylightSavingTime() ? 1 : 0; - return tm; - } } } diff --git a/src/ElmSharp/Interop/Interop.Elementary.CalendarView.cs b/src/ElmSharp/Interop/Interop.Elementary.CalendarView.cs index b48c8c429..33c03b721 100644 --- a/src/ElmSharp/Interop/Interop.Elementary.CalendarView.cs +++ b/src/ElmSharp/Interop/Interop.Elementary.CalendarView.cs @@ -21,10 +21,10 @@ internal static partial class Interop internal static extern void elm_calendar_min_max_year_get(IntPtr obj, out int min, out int max); [DllImport(Libraries.Elementary)] - internal static extern void elm_calendar_selected_time_set(IntPtr obj, ref tm selectedtime); + internal static extern void elm_calendar_selected_time_set(IntPtr obj, ref Libc.SystemTime selectedtime); [DllImport(Libraries.Elementary)] - internal static extern void elm_calendar_selected_time_get(IntPtr obj, out tm selectedtime); + internal static extern void elm_calendar_selected_time_get(IntPtr obj, ref Libc.SystemTime selectedtime); [DllImport(Libraries.Elementary)] internal static extern void elm_calendar_first_day_of_week_set(IntPtr obj, int day); @@ -39,7 +39,7 @@ internal static partial class Interop internal static extern int elm_calendar_selectable_get(IntPtr obj); [DllImport(Libraries.Elementary)] - internal static extern bool elm_calendar_displayed_time_get(IntPtr obj, out tm displayedtime); + internal static extern bool elm_calendar_displayed_time_get(IntPtr obj, out Libc.SystemTime displayedtime); [DllImport(Libraries.Elementary)] internal static extern void elm_calendar_interval_set(IntPtr obj, double interval); @@ -47,4 +47,4 @@ internal static partial class Interop [DllImport(Libraries.Elementary)] internal static extern double elm_calendar_interval_get(IntPtr obj); } -} \ No newline at end of file +} diff --git a/src/ElmSharp/Interop/Interop.Elementary.DateTimePicker.cs b/src/ElmSharp/Interop/Interop.Elementary.DateTimePicker.cs index 759e0ddb0..6cb1e3e34 100644 --- a/src/ElmSharp/Interop/Interop.Elementary.DateTimePicker.cs +++ b/src/ElmSharp/Interop/Interop.Elementary.DateTimePicker.cs @@ -17,10 +17,10 @@ internal static partial class Interop internal static extern IntPtr elm_datetime_add(IntPtr obj); [DllImport(Libraries.Elementary)] - internal static extern bool elm_datetime_value_set(IntPtr obj, ref tm newtime); + internal static extern bool elm_datetime_value_set(IntPtr obj, ref Libc.SystemTime newtime); [DllImport(Libraries.Elementary)] - internal static extern bool elm_datetime_value_get(IntPtr obj, ref tm currtime); + internal static extern bool elm_datetime_value_get(IntPtr obj, ref Libc.SystemTime currtime); [DllImport(Libraries.Elementary)] internal static extern bool elm_datetime_format_set(IntPtr obj, string format); @@ -35,16 +35,16 @@ internal static partial class Interop } [DllImport(Libraries.Elementary)] - internal static extern bool elm_datetime_value_max_set(IntPtr obj, ref tm maxtime); + internal static extern bool elm_datetime_value_max_set(IntPtr obj, ref Libc.SystemTime maxtime); [DllImport(Libraries.Elementary)] - internal static extern bool elm_datetime_value_max_get(IntPtr obj, ref tm maxtime); + internal static extern bool elm_datetime_value_max_get(IntPtr obj, ref Libc.SystemTime maxtime); [DllImport(Libraries.Elementary)] - internal static extern bool elm_datetime_value_min_set(IntPtr obj, ref tm mintime); + internal static extern bool elm_datetime_value_min_set(IntPtr obj, ref Libc.SystemTime mintime); [DllImport(Libraries.Elementary)] - internal static extern bool elm_datetime_value_min_get(IntPtr obj, ref tm mintime); + internal static extern bool elm_datetime_value_min_get(IntPtr obj, ref Libc.SystemTime mintime); [DllImport(Libraries.Elementary)] internal static extern void elm_datetime_field_limit_set(IntPtr obj, int type, int min, int max); @@ -58,19 +58,6 @@ internal static partial class Interop [DllImport(Libraries.Elementary)] internal static extern bool elm_datetime_field_visible_get(IntPtr obj, int type); - [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] - internal struct tm - { - public int tm_sec; - public int tm_min; - public int tm_hour; - public int tm_mday; - public int tm_mon; - public int tm_year; - public int tm_wday; - public int tm_yday; - public int tm_isdst; - } internal enum DateTimeFieldType { Year, diff --git a/src/ElmSharp/Interop/Interop.Libc.cs b/src/ElmSharp/Interop/Interop.Libc.cs index b01ae6dcb..af32e2072 100644 --- a/src/ElmSharp/Interop/Interop.Libc.cs +++ b/src/ElmSharp/Interop/Interop.Libc.cs @@ -1,4 +1,4 @@ -// Copyright 2016 by Samsung Electronics, Inc., +// Copyright 2016 by Samsung Electronics, Inc., // // This software is the confidential and proprietary information // of Samsung Electronics, Inc. ("Confidential Information"). You @@ -15,5 +15,45 @@ internal static partial class Interop { [DllImport(Libraries.Libc, EntryPoint = "free", CallingConvention = CallingConvention.Cdecl)] internal static extern int Free(IntPtr ptr); + + // Broken-down time is stored in the structure tm which is defined in as follows: + [StructLayout(LayoutKind.Sequential)] + internal struct SystemTime + { + public int tm_sec; + public int tm_min; + public int tm_hour; + public int tm_mday; + public int tm_mon; + public int tm_year; + public int tm_wday; + public int tm_yday; + public int tm_isdst; + + // The glibc version of struct tm has additional fields + public long tm_gmtoff; + public IntPtr tm_zone; + + public static implicit operator SystemTime(DateTime value) + { + SystemTime tm = new SystemTime(); + tm.tm_sec = value.Second; + tm.tm_min = value.Minute; + tm.tm_hour = value.Hour; + tm.tm_mday = value.Day; + tm.tm_mon = value.Month - 1; + tm.tm_year = value.Year - 1900; + tm.tm_wday = (int)value.DayOfWeek; + tm.tm_yday = value.DayOfYear; + tm.tm_isdst = value.IsDaylightSavingTime() ? 1 : 0; + return tm; + } + + public static implicit operator DateTime(SystemTime value) + { + DateTime date = new DateTime(value.tm_year + 1900, value.tm_mon + 1, value.tm_mday, value.tm_hour, value.tm_min, value.tm_sec); + return date; + } + } } } -- 2.34.1