Skip to content

Instantly share code, notes, and snippets.

@CliffordAnderson
Last active August 29, 2016 20:32
Show Gist options
  • Save CliffordAnderson/f2ad41f7e1ad78c3d7c07dff62553c06 to your computer and use it in GitHub Desktop.
Save CliffordAnderson/f2ad41f7e1ad78c3d7c07dff62553c06 to your computer and use it in GitHub Desktop.
Milliseconds since Unix Epoch
declare function local:milliseconds-since-epoch($dateTime as xs:dateTime) as xs:integer
{
let $epoch := xs:dateTime("1970-01-01T00:00:00Z")
let $duration := xs:duration($dateTime - $epoch)
let $days := fn:days-from-duration($duration) * 8.64e+7
let $hours := fn:hours-from-duration($duration) * 3.6e+6
let $minutes := fn:minutes-from-duration($duration) * 60000
let $seconds := fn:seconds-from-duration($duration) * 1000
return xs:integer($days + $hours + $minutes + $seconds)
};
local:milliseconds-since-epoch(fn:current-dateTime())
@joewiz
Copy link

joewiz commented Aug 29, 2016

Very nice! I first saw this in Norm Walsh's OAuth library, at https://github.com/ndw/XQuery-OAuth/blob/master/oauth.xqy#L40, and applied it in my adaptation at https://gist.github.com/joewiz/5929809#file-oauth-xq-L92.

@CliffordAnderson
Copy link
Author

CliffordAnderson commented Aug 29, 2016

@joewiz I figured that I'd be reinventing the wheel, but I couldn't find an example anywhere. Thanks for the pointer to @ndw's elegant version!

@joewiz
Copy link

joewiz commented Aug 29, 2016

Sure! Notice, too, @ndw's use of xs:unsignedLong which might help with very large numbers like 1472502093491.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment