Page 1 of 1

GetLocalDate not work with x86

Posted: Mon Jul 24, 2017 10:48 pm
by mk-soft
This is a addition vor Wilbert's QDate...
But is not work as X86

Code: Select all

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_MacOS
    ImportC ""
      CFAbsoluteTimeGetCurrent.d()
      CFTimeZoneCopyDefault()
      CFAbsoluteTimeGetGregorianDate.q(timeCurrent.d, *timeZone)
      CFAbsoluteTimeGetGregorianDateSecond.d(timeCurrent.d, *timeZone) As "_CFAbsoluteTimeGetGregorianDate"
      CFRelease(*a)
    EndImport
    
    Structure CFGregorianDateHelp
      Year.l
      Month.b
      Day.b
      Hour.b
      Minute.b
    EndStructure
    
    Structure CFGregorianDate
      StructureUnion
        Date.q
        Detail.CFGregorianDateHelp
      EndStructureUnion
    EndStructure
    
    Procedure GetLocalDate(*Year.word, *Month.byte, *Day.byte, *Hour.byte, *Minute.byte, *Second.byte)
      Protected timeCurrent.d, *timeZone
      Protected LocalDateTime.CFGregorianDate, LocalSecond.d
      timeCurrent = CFAbsoluteTimeGetCurrent()
      *timeZone = CFTimeZoneCopyDefault()
      LocalDateTime\Date = CFAbsoluteTimeGetGregorianDate(timeCurrent, *timeZone)
      localsecond = CFAbsoluteTimeGetGregorianDateSecond(timeCurrent, *timeZone)
      CFRelease(*timeZone)
      With LocalDateTime\Detail
        *Year\w = \Year
        *Month\b = \Month
        *Day\b = \Day
        *Hour\b = \Hour
        *Minute\b = \Minute
      EndWith
      *Second\b = Round(LocalSecond, #PB_Round_Down)
    EndProcedure
    
  CompilerCase #PB_OS_Linux
    
  CompilerCase #PB_OS_Windows
    
CompilerEndSelect

Define.w year
Define.b month, day, hour, minute, second

GetLocalDate(@year, @month, @day, @hour, @minute, @second)
Debug "Year: " + Str(year)
Debug "Month: " + Str(month)
Debug "Day: " + Str(day)
Debug "Hour: " + Str(hour)
Debug "Minute: " + Str(minute)
Debug "Second: " + Str(second)

Re: GetLocalDate not work with x86

Posted: Tue Jul 25, 2017 6:24 am
by wilbert
mk-soft wrote:But is not work as X86
You can use CFCalendar but in this case the calendar components need to be of the integer type.

Code: Select all

ImportC ""
  CFCalendarCopyCurrent()
  CFCalendarDecomposeAbsoluteTime(calendar, at.d, componentDesc.p-ascii, *year, *month, *day, *hour, *minute, *second)
EndImport

Procedure GetLocalDate(*Year, *Month, *Day, *Hour, *Minute, *Second)
  Protected.i CurrentCalendar = CFCalendarCopyCurrent()
  CFCalendarDecomposeAbsoluteTime(CurrentCalendar, CFAbsoluteTimeGetCurrent_(), "yMdHms", 
                                  *Year, *Month, *Day, *Hour, *Minute, *Second)
  CFRelease_(CurrentCalendar)
EndProcedure



Define.i year, month, day, hour, minute, second

GetLocalDate(@year, @month, @day, @hour, @minute, @second)
Debug "Year: " + Str(year)
Debug "Month: " + Str(month)
Debug "Day: " + Str(day)
Debug "Hour: " + Str(hour)
Debug "Minute: " + Str(minute)
Debug "Second: " + Str(second)
Another option is to use the NSDateComponents class.

Code: Select all

CurrentDate = CocoaMessage(0, 0, "NSDate date")
CurrentCalendar = CocoaMessage(0, 0, "NSCalendar currentCalendar")
DateComponents = CocoaMessage(0, CurrentCalendar, "components:", %11111100, "fromDate:", CurrentDate)

Debug CocoaMessage(0, DateComponents, "year")
Debug CocoaMessage(0, DateComponents, "month")
Debug CocoaMessage(0, DateComponents, "day")

Debug CocoaMessage(0, DateComponents, "hour")
Debug CocoaMessage(0, DateComponents, "minute")
Debug CocoaMessage(0, DateComponents, "second")

Re: GetLocalDate not work with x86

Posted: Tue Jul 25, 2017 5:17 pm
by mk-soft
Thanks Wilbert,

works fine :wink: