Date Extended

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
kpeters58
Enthusiast
Enthusiast
Posts: 341
Joined: Tue Nov 22, 2011 5:11 pm
Location: Kelowna, BC, Canada

Re: Date Extended

Post by kpeters58 »

This poorly thought out Unix/Epoch time thing should have

- never been invented
- never been used by PB

If I cannot even easily handle my own birthdate via the dategadget ....... :(
PB 5.73 on Windows 10 & OS X High Sierra
normeus
Enthusiast
Enthusiast
Posts: 470
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: Date Extended

Post by normeus »

To set the Gadget Calendar to an "old" date You would need a procedure like this:

Code: Select all

Procedure.q SetDate64(gadget, year, month, day, hour=0, minute=0, second=0) 
  Protected sTime.SYSTEMTIME
  Protected fTime.FILETIME
  sTime\wYear = year 
  sTime\wMonth = month 
  sTime\wDay = day 
  sTime\wHour = hour 
  sTime\wMinute = minute 
  sTime\wSecond = second 
  SystemTimeToFileTime_(sTime, @fTime) 
  FileTimeToSystemTime_(fTime, @sTime) 
  SendMessage_(GadgetID(gadget), #DTM_SETSYSTEMTIME, #GDT_VALID, sTime)
EndProcedure 

If OpenWindow(0, 0, 0, 250, 200, "CalendarGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    CalendarGadget(0, 10, 10, 230, 180)
       Setdate64(0,1949,12,12)  ; This is and OLD Date! 
    Repeat
    Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf

now if you think thats easy enough get the full date64 library from german forums ( it might be here too but I havent looked for it )

http://www.purebasic.fr/german/viewtopi ... 20#p334520

This is a module you can include in your programs. you'll need to add these procedures to handle gadgets:
first inside the declare module just before EndDeclareModule add

Code: Select all

Declare.q LocalDateAsUTC64()  ; extra function to get local time
;- FUNCTIONS FOR GADGETS THAT USE date()
Declare.q SetDate64(gadget, year, month, day, hour, minute, second)
Declare  SetDateQ64(gadget,qt.q)
Declare  GetDate64(gadget,part=#PB_Date_Year)
Declare.q GetDateQ64(gadget)
now at the end of the module add these procedures:

Code: Select all

Procedure.q SetDate64(gadget, year, month, day, hour, minute, second) 
  Protected sTime.SYSTEMTIME
  Protected fTime.FILETIME
  sTime\wYear = year 
  sTime\wMonth = month 
  sTime\wDay = day 
  sTime\wHour = hour 
  sTime\wMinute = minute 
  sTime\wSecond = second 
  SystemTimeToFileTime_(sTime, @fTime) 
  FileTimeToSystemTime_(fTime, @sTime) 
  SendMessage_(GadgetID(gadget), #DTM_SETSYSTEMTIME, #GDT_VALID, sTime)
EndProcedure 
Procedure SetDateQ64(gadget,qt.q)
  Protected sTime.FILETIME
    qt = qt * #HundredNanosecondsInOneSecond + #HundredNanosecondsFrom_1Jan1601_To_1Jan1970
  FileTimeToSystemTime_(@qt,sTime)
 SendMessage_(GadgetID(gadget), #DTM_SETSYSTEMTIME, #GDT_VALID, sTime)
EndProcedure

Procedure GetDate64(gadget,part=#PB_Date_Year)
  Protected   sTime.SYSTEMTIME
  Protected fTime.FILETIME
  Protected result
  
   SendMessage_(GadgetID(gadget),#DTM_GETSYSTEMTIME,#GDT_VALID,sTime)
   SystemTimeToFileTime_(sTime,@fTime) 
   FileTimeToSystemTime_(fTime,@sTime)
   Select part
      Case   #PB_Date_Day
         result=sTime\wDay
      Case   #PB_Date_Month
        result=sTime\wMonth
      Case   #PB_Date_Hour
      Default
         result=sTime\wYear
   EndSelect
   ProcedureReturn result
EndProcedure
Procedure.q GetDateQ64(gadget)
  Protected   sTime.SYSTEMTIME
  Protected fTime.FILETIME
   SendMessage_(GadgetID(gadget),#DTM_GETSYSTEMTIME,#GDT_VALID,sTime)
   SystemTimeToFileTime_(sTime,@fTime) 
   FileTimeToSystemTime_(fTime,@sTime)
   ProcedureReturn (PeekQ(@fTime) - #HundredNanosecondsFrom_1Jan1601_To_1Jan1970) / #HundredNanosecondsInOneSecond
EndProcedure

  Procedure.q LocalDateAsUTC64()
    CompilerIf #PB_Compiler_OS = #PB_OS_Windows
      Protected.SYSTEMTIME st
      Protected.FILETIME   ft
      GetSystemTime_(@st)
      SystemTimeToFileTime_(@st, @ft)
      ProcedureReturn (PeekQ(@ft) - #HundredNanosecondsFrom_1Jan1601_To_1Jan1970) / #HundredNanosecondsInOneSecond
    CompilerElse
      ProcedureReturn time_(0)
    CompilerEndIf
  EndProcedure
after this you should be able to handle OLD dates, NEW dates, mortgage dates ( 50 years! of debt for some ) or day you were born ( not this century for me ) etc...
I know it craps out on dates before 1400's in wester calendar but I am happy if it gets 1492 to 2400 right.
Yes it should be native to the language but it is not.
Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
User avatar
langinagel
Enthusiast
Enthusiast
Posts: 131
Joined: Fri Jan 28, 2005 11:53 pm
Location: Germany
Contact:

Re: Date Extended

Post by langinagel »

Läuft nicht unter Mac
Translation: does not run on Mac.

therefore:
+1
https://www.doerpsoft.org

Boost. Work. Efficiency.
Post Reply