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.

Share this post:

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

4V Services works with development teams to boost their knowledge and capabilities. Contact us today to talk about how we can help you succeed!

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
cta-bg

Looking Forward to Building a Partnership!

Let's discuss how we can help your organization