开发者

KRL: Comparing two timestamps

开发者 https://www.devze.com 2023-04-06 05:16 出处:网络
I have two timestamps created with time:now() (one stored in an app variable from the past, one the current time). I need to find the 开发者_如何学Godifference between them (preferably in minutes). Ho

I have two timestamps created with time:now() (one stored in an app variable from the past, one the current time). I need to find the 开发者_如何学Godifference between them (preferably in minutes). How do I do that?

I've tried this syntax, but the parser didn't like it:

diff = time:now() - original_time;

time:compare() doesn't give me enough information, and time:add() is the opposite of what I need. There don't seem to be any other applicable time functions documented.


The time functions return a time string, not a time object. To calculate time elapsed, you will have to convert your time string into epoch time (seconds since 1970..). Fortunately, epoch time is one of the formats supported by strftime.

foo = time:now();
efoo = time:strftime(foo,"%s);

The minus operator is actually sensitive to a leading whitespace. It's on the list of things to work out of the parser, but I just haven't had time to get to it. Here is a working rule:

rule first_rule {
    select when pageview ".*" setting ()
    pre {
        foo = time:now();
        bar = time:add(foo,{"minutes": -5});
        ebar = time:strftime(bar,"%s");
        efoo = time:strftime(foo,"%s");
        diff = efoo-ebar;
    }
    notify("-5 minutes in seconds", diff) with sticky = true;
}

KRL: Comparing two timestamps

0

精彩评论

暂无评论...
验证码 换一张
取 消