It is currently Sat Jan 23, 2021 2:17 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Fast parsing of datetime strings (like YYYYMMDDHHMMSS)
PostPosted: Sat Aug 20, 2016 8:33 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3710
Location: Netherlands
Not as flexible as ParseDate since the offset of each component is hard coded
but the code is easy to adapt to a format like YYYY-MM-DD HH:MM:SS .

You have to make sure yourself the strings contain valid information; no checks are performed. The focus is on speed.
It's much faster as the PB procedure ParseDate and returns a quad value so no problem with dates beyond 2037.

Code:
#DT_Unicode = #True; set to #False for parsing Ascii strings

Macro GetDigits(reg, pos)
  CompilerIf #DT_Unicode
    CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
      !mov reg, [r8 + pos * 2]
    CompilerElse
      !mov reg, [esi + pos * 2]
    CompilerEndIf
    !sub  reg, 0x00300030
    !imul reg, 0x0a000100
  CompilerElse
    CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
      !movzx reg, word [r8 + pos]
    CompilerElse
      !movzx reg, word [esi + pos]
    CompilerEndIf
    !sub  reg, 0x00003030
    !imul reg, 0x0a010000
  CompilerEndIf
  !shr reg, 24
EndMacro

Procedure.q ParseDateTimeStamp(*dt) ; yyyymmddhhiiss
 
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !mov r8, [p.p_dt]
    !lea r9, [pdts_month_table]
  CompilerElse
    !mov eax, [p.p_dt]
    !push esi
    !mov esi, eax
  CompilerEndIf
 
  ; *** get date part ***
  GetDigits(ecx, 0); years (first two digits)
  !imul eax, ecx, 36524
  !shl ecx, 8
  GetDigits(edx, 2); years (last two digits)
  !or ecx, edx
  !imul edx, 365
  !add eax, edx
  GetDigits(edx, 4); months
  !and edx, 15
  ; leap correction
  !cmp edx, 3
  !sbb ecx, 0xfffffc00
  !and ecx, 0x0000fcff
  !bt ecx, 7
  !adc ch, cl
  !shr ecx, 10
  !add eax, ecx
  ; lookup and add month offset
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !movsx ecx, word [r9 + rdx * 2]
  CompilerElse
    !movsx ecx, word [pdts_month_table + edx * 2]
  CompilerEndIf
  !add eax, ecx
  ; add days
  GetDigits(ecx, 6); days
  !add eax, ecx
 
  ; *** get time part ***
  GetDigits(ecx, 8); hours
  !imul ecx, 3600
  GetDigits(edx, 10); minutes
  !imul edx, 60
  !add ecx, edx
  GetDigits(edx, 12); seconds
  !add ecx, edx

  ; combine days and seconds and return result
  !sub eax, 0xafaa9; 1/1/1970 offset (use 0xb2576 for 1/1/2000 offset)
  !mov edx, 86400
  !imul edx
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !shl rdx, 32
    !or rax, rdx
    !add rax, rcx
  CompilerElse
    !add eax, ecx
    !adc edx, 0
    !pop esi
  CompilerEndIf
  ProcedureReturn
  !pdts_month_table:
  !dw 0,0,31,59,90,120,151,181,212,243,273,304,334,0,0,0
EndProcedure

_________________
macOS 10.15 Catalina, Windows 10


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast parsing of datetime strings (like YYYYMMDDHHMMSS)
PostPosted: Sat Aug 20, 2016 8:35 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3710
Location: Netherlands
Example
Code:
Debug ParseDate("%yyyy%mm%dd%hh%ii%ss", "20160820093000")
Debug ParseDateTimeStamp(@"20160820093000")
Debug ParseDateTimeStamp(@"21160820093000")

_________________
macOS 10.15 Catalina, Windows 10


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast parsing of datetime strings (like YYYYMMDDHHMMSS)
PostPosted: Fri Sep 02, 2016 2:17 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Mon May 12, 2008 1:25 pm
Posts: 374
Location: The Netherlands
Thanks for sharing Wilbert!

_________________
Image
Bas Groothedde,
Imagine Programming

I live in a philosophical paradoxal randome filled with enigma's!


Top
 Profile  
Reply with quote  
 Post subject: Re: Fast parsing of datetime strings (like YYYYMMDDHHMMSS)
PostPosted: Fri Sep 02, 2016 7:17 pm 
Offline
Addict
Addict

Joined: Fri Nov 09, 2012 11:04 pm
Posts: 1809
Location: Uttoxeter, UK
@wilbert,

Excellent, thank you for sharing. :D

_________________
DE AA EB


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye