What if … Date/Time is too fuzzy for date/time parser?

Just recently I came across unusual (for HL7) Date/Time timestamp.

Example: 08/03 2011 11:54

i.e., dd/mm YYYY HH:MM

Here is simple way to handle it:

We use two extensions to string library, string:zfill() and string:tokenize(), both added to the shared dateutil Module.

Last but not least – the code snippets 🙂

main:

require 'dateutil'

local function WeirdDateTime(s)
   local t
   t = s:tokenize()
   return t[3]..
          t[2]:zfill(2)..
          t[1]:zfill(2)..
          t[4]:zfill(2)..
          t[5]:zfill(2)
end

function main()
   s = '8/3 2010 11.34'
   dt = WeirdDateTime(s)
end

dateutil:

function string.tokenize(self)
   t  = {}
   for w in string.gmatch(self, "(%w+)" ) do
      t[#t+1] = w
   end
   return t
end

function string.zfill(self,n)
   local R = n
   local L = #self
   if L < R then 
      self = '0'..self
      self:zfill(R)
   else
      z = self   
   end

   return z
end

Leave A Comment?