3

I am working on an application in which I need to get the current year, month, and day. Is there a way to get this information in the pre block of a rule?

Can I get this data as a string or a number or both?

There are currently time functions documented on http://docs.kynetx.com/docs/Time but none of them seem to work for what I am trying to do.

Is there a way to set the timezone when getting this data?

Mike Grace
  • 16,636
  • 8
  • 59
  • 79

2 Answers2

4

I was able to do it using strftime which appears to be an undocumented feature so use with caution.

ruleset a60x518 {
  meta {
    name "date-test"
    description <<
      date-test
    >>
    author "Mike Grace"
    logging on
  }

  rule testing {
    select when pageview ".*"
    pre {
      retTime = time:strftime(time:now({"tz":"America/Denver"}), "%c");
      month = time:strftime(time:now({"tz":"America/Denver"}), "%B");
      year = time:strftime(time:now({"tz":"America/Denver"}), "%Y");
      day = time:strftime(time:now({"tz":"America/Denver"}), "%d");
    }
    {
      notify("time",retTime) with sticky = true;
      notify("month",month) with sticky = true;
      notify("year",year) with sticky = true;
      notify("day",day) with sticky = true;
    }
  }
}

App run on example.com twice. Once with the timezone set to New York and onother time set to Denver

alt text

I used this site http://www.statoids.com/tus.html to get the correct strings to use for the timezone. I have no idea if they all work. I just found this site and tried a few and they worked so use with caution.

Mike Grace
  • 16,636
  • 8
  • 59
  • 79
  • Mike, why don't you fix the documentation please and then amend this to point to that. :-) Keep in mind that since KRL is implemented in Perl, it's strfmt is likely the best indicator of what is acceptable. – Phil Windley Dec 28 '10 at 01:45
  • @Phil, because I wasn't sure if it was something that was ready to be documented or if it was temporarily in place to see if it would stay. – Mike Grace Dec 28 '10 at 06:40
3

Perhaps the docs got reverted. For convenience, here is the documentation for strftime:

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.

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

The other time 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})  
MEH
  • 598
  • 4
  • 11