[xquery-talk] XQuery functions for date calculation/manipulation

Michael Kay mhk at mhk.me.uk
Thu Aug 26 19:37:35 PDT 2004

It's possible to write a function that calculates day-of-the-week using the
facilities available, I use this as an example in my book XPath 2.0
Programmer's Reference (just out):

("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
    [1 + (($date - xs:date("1901-01-06")) div xdt:dayTimeDuration("P1D") mod

It's rather easier in XSLT, which has a format-date() function.

I would personally be a little reluctant to rely completely on the XML
Schema date and time types for handling historical documents. One issue is
the change in calendar - translating historical dates to the proleptic
Gregorian calendar, which is what this requires, is likely in my view to
lead to errors. But perhaps this isn't an issue with your data: if it's all
US material, for example, then the problem probably doesn't arise.

Michael Kay

> -----Original Message-----
> From: talk-bounces at xquery.com 
> [mailto:talk-bounces at xquery.com] On Behalf Of David Sewell
> Sent: 26 August 2004 16:53
> To: talk at xquery.com
> Subject: [xquery-talk] XQuery functions for date 
> calculation/manipulation
> We're going to need to make some decisions about date representations
> that will be partly constrained by the ability of XQuery to do various
> manipulations and arithmetic with calendar dates, and I could use
> guidance on what is and isn't possible with the current specs.
> The basic situation is a large collection of historical 
> documents stored
> in XML that bear dates, where in many cases the exact date is 
> uncertain.
> For example, a letter known to be from 1832 may have a 
> dateline reading
> merely "Sunday". If we want to enable searches on these documents with
> granularity down to the day, we must generate a sequence of 
> all possible
> dates for the document. It could of course be done by hand:
> <date value="1832-01-07|1832-01-14 ... 1832-12-29">Sunday 
> [1832]</date>
> but needless to say that's a lot of work. It's not hard to write a
> function that takes an xs:date and adds xdt:dayTimeDuration("P7DT0M")
> times (1 to 51) to generate the same list. You still need to 
> look up the
> start date manually, however. Is there any way to get XQuery 
> to generate
> "every Sunday in 1832" or "every 2nd Thursday of the month in 
> 1832" in a
> higher-level way? Can it do Gregorian calendar lookups? (I can't see
> that it has a concept of "day of the week" anywhere.)
> David S.
> -- 
> David Sewell, Editorial and Technical Manager
> Electronic Imprint, The University of Virginia Press
> PO Box 400318, Charlottesville, VA 22904-4318 USA
> Courier: 310 Old Ivy Way, Suite 302, Charlottesville VA 22903
> Email: dsewell at virginia.edu   Tel: +1 434 924 9973
> Web: http://www.ei.virginia.edu/
> _______________________________________________
> talk at xquery.com
> http://xquery.com/mailman/listinfo/talk

More information about the talk mailing list