Friday, October 1, 2010

New Time functions

One of the quirky things that I noticed while I was building the Google Calendar integration is that KRL has numerous functions to make decisions based upon time, but no simple way to get the current time.

Furthermore, Google calendar uses a specific time format (RFC 3339) and is very strict about enforcing it.

In response, I wrote several new functions for the time module.

  time:now()

  time:new()

  time:add()

  time:strftime()

  time:atom()

Default Time Format

The default format for a time string is RFC 3339 which looks something like this:

1985-04-12T23:20:50.52Z


Functions


time:now()



  • Current datetime based upon user’s location data
Usage

time:now()

time:now({“tz” : <timezone>)

 


time:new()



  • Create a new RFC 3339 datetime string from a string (allows some flexibility in how the source string is formatted)
Usage

time:new() # Equivalent to time:now()

time:new(<string>)

 


  Valid formats for the datetime source string can be found in ISO8601 (v2000).


time:add()



  • Add (or subtract) a specific number of time units to a source string 
Usage

time:add(<string>,{<unit> : n}) 

 


time:strftime()



  • Convert a datetime string to a different format
Usage

time:strftime(<string>,<format>) 


  Valid format arguments to strftime follow the POSIX strftime conventions.



time:atom()



  • Convert a datetime string to an ATOM compatible format
Usage

time:atom(<string>)

time:atom(<string>,{“tz” : <timezone>}) 


Examples


time:now()

Samples

xTime = time:now() # 2010-10-06T18:11:47Z

xTime = time:now({ # 2010-10-06T18:11:47Z

“tz” : “America/Los_Angeles”

})



time:new()

Samples

time:new(“2010-08-08") # 2010-08-08T00:00:00Z (Date only—defaults to 00:00)

time:new(“67342”) # 1967-12-08T00:00:00Z (Year DayOfYear)

time:new(“2011W206T1345-0600”) # 2011-05-21T19:45:00Z (Year WeekOfYear DayOfWeek)

time:new(“083023Z") # 2010-10-05T08:30:23Z (Time only—defaults to today)


time:add()

Samples

time:add(“2010-08-08",{“weeks” : 5}) # 2010-09-12T00:00:00Z

time:add(“67342”,{“hours”:3}) # 1967-12-08T04:00:00Z

time:add(“2011W206T1345-0600”,{“days”:-65}) # 2011-03-17T13:45:00Z

time:add(“083023Z",{“seconds” : 632}) # 2010-10-06T08:40:55Z


time:strftime()

Samples

time:strftime(xTime,”%F %T”) # 2010-10-06 18:15:24

time:strftime(xTime,”%F”) # 2010-10-06

time:strftime(xTime,”%T”) # 18:19:29

time:strftime(xTime,”%A %d %b %Y”) # Wednesday 06 Oct 2010

time:strftime(xTime,”%c”) # Oct 6, 2010 6:25:55 PM



time:atom()

Samples

time:atom(“2010-10-31") # 2010-10-31T00:00:00Z

time:atom(“2010-10-31",{ # 2010-10-31T06:00:00Z

“tz” : “America/Denver”

})

No comments:

Post a Comment