We can use this for QDate, Date64, etc
Testet with:
- MacOS X86 and X64. Thanks to Wilbert
- Linux X86 and X64
- Window X86 and X64
Update v1.02
- Update structure type year to word
Update v1.03
- Added GetSystemDate(...)
Update v1.04
- Change MacOS GetSystemDate(..). Thanks to Wilbert
- Cleanup code
Code: Select all
;-TOP
; Comment: Get loacl date and time as simple structure
; Author : mk-soft
; Version: v1.04
; Created: 26.07.2016
; Updated:
; ***************************************************************************************
DeclareModule DateX
Structure sDateTime
Year.w
Month.b
Day.b
Hour.b
Minute.b
Second.b
EndStructure
Declare GetLocalDate(*DateTime.sDateTime)
Declare GetSystemDate(*DateTime.sDateTime)
EndDeclareModule
Module DateX
EnableExplicit
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_MacOS
; Thanks to Wilbert
ImportC ""
CFAbsoluteTimeGetCurrent.d()
CFCalendarCopyCurrent()
CFCalendarSetTimeZone(*calendar, tz)
CFCalendarDecomposeAbsoluteTime(*calendar, at.d, componentDesc.p-ascii, *year, *month, *day, *hour, *minute, *second)
CFTimeZoneCreateWithTimeIntervalFromGMT(*allocator, ti.d)
EndImport
Procedure GetLocalDate(*DateTime.sDateTime)
Protected Year.integer, Month.integer, Day.integer, Hour.integer, Minute.integer, Second.integer
Protected timeCurrent.d = CFAbsoluteTimeGetCurrent()
Protected *CurrentCalendar = CFCalendarCopyCurrent()
CFCalendarDecomposeAbsoluteTime(*CurrentCalendar, timeCurrent, "yMdHms", @Year, @Month, @Day, @Hour, @Minute, @Second)
CFRelease_(*CurrentCalendar)
With *DateTime
\Year = Year\i
\Month = Month\i
\Day = Day\i
\Hour = Hour\i
\Minute = Minute\i
\Second = Second\i
EndWith
EndProcedure
Procedure GetSystemDate(*DateTime.sDateTime)
Protected Year.integer, Month.integer, Day.integer, Hour.integer, Minute.integer, Second.integer
Protected timeCurrent.d
Protected *CurrentCalendar = CFCalendarCopyCurrent()
Protected *TimeZoneGMT = CFTimeZoneCreateWithTimeIntervalFromGMT(#Null, 0.0)
CFCalendarSetTimeZone(*CurrentCalendar, *TimeZoneGMT)
timeCurrent = CFAbsoluteTimeGetCurrent()
CFCalendarDecomposeAbsoluteTime(*CurrentCalendar, timeCurrent, "yMdHms", @Year, @Month, @Day, @Hour, @Minute, @Second)
CFRelease_(*CurrentCalendar)
CFRelease_(*TimeZoneGMT)
With *DateTime
\Year = Year\i
\Month = Month\i
\Day = Day\i
\Hour = Hour\i
\Minute = Minute\i
\Second = Second\i
EndWith
EndProcedure
CompilerCase #PB_OS_Linux
Structure tm
tm_sec.l ; int seconds after the minute 0-61*
tm_min.l ; int minutes after the hour 0-59
tm_hour.l ; int hours since midnight 0-23
tm_mday.l ; int day of the month 1-31
tm_mon.l ; int months since January 0-11
tm_year.l ; int years since 1900
tm_wday.l ; int days since Sunday 0-6
tm_yday.l ; int days since January 1 0-365
tm_isdst.l; int Daylight Saving Time flag
EndStructure
Procedure GetLocalDate(*DateTime.sDateTime)
Protected time.i, *timeinfo.tm
time_(@time)
*timeinfo = localtime_(@time)
With *DateTime
\Year = *timeinfo\tm_year + 1900
\Month = *timeinfo\tm_mon + 1
\Day = *timeinfo\tm_mday
\Hour = *timeinfo\tm_hour
\Minute = *timeinfo\tm_min
\Second = *timeinfo\tm_sec
EndWith
EndProcedure
Procedure GetSystemDate(*DateTime.sDateTime)
Protected time.i, *timeinfo.tm
time_(@time)
*timeinfo = gmtime_(@time)
With *DateTime
\Year = *timeinfo\tm_year + 1900
\Month = *timeinfo\tm_mon + 1
\Day = *timeinfo\tm_mday
\Hour = *timeinfo\tm_hour
\Minute = *timeinfo\tm_min
\Second = *timeinfo\tm_sec
EndWith
EndProcedure
CompilerCase #PB_OS_Windows
Procedure GetLocalDate(*DateTime.sDateTime)
Protected timeinfo.systemtime
GetLocalTime_(timeinfo)
With *DateTime
\Year = timeinfo\wYear
\Month = timeinfo\wMonth
\Day = timeinfo\wDay
\Hour = timeinfo\wHour
\Minute = timeinfo\wMinute
\Second = timeinfo\wSecond
EndWith
EndProcedure
Procedure GetSystemDate(*DateTime.sDateTime)
Protected timeinfo.systemtime
GetSystemTime_(timeinfo)
With *DateTime
\Year = timeinfo\wYear
\Month = timeinfo\wMonth
\Day = timeinfo\wDay
\Hour = timeinfo\wHour
\Minute = timeinfo\wMinute
\Second = timeinfo\wSecond
EndWith
EndProcedure
CompilerEndSelect
; -----------------------------------------------------------------------------------
EndModule
; ***************************************************************************************
CompilerIf #PB_Compiler_IsMainFile
;-Test
Define DateTime.DateX::sDateTime
DateX::GetLocalDate(DateTime)
Debug "*** Local Date ***"
With DateTime
Debug "Year: " + Str(\year)
Debug "Month: " + Str(\month)
Debug "Day: " + Str(\day)
Debug "Hour: " + Str(\hour)
Debug "Minute: " + Str(\minute)
Debug "Second: " + Str(\second)
EndWith
DateX::GetSystemDate(DateTime)
Debug "*** System Date ***"
With DateTime
Debug "Year: " + Str(\year)
Debug "Month: " + Str(\month)
Debug "Day: " + Str(\day)
Debug "Hour: " + Str(\hour)
Debug "Minute: " + Str(\minute)
Debug "Second: " + Str(\second)
EndWith
CompilerEndIf