5

I'm pulling a timestamp from a datebase, it is using RFC3339 format but the timezone is missing. So when I try to compare the timestamps it will be off. How do I change dbtime to be Eastern time?

// time format
const
(
    RFC3339     = "2006-01-02T15:04:05Z07:00"
)

//now time
now := time.Now()
nowtime := now.Unix()

fmt.Println("Nowtime:", nowtime)
fmt.Println("Now:", now)

//time in db
fmt.Println("Dbtime string:", dbtime)
udbtime, err := time.Parse.EST(RFC3339,dbtime)
fmt.Println("RFC3339: " + RFC3339)
fmt.Println("dbtime parsed", udbtime)
fmt.Println("dbtime parsed unixtime", udbtime.Unix())

My output is

Nowtime: 1466443640
Now: 2016-06-20 13:27:20.963232824 -0400 EDT
Dbtime string: 2016-06-20T12:41:45.14Z
RFC3339: 2006-01-02T15:04:05Z07:00
dbtime parsed 2016-06-20 12:41:45.14 +0000 UTC
dbtime parsed unixtime 1466426505
7
  • Give an example of the dbtime you're parsing. Commented Jun 20, 2016 at 17:35
  • 2
    The timezone isn't missing, the time 2016-06-20T12:41:45.14Z is UTC. Commented Jun 20, 2016 at 17:37
  • Sorry the original timestamp. is 2016-06-20T12:41:45.14Z but its actually in EDT. Commented Jun 20, 2016 at 17:40
  • So you want to change the timezone? Commented Jun 20, 2016 at 17:42
  • 2016-06-20T12:41:45.14Z can't be EDT, because the trailing Z specifies UTC, so you need to fix the times yourself. Commented Jun 20, 2016 at 17:44

2 Answers 2

4

you can try go-carbon,a simple,semantic and developer-fridendly golang package for datetime

carbon.Parse("2020-12-31").ToRfc3339String() 
//  output
2020-12-31T00:00:00+08:00

https://github.com/golang-module/carbon

Sign up to request clarification or add additional context in comments.

Comments

3

You could parse the timestamps using the defined time.RFC3339 like you've shown, and adjust them after the fact by adding the UTC offset so that they are correct in UTC.

udbtime = udbtime.Add(4*time.Hour)

This however requires that you check whether each time falls in EST or EDT to add the correct offset, unless you can assume they are all EDT.

A better way would be to use your own time format without a specific TZ offset (the Z isn't part of the time specification for parsing), and parse it with time.ParseInLocation.

This way you can account for the correct offset depending on whether the time would have been in EST or EDT.

https://play.golang.org/p/IrUnTwvlkk

RFC3339local := "2006-01-02T15:04:05Z"

loc, err := time.LoadLocation("America/New_York")
if err != nil {
    log.Fatal(err)
}

ts1 := "2016-06-20T12:41:45.14Z"
t1, _ := time.ParseInLocation(RFC3339local, ts1, loc)
fmt.Println(t1)
// prints: 2016-06-20 12:41:45.14 -0400 EDT

ts2 := "2016-01-20T12:41:45.14Z"
t2, _ = time.ParseInLocation(RFC3339local, ts2, loc)
fmt.Println(t2)
// prints: 2016-01-20 12:41:45.14 -0500 EST

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.