简体   繁体   中英

In KRL How can I get the current year, month, and day?

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?

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

替代文字

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.

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})  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM