質問

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 difference 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;
}

enter image description here

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top