Comparing xs.dateTime values

Blog

Comparing xs.dateTime values

  • 20 February, 2021
  • By Dave Cassel
  • No Comments
blog-image

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 takeaway 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.

quote
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...
0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
cta-bg

Looking Forward to Expanding Our Horizon!

Let us know how we can help your organization