Convert clj-time date-time to UTC

665 views
Skip to first unread message

gvim

unread,
Jun 14, 2014, 10:10:16 AM6/14/14
to clo...@googlegroups.com
I want to convert a time specified with a TZ datababse timezone such as
"America/Caracas" into a UTC date-time but I can only find in clj-time
from-time-zone and to-time-zone allowing the zone to be specified as a
string. I want:

(t/<some-utc-func> (t/date-time 1967 7 31 6 30) (t/time-zone-for-id
"America/Caracas"))

to give me:

#<DateTime 1967-07-31T10:30:00.000-00:00>

Even the (t/from-time-zone []) output would do if I could read the UTC
date and time straight from it.

gvim

Stephen Gilardi

unread,
Jun 14, 2014, 11:13:09 AM6/14/14
to clo...@googlegroups.com
This is not quite to your exact specification, but should help you to write what you want:

user> (defn to-utc [dt]
(t/to-time-zone dt (t/time-zone-for-offset 0)))
#'user/to-utc
user> (to-utc (t/from-time-zone (t/date-time 1967 7 31 6 30) (t/time-zone-for-id "America/Caracas")))
#<DateTime 1967-07-31T10:30:00.000Z>

—Steve

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

gvim

unread,
Jun 14, 2014, 12:50:16 PM6/14/14
to clo...@googlegroups.com
On 14/06/2014 16:12, Stephen Gilardi wrote:
> This is not quite to your exact specification, but should help you to
> write what you want:
>
> user> (defn to-utc [dt]
> (t/to-time-zone dt (t/time-zone-for-offset 0)))
> #'user/to-utc
> user> (to-utc (t/from-time-zone (t/date-time 1967 7 31 6 30)
> (t/time-zone-for-id "America/Caracas")))
> #<DateTime 1967-07-31T10:30:00.000Z>
>

Thanks. Can't think why it's not baked into the library, though, as it
must be a common requirement.

gvim

Stephen Gilardi

unread,
Jun 14, 2014, 12:59:53 PM6/14/14
to clo...@googlegroups.com
You’re welcome.

As another small refinement, I noticed that there’s a var for the utc timezone:

(t/time-zone-for-offset 0)

can be replaced with

t/utc

—Steve

gvim

unread,
Jun 14, 2014, 1:04:50 PM6/14/14
to clo...@googlegroups.com
On 14/06/2014 17:59, Stephen Gilardi wrote:
> You’re welcome.
>
> As another small refinement, I noticed that there’s a var for the utc
> timezone:
>
> (t/time-zone-for-offset 0)
>
>
> can be replaced with
>
> t/utc
>

I tried t/utc in place of your function but it didn't produce the
desired result.

gvim

Michael Klishin

unread,
Jun 14, 2014, 5:00:38 PM6/14/14
to clo...@googlegroups.com
2014-06-14 20:49 GMT+04:00 gvim <gvi...@gmail.com>:
Can't think why it's not baked into the library, though, as it must be a common requirement.

Feel free to submit a pull request and a few tests. The clj-time maintainers are responsive
and the library is primarily driven by user feedback at this point.
--
MK

http://github.com/michaelklishin
http://twitter.com/michaelklishin

gvim

unread,
Jun 14, 2014, 9:06:41 PM6/14/14
to clo...@googlegroups.com
On 14/06/2014 21:59, Michael Klishin wrote:
> 2014-06-14 20:49 GMT+04:00 gvim <gvi...@gmail.com
> <mailto:gvi...@gmail.com>>:
>
> Can't think why it's not baked into the library, though, as it must
> be a common requirement.
>
>
> Feel free to submit a pull request and a few tests. The clj-time
> maintainers are responsive
> and the library is primarily driven by user feedback at this point.
> --
> MK

I'll have to leave it to the gods as I'm a mere mortal :)

gvim
Reply all
Reply to author
Forward
0 new messages