-3

I wrote a program that returns summary based on current time, but I am expereiancing an issue that current time is in my local format, but time of start and end, which I get from database are in different time zone, so in example instead of event lasting from 16:00 to 23:59 it lasts from 17:00 to 00:59 (one hour difference)

currenttime 2020-02-12 19:59:56.161262078 +0100 CET m=+0.001121940
eventstart 2019-02-13 16:00:00 +0000 UTC
eventend 2019-02-13 23:59:00 +0000 UTC

Is there any way to "parse" eventstart and eventend to be in the same timezone?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • You cannot _parse_ in a specific timezone, but of course you can simply change the timezone _after_ parsing. – Volker Feb 13 '20 at 08:40
  • Yes, but after parsing i get 2020-02-13 09:00:00 +0100 CET 2020-02-13 17:00:00 +0100 CET instead of 16:00 and 8:00, although it is in the same timezone – Ondřej Lachman Feb 13 '20 at 09:40
  • 1
    I have to admit I have no idea what the problem is. – Volker Feb 13 '20 at 10:01
  • "But after parsing i get [...] although it is in the same timezone." Same timezone as what? What value did you parse and how? Does the value you want to parse contain timezone or offset information? Did you use time.Parse or time.ParseInLocation? Also, 16:00:00 +0000 is the same point in time as 17:00:00 +0100. If you want to change the timezone for presentation use [time.Time.In](https://golang.org/pkg/time/#Time.In). Try to come up with a reproducable example. – Peter Feb 13 '20 at 11:23
  • Perhaps [this playground](https://play.golang.org/p/QdkrG2qJ52W) might provide you with a starting point to better demonstrate your issue? – Brits Feb 13 '20 at 20:46
  • Note that CET (Central European Time) *is* one hour ahead of UTC, so all the values you have seen here are correct. https://www.timeanddate.com/time/zones/cet – torek Feb 13 '20 at 22:13

1 Answers1

1

If the event is stored somewhere (in a database), it will be saved as a timestamp from UTC. When you want to display the local time in a different timezone you can use something like this:

startTime := time.Now()
// startTime will be in UTC and the timezone
// will be set to whatever your local timezone is

// load the location you want to display the time in 
loc, _ := time.LoadLocation("Europe/Berlin")

// set timezone and display:
timeToDisplay := startTime.In(loc).String()
fmt.Println(timeToDisplay)
Christian
  • 1,676
  • 13
  • 19