Parse any date time value

Introduction

This example shows how to use the date.parse.lua fuzzy date/time parser module to translate a wide variety of date/time formats.

The date.parse.parse() function is pretty smart, but it can’t predict everything, to address this we allow you to create custom date formats.

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

Using the Code [top]

  • Import the Parse any date time value channel from the Builtin: Iguana Date/Time repository
  • Experiment with the code to find out how it works
  • Then add the module to your Translator project
  • Copy the require statement from the channel and add it at the top of your script
    Note: This module uses require to return a table and also modifies the global namespace
  • Adapt the code to your own requirements
  • You can also use date.parse.parse() to parse dates
  • Interactive scripting help is included for this module

This is the github code for the main module:

Using other date.parse functions:

Here are some code snippets that demonstrate the other functions in this module.

The code should work immediately if you paste it into the main module – remember to do a source code commit first, so you undo the change).

Here are the three node methods :D(), :T(), and :TS():

dateparse = require 'date.parse'
-- date.parse automatically translates a wide variety of date/time formats
-- http://help.interfaceware.com/datetime-conversion-using-the-fuzzy-datetime-parser.html

function main(Data)
   local Msg = hl7.parse{data=Data, vmd='demo.vmd'}
   local T = db.tables{vmd='demo.vmd', name='ADT'}
   
   local Dob = Msg.PID[7][1]
   trace(Dob)      --> node containing '19830711183045'
   
   -- date compatible with database table node tree
   trace(Dob:D())  --> '1983-07-11 00:00:00' 
   
   -- datetime compatible with database table node tree
   trace(Dob:T())  --> '1983-07-11 18:30:45'
   
   -- HL7 timestamp - not compatible with db table
   trace(Dob:TS()) --> '19830711183045'
end

And here is some code using date.parse.parse():

dateparse = require 'date.parse'
-- date.parse automatically translates a wide variety of date/time formats
-- http://help.interfaceware.com/datetime-conversion-using-the-fuzzy-datetime-parser.html

function main(Data)
   local Msg = hl7.parse{data=Data, vmd='demo.vmd'}
   local T = db.tables{vmd='demo.vmd', name='ADT'}

   local Dob = Msg.PID[7][1]
   trace(Dob)      --> node containing '19830711183045'
   
   -- returns date as Unix Epoch time value and as a table
   local t, d = dateparse.parse(Dob:nodeValue())
   trace(t, d)
   
   -- convert t (unix epoch time) to a string like :D()
   os.date('%Y-%m-%d 00:00:00',t) --> '1983-07-11 00:00:00'
   -- produces the same result
   Dob:D()                        --> '1983-07-11 00:00:00'

   -- using a custom date format
   t, d = dateparse.parse("2PM March 5th '77", "H[:MM]tt mmmm dw [']yy[yy]")
   tostring(t):T()  --> '1977-03-05 14:00:00'
   tostring(t):D()  --> '1977-03-05 00:00:00'
   tostring(t):TS() --> '19770305140000'
end

How it works [top]

The date.parse.parse() function recognizes the supplied format, and returns two values: first a unix epoch time value, and second a table with the date/time components. The time value can be compared with the Lua routine os.difftime() or formatted as text with os.date().

The component table has various fields usable with Lua routines (i.e., year, month, day, hour, min, sec etc), as well as fields for partial seconds (sec_fraction) and time zones (tz and tz_offset).

We also supply three node functions for your convenience, :D(), :T() and :TS(), to return dates in common formats. All of these functions date.parse.parse() to recognize the dates and os.date() to format them.

If you have other formats that you use a lot it is easy to create similar functions:

More information [top]

Leave A Comment?