Me
My name is Laura Otermat. I’m a software engineer. My day job is to work for Adap.tv. In my free time, I like to make things, like this blog.
Stuff
Posts
Geek Silicon Valley: A Travel Guide Review
Clarissa Explains It All Computer Games
Tools
Videos
Friends
Like any other engineer, every year or so, I need to solve a time zone conversion bug. Combining the results from a quick Google search will usually solve any new problem. On the rare occasion that it doesn't, I'll post my solutions here.
Sunday, March 18, 2018Golang Epoch Timezone Conversion
Epoch is supposed to be UTC. But not every company follows that rule. Sometimes a company will break that rule before exposing their API to customers. Then, when the mistake is pointed out, they are loath to change it because other customers rely on the error being consistent. That is how I ended up needing to convert epoch from Eastern time to UTC in Golang. Here's how I did it.
func convertToRealEpoch(unix_timestamp uint64) uint64 { fmt.Println(unix_timestamp) t := time.Unix(0, int64(unix_timestamp) * 1000) fmt.Println(t) loc, _ := time.LoadLocation("America/New_York") t2 := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), loc) fmt.Println("t2", t2) l, _ := time.LoadLocation("UTC") real_epoch := t2.In(l).UnixNano() / 1000 fmt.Println(real_epoch) return uint64(real_epoch) }
Wednesday, October 31, 2012
Java DateTime Timezone Conversion
If you're using joda.org.time.DateTime and want to convert for Timezone, use this method:
private DateTime convertJodaTimezone(DateTime dateTime, String srcTz, String destTz) { LocalDateTime date = new LocalDateTime(dateTime); DateTime srcDateTime = date.toDateTime(DateTimeZone.forID(srcTz)); DateTime dstDateTime = srcDateTime.withZone(DateTimeZone.forID(destTz)); return dstDateTime.toLocalDateTime().toDateTime(); }
Nobody else on my Google search results had pulled it out into a separate method, so I thought I'd put this out there for anyone else having the same problem.