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

Introduction

Sometimes you might come across unusual Date/Time formats that the fuzzy date/time parser does not recognize. This article shows how to handle one such example — you can easily adapt the example for your own use.

If you have any questions please contact us at support@interfaceware.com.

Task [top]

How to handle unusual Date/Time formats that the date/time parser does not recognize.

Implementation [top]

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
en

More information [top]

Leave A Comment?

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.