Skip to content

Instantly share code, notes, and snippets.

@WaxCylinderRevival
Last active October 11, 2016 04:38
Show Gist options
  • Save WaxCylinderRevival/f71d6b6afbd88b642cb862574ad4adb3 to your computer and use it in GitHub Desktop.
Save WaxCylinderRevival/f71d6b6afbd88b642cb862574ad4adb3 to your computer and use it in GitHub Desktop.
Function to convert certain ISO 8601-compliant formats [date(yyyy-mm-dd) or dateTime] to epoch/Unix time
module namespace epoch = "https://gist.github.com/WaxCylinderRevival/ns/xquery/date-to-epoch-time";
declare function epoch:date-to-epoch-time
( $dateString as xs:string? ) as xs:decimal? {
if (empty($dateString))
then ()
else
if (matches($dateString, '^\d{4}-\d{2}-\d{2}$'))
then (xs:dateTime(xs:date($dateString)) - xs:dateTime("1970-01-01T00:00:00-00:00")) div xs:dayTimeDuration('PT1S')
else
(xs:dateTime($dateString) - xs:dateTime("1970-01-01T00:00:00-00:00")) div xs:dayTimeDuration('PT1S')
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment