Confirmation on the correct method of time conversion on specific timezone?

I have my codes here Go Playground - The Go Programming Language. I just would like to confirm is this the right method to convert a particular time to specific time zone. Is there any method where say I got +08:00 where I can directly apply it ? Another thing what should be the correct value to be enter into example this formatting line fmt.Println(mstTime.Format(“2006-01-02 05:04:05”)) cause when I change some values the output seems to be different ?

The time formatting/parsing layout string is described at time package - time - Go Packages. Each digit references a different field. 1=Month, 2=Date, 6=Year, etc.

Hi Jeff,
So according to the link its predefined format and thats how we should be using it right ? How about this is there any method where say I got +08:00 where I can directly apply it ?

What do you mean by “directly apply it?”

Hi Sean,
What I meant is this e.g.

tzOffset := “+08:00”
loc := time.FixedZone(tzName, tzOffset)
So I dont need to convert it to 86060 ? Is that possible with the direct apply.

Not that I know of. I suppose if you don’t like the “magic numbers,” you could use time.Duration constants:

tzOffsetSeconds := 8 * time.Hour / time.Second
loc := time.FixedZone(tzName, tzOffsetSeconds)

Edit: If you need to do this timezone conversion in multiple places, you could also move the code into its own variable and function: Go Playground - The Go Programming Language

Hi Sean,
Thank you for the confirmation then I will stick to your latest suggestion of codes cause some of time zone are has e.g. +5.30 etc.

Can you provide a bit more information around these time zones? Why are you using custom time zones instead of, e.g. the IANA time zones like “Asia/Hong_Kong” for China Standard Time, or “Asia/Kuching” (both UTC+08:00) or “Asia/Kolkata” for India Standard Time or “Asia/Colombo” for Sri Lanka Standard Time (both UTC+05:30), etc.?

Offsets are not timezones, and timezones are not offsets.

+0200 means the same thing, be it in August or February.

Europe/Berlin means something different depending on the time of the year.

That’s why I’m asking; I’m probing to see if this is an XY problem :slight_smile:. Of course if @newbiegolang really means UTC+08:00, UTC+05:30, etc., then so be it!

Hi Guys,
Yes my issue is it fix without any daylight savings etc. So I need to convert to according UTC+08:00, UTC+05:30. So anything best solution besides what I have posted ? Thank you.

How do you know if you have to shift a parsed strings time into +8 or +5:30 if it is not part of the input?

Hi Nobbz,
What I was thinking example for +5:30 I break into two strings +5 the hours and 30 as the minutes then I convert it accordingly and put into the function? Correct me if I am wrong here.

My question was, how do you know what offset to use? If it is not part of the input, how do you know? If it is part of the input, why not parse it as +07:00 in the template?

Hi Nobz,
Sorry that I will run a sql query and based on the query I have set for example for each user a particular timezone. I know I have not shown the query here to over complicate things.