Skip to content

Instantly share code, notes, and snippets.

@ssire
Created March 19, 2013 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssire/5195019 to your computer and use it in GitHub Desktop.
Save ssire/5195019 to your computer and use it in GitHub Desktop.
Utility XQuery date manipulation functions
(: ======================================================================
Returns a label (in French or English) to print $date as a month-day
("on March 15") or as a short label ("today", "yesterday",
"the day before yesterday") depending on current dateTime
======================================================================
:)
declare function local:gen-display-date( $date as xs:string, $lang as xs:string ) as xs:string
{
let $today := current-dateTime()
let $dur := $today - xs:dateTime($date)
let $days := days-from-duration($dur)
let $delta := if ($days = 0) then
if (hours-from-duration($dur) > (hours-from-duration(($today - xs:dateTime(current-date()))) - 1)) then
2
else
1
else
$days + 1
return
if ($delta > 3) then
let $m := number(substring($date, 6, 2))
let $d := number(substring($date, 9, 2))
return
if ($lang = 'en') then
let $month := ('Jan', 'Feb', 'March', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')[$m]
return
concat('on ', $month, ' ', $d)
else
let $month := ('jan', 'fév', 'mars', 'avr', 'mai', 'juin', 'juil', 'août', 'sept', 'oct', 'nov', 'déc')[$m]
return
concat('le ', if ($d = 1) then '1er' else $d, ' ', $month)
else
if ($lang = 'en') then
("today", "yesterday", "the day before yesterday")[$delta]
else
("aujourd'hui", "hier", "avant-hier")[$delta]
};
(: ======================================================================
Returns the max of two dateTime strings that may be the empty strings
Returns the empty sequence if both are empty
======================================================================
:)
declare function local:max-date ( $one as xs:string, $two as xs:string ) as xs:dateTime? {
let $d1 := if ($one != '') then xs:dateTime($one) else ()
let $d2 := if ($two != '') then xs:dateTime($two) else ()
return
if (empty($d1)) then
$d2
else if (empty($d2)) then
$d1
else if ($d1 gt $d2) then
$d1
else
$d2
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment