1

I'm writing a program using Golang (1.17.8), storing information in a MariaDB (10.6.5) database. I often need to store a created_at field inserted as current_timestamp() which is pretty common. My system time is UTC +1.00 (BST) and I'm working with Windows 10 (21H1) MariaDB is set to use system time and from the command line both SELECT NOW() and SELECT CURRENT_TIMESTAMP() output exactly that. Logging the output of the Golang function time.Now() also gives system time. If I insert time.Now() from my program into my MariaDB table it is inserted as UTC Perhaps a simple reason exists for this but I do not know why. I have not ever used a different timezone and as mentioned, MariaDB reports it is using system time anyhow. How can I change this please ?

1 Answer 1

1

As the documentation states, this is normal behavior in the absence of a timezone. Which time zone would you wish to use in your output?

Is your server in the UTC timezone by any chance?

5
  • At the moment, it's in development and as such the server is localhost on a Windows machine and yes, my timezone is UTC. Being a Windows machine is perhaps at least part of the problem here ?... no access to IANA timezone list ? It looks like I expect too much from Now(), of course it's ok when not in daylight saving time because it's plain old UTC but this time of year, local time is +1:00 (BST). I've tried ParseInLocation() using format including (BST) but I have not yet been able to get away from inserting UTC into database. Perhaps need to fix an offset and change it every DST change :( May 8, 2022 at 12:06
  • I've now used ParseInLocation() with a fixed location zone... i.e. loc := time.FixedZone("UTC+1", 3600) unfortunately current_timestamp() insertions are still UTC May 8, 2022 at 13:18
  • I think Now() will always use the server timezone. Since you're still in development, perhaps you can convert the UTC to BST? See this as an example: golangprograms.com/…
    – user15558657
    May 9, 2022 at 14:03
  • Yes, I think you are correct. I will have to simply insert as UTC (there is no big issue there) and when I query, any necessary time offset which might need to be shown within the program will have to be altered within the program. Perhaps it's just me but with MariaDB set to use system time and that being currently set at UTC+1.00 (BST), I (reasonably?) expected current_timestamp() insertions to be at that time. The documentation says GO's Now() returns current local time but it seems to be UTC. I expected it to be local time (BST) because I expected it to take its value from system time. Thx May 9, 2022 at 15:25
  • I don't think there's a definitive answer to this as such, it's one of those 'it's the way it is' issues. The references you provided were really helpful to clarify somewhat, so I'm going to mark up your answer, regards May 9, 2022 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.