Calculate the age of a FILETIME/SYSTEMTIME in day/hr/min/sec

Share your advanced PureBasic knowledge/code with the community.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Calculate the age of a FILETIME/SYSTEMTIME in day/hr/min/sec

Post by Keya »

This procedure returns a word-based age of a FILETIME, for example "3days 7hrs 21mins".
It is 'smart' in that it only shows seconds if the time is brief enough, only shows hours/days if old enough, and knows when to use single or plural ("min/mins").
I've only made it go up to Days (2years is reported as 730days), but it's very simple to extend for Weeks/Months/Years.

FILETIME and SYSTEMTIME are of course interchangeable so you can use either, but convert to FILETIME before calling my procedure :)
WinAPI helpers:
FileTimeToSystemTime_(ft.FILETIME, st.SYSTEMTIME)
SystemTimeToFileTime_(st.SYSTEMTIME, ft.FILETIME)
FileTimeToLocalFileTime_(ft.FILETIME, ft.FILETIME)

Sample output:

Code: Select all

Age             Example
 0-59 secs       32secs
 1-59 mins       21mins 32secs
 1-23 hours      7hrs 21mins
 24+ hours       3days 7hrs 21mins

Code: Select all

Procedure.s AgeFILETIME(ftold)  ;Returns string describing age (from Now) of FILETIME
  Protected qold.q, qnow.q, qSecs.q, Hrs.l, Mins.l, Secs.l, Days.l, ftnow.FILETIME, stnow.SYSTEMTIME
  Protected sAge.s, sDays.s, sHrs.s, sMins.s, sSecs.s
  GetLocalTime_(@stnow)
  SystemTimeToFileTime_(@stnow, @ftnow)
  qnow = PeekQ(ftnow)
  qold = PeekQ(ftold)
  qSecs = ((qnow - qold) / 10000) / 1000
  Hrs = qSecs / 3600
  Mins = (qSecs - (Hrs * 3600)) / 60
  Secs = (qSecs % 3600) % 60
  If Hrs > 23
    Days = Hrs / 24
    Hrs = Hrs % 24
  EndIf    
  If Days = 1
    sDays = Str(Days) + "day"
  Else
    sDays = Str(Days) + "days"
  EndIf
  If Hrs = 1
    sHrs = Str(Hrs) + "hr"
  Else
    sHrs = Str(Hrs) + "hrs"
  EndIf
  If Mins = 1
    sMins = Str(Mins) + "min"
  Else
    sMins = Str(Mins) + "mins"
  EndIf  
  If Secs = 1
    sSecs = Str(Secs) + "sec"
  Else
    sSecs = Str(Secs) + "secs"
  EndIf   
  If Days > 0      ;=> 24hrs
    sAge = sDays + " " + sHrs + " " + sMins       ;eg. "3days 7hrs 21mins"
  Else         
    If Hrs < 1  
      If Mins < 1  ;<1min
        sAge = sSecs                              ;eg. "32secs"
      Else         ;1-59min
        sAge = sMins + " " + sSecs                ;eg. "21mins 32secs"
      EndIf
    Else           ;1hr-23hrs
      sAge = sHrs + " " + sMins                   ;eg. "7hrs 21mins"
    EndIf
  EndIf  
  ProcedureReturn sAge
EndProcedure



Define time_create.FILETIME, time_access.FILETIME, time_write.FILETIME, st.SYSTEMTIME
;The _OLD_ FILETIME to compare against.     This example simply uses an existing file.
sFilename.s = ProgramFilename()  ;"c:\windows\system32\notepad.exe"
hFile.l = CreateFile_(sFilename, 0, #FILE_SHARE_READ, 0, #OPEN_EXISTING, #FILE_ATTRIBUTE_NORMAL, 0)
If GetFileTime_(hFile,time_create, time_access, time_write) 
  FileTimeToLocalFileTime_(time_create,time_create)
  FileTimeToSystemTime_(time_create,st)                            
  ;Display the old date, as well as calculate its age
  Debug(sFilename + " created " + Str(st\wDay) + "/" + Str(st\wMonth) + "/" + Str(st\wYear) + " " + RSet(Str(st\wHour),2,"0") + ":" + RSet(Str(st\wMinute),2,"0") + ":" + RSet(Str(st\wSecond),2,"0") +  
        " (" + AgeFILETIME(time_create) + " ago)" )  
Else
  Debug("Couldnt read filetime")
EndIf
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Calculate the age of a FILETIME/SYSTEMTIME in day/hr/min

Post by Kwai chang caine »

Can be usefull
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
Post Reply