1

I'm storing times in UTC at a MongoDB server.

When reading them back, using:

import org.joda.time.DateTime
...
val time_utc: DateTime = dbo.get("time_utc").asInstanceOf[DateTime]

I get times converted to local time zone of the server. How should I read the dates to remain in UTC all the time?

akauppi
  • 17,018
  • 15
  • 95
  • 120
  • Fixed the problem by setting JodaDateTime to run in the UTC time zone, at the start of the server process. DateTimeZone.setDefault( DateTimeZone.UTC ) – akauppi Sep 25 '14 at 12:08

1 Answers1

1

I'm not sure what "JodaDateTime" is, but assuming it's an alias for org.joda.time.DateTime:

val time_utc: JodaDateTime = dbo.get("time_utc").asInstanceOf[JodaDateTime].withZone(DateTimeZone.UTC)

should work.

I think you could also use DateTimeZone.setDefault(DateTimeZone.UTC) to do it universally but I've never used that, and it might be better to just be explicit in the spots where you need it to be in UTC, and not risk breaking other spots where it might be assumed to be in local time.

ryryguy
  • 610
  • 4
  • 15