Comparing xs.dateTime values

5 o’clock is 5 o’clock, right?

Well, not once we start thinking about time zones. 17:00:00-05:00 (5pm here on the east coast of the US) is 22:00:00Z in London. But that’s simple enough; this is why we have the time zone expressed as part of date times.

For a recent project, I needed to check whether a xs.dateTime value was current (defined as 15 minutes in this case). MarkLogic provides some functions to help with this.

let refresh = 
  profile.root.retrieved < 
    fn.currentDateTime().subtract(xs.dayTimeDuration("PT15M"));

The retrieved value is recorded in the GMT time zone (so it has “Z” at the end). In this case, the implicit time zone included in the current dateTime is “-05:00” — Eastern Standard Time. Looked good to me, but it turns out that the “<” operator doesn’t factor in time zones.

Happily, there’s a really easy solution to this: MarkLogic’s xs.dateTime comes with comparison operators. The code above should be written like this:

let refresh =
  profile.root.retrieved.lt(
    fn.currentDateTime().subtract(xs.dayTimeDuration("PT15M"))
  );

The take-away is to remember to use the provided functions (.add, .substract, .eq, .ne, .ge, .gt, .le, .lt) when working with xs.dateTime values, rather than relying on standard operators.

Leave a Reply

Your email address will not be published. Required fields are marked *