Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


Info

time:atom(), time:compare(), and time predicates have not yet been ported to the new pico engine.


The time library provides functions for determining the time and manipulating time values. KRE tries to determine the user's location and creates a time object that is localized for the user (i.e., in the user's time zone). The following functions are available:

  • time:now() – returns the current date and time based as a ISO8601 string (ex. "2017-03-08T22:45:18.887Z") based upon the user's location data.
  • time:new() – returns a new RFC 3339 datetime string from a string formatted as described in ISO8601 (v2000).
  • time:add() – adds (or subtracts) a specific number of time units to a source string.

  • time:strftime() – returns a datetime string in a specified format following POSIX strftime conventions.
  • time:atom() – converts a datetime string to an ATOM compatible form.
  • time:compare() – takes two datetime strings (ISO8601) and returns 1, 0, or [ms]-1, depending on whether the first is before, the same, or after the second.

...

time:now() - returns the current date based upon the user's location dataand time based on a time zone.

Code Block
languagejavascript
themeConfluence
languagejavascript
xTime = time:now()		# 2010-10-06T18:11:47Z
 
xTime = time:now({		# 2010-10-06T18:11:47Z
	"tz" : "America/Los_Angeles"
})

The default time zone is determined by the operating system running Node and is used if an invalid time zone is provided. The list of valid time zones is updated periodically — for example, version 0.40.0 of the core engine used IANA TZDB 2017b.

time:new()

time:new() - returns a new RFC 3339 datetime string from a string formatted as described in ISO8601 (v2000). It assumes UTC unless otherwise specified.

Code Block
languagejavascript
themeConfluence
languagejavascript
time:new("2010-08-08")            # 2010-08-08T00:00:00Z (Date only—defaults to 00:00)
time:new("673421967342")                 # 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() - adds (or subtracts) a specific number of time units to a source string, assuming UTC unless otherwise specified.

Code Block
languagejavascript
themeConfluence
languagejavascript
time:add("2011W206T1345-0600",{"days": -65})    # 2011-03-17T13:45:00Z
time:add("2010-08-08",{"weeks" : 5})              # 2010-09-12T00:00:00Z
time:add("673421967342",{"hours": 3})                    # 1967-12-08T04:00:00Z
time:add("2011W206T1345-0600083023Z",{"daysminutes": -6510})             # 20112010-0310-17T1306T08:4540:00Z23Z
time:add("083023Z1970-01-01",{"seconds" : 6321286388924})  # 2010-10-06T18:15:24Z
time:add("1967342", {"milliseconds": 5})        # 20101967-1012-06T0808T00:4000:55Z00.005Z
time:add("1970-01-011967342", {"secondsms": 12863889245})                  # 20101967-1012-06T18:15:24Z08T00:00:00.005Z

Unrecognized time units are ignored. The exact rules can get complicated considering leap years, different month lengths, and adding (or subtracting) multiple units at once versus one after another. See http://momentjs.com/docs/#/manipulating/add/ (the time module uses a version above 2.18.0). UTC does not have daylight savings time.

time:strftime()

time:strftime() - returns a datetime string in a specified format following POSIX strftime conventions. It assumes the default time zone of the operating system running Node.

Code Block
languagejavascript
themeConfluence
languagejavascript
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:strftime(xTime, "%s")             # 1286388924 -- seconds since epoch

Valid format arguments to strftime follow the POSIX strftime conventions. If the format string is invalid, the format string is returned as is.


Note

The third argument below is not yet implemented.

time:strftime() takes an optional third argument that is used to specify the timezone of the resulting formatted string. This can be used, among other things, to simply convert the timezone by returning an ISO formatted datetime string. For example, the following function uses time:strftime() to convert an incoming datetime string to UTC:

Code Block
languagejavascript
themeConfluence
convertToUTC = function(dt) {
  time:strftime(dt, "%Y%m%dT%H%M%S%z", {"tz":"UTC"})
};


time:atom()

time:atom() - converts a datetime string to an ATOM compatible form.

Code Block
languagejavascript
themeConfluence
languagejavascript
time:atom("2010-10-31")        # 2010-10-31T00:00:00Z
time:atom("2010-10-31",{       # 2010-10-31T06:00:00Z
    "tz" : "America/Denver"
})

Time Predicates

All of the following predicates are referenced to the user's local time. These predicates depend on knowing timezone context for the event or query that causes them to run. The timezone context can be specified via a reserved event attribute, _timezone. If _timezone is not specified for an event UTC is used. 

All times are 24-hour clock. All  

All predicates must be preceded by the modules name, for example:

...

timezone(<arg>) - takes a timezone abbreviation as the argument and checks the user's geo-location (based on IP address) and returns true or false

Code Block
languagejavascript
themeConfluencelanguagejavascript
 rule glee_check {
   select when pageview ".*"
   if (time:timezone("MST")) then {
     notify("Glee alert", "Glee starts at 7pm");
   }
 }

daytime

time:daytime() - returns true if it is between sunrise and sunset, otherwise returns false.

nighttime

...

morning

time:morning() - returns true if it is between 0600 and 1200 hours, otherwise returns false.

...