Converting UTC time

Post your Gambas programming questions here.
Post Reply
toyman61
Posts: 4
Joined: Friday 26th April 2024 7:19am

Converting UTC time

Post by toyman61 »

I'm reading info from a file and get date and time information on the form "YYYY-MM-DD"T"HH:MM:SS"Z (an example: 2024-04-27T09:25:00Z).
Now I need to adjust this time according to my local timezone.
I know that I can read the UTC time from my system with the shell-command "timedatectl", but how do I adjust the time to my local time ?
Any help would be appreciated.
User avatar
BruceSteers
Posts: 1591
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Converting UTC time

Post by BruceSteers »

See Format() command
https://gambaswiki.org/wiki/lang/format

Also see Date() command as you don't really want to be running shell timedatectl when gambas has built in date functions.
https://gambaswiki.org/wiki/lang/date

There is lots of info in the gambas help system. https://gambaswiki.org/wiki
If at first you don't succeed , try doing something differently.
BruceS
toyman61
Posts: 4
Joined: Friday 26th April 2024 7:19am

Re: Converting UTC time

Post by toyman61 »

But the Date()-function does not contain any information about which timezone I'm in and that is essential to me.

The UTC-time read from file is in another timezone than my local time and I need to adjust the read time to my timezone before I can use it in my program.
User avatar
BruceSteers
Posts: 1591
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Converting UTC time

Post by BruceSteers »

and Format?
Format will convert local time from UTC

And if you need to show the timezone there is t or tt
https://gambaswiki.org/edit/cat/userformat#t4
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1591
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Converting UTC time

Post by BruceSteers »

So something like this...
to convert your "YYYY-MM-DD"T"HH:MM:SS"Z format..

gambas expects utc dates to be american format and time separated by a space.

so to make it gambas compliant Date object we need to remove the T and Z and jumble the date values around to be like
"mm/dd/yyyy hh:nn:ss"


Public Sub Form_Open()

  Print ConvertUTCDate("2020-02-25T20:15:10Z")

End

Public Sub ConvertUTCDate(sDate As String) As String

  Dim hDate As Date

  ' turn your time format into a date object
  Dim sDay As String = Split(sDate, "T")[0]
  Dim sTime As String = Left(Split(sDate, "T")[1], -1)  ' get Time part and remove the Z
  Dim aYMD As String[] = Split(sDay, "-")

  ' convert date values to american format by reversing and swapping month/day
  aYMD = aYMD.Reverse()
  Swap aYMD[0], aYMD[1]

  Dim s As String = aYMD.Join("/") & " " & sTime  ' now make a non localised date object
  hDate = CDate(s)

  Return Format(hDate, "yyyy-mm-ddThh:nn:sst")

End


Not sure if that's exactly what you want but should help you towards getting to the correct data.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
cogier
Site Admin
Posts: 1128
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Converting UTC time

Post by cogier »

I'm reading info from a file and get date and time information on the form "YYYY-MM-DD"T"HH:MM:SS"Z (an example: 2024-04-27T09:25:00Z).
What does this date refer to? It looks like it's the file date. What time zones are involved?
vuott
Posts: 264
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Converting UTC time

Post by vuott »

toyman61 wrote: Friday 26th April 2024 10:24am But the Date()-function does not contain any information about which timezone I'm in and that is essential to me.

The UTC-time read from file is in another timezone than my local time and I need to adjust the read time to my timezone before I can use it in my program.
Maybe this can help you:
https://gambaswiki.org/wiki/comp/gb/system/timezone
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
toyman61
Posts: 4
Joined: Friday 26th April 2024 7:19am

Re: Converting UTC time

Post by toyman61 »

Thanks for all replies!

I just wanted to get the timezone offset so that I could adjust the hour parameter in the Date-function to reflect my local timezone.

Here is the code snippet that I finally ended up with:


Dim tDate As String = Format$(Now, "yyyy-mm-ddThh:nn:sstt")
Dim sDate As String[] = Split(tDate, "+")
Dim iOffset As Integer = Val(sDate[1]) / 100

Then I could use the iOffset parameter to add to the hour part of the time stamp read from file and then it is adjusted to my local timezone.

Thanks to all of you!
User avatar
BruceSteers
Posts: 1591
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Converting UTC time

Post by BruceSteers »

toyman61 wrote: Sunday 28th April 2024 8:07am Thanks for all replies!

I just wanted to get the timezone offset so that I could adjust the hour parameter in the Date-function to reflect my local timezone.

Here is the code snippet that I finally ended up with:


Dim tDate As String = Format$(Now, "yyyy-mm-ddThh:nn:sstt")
Dim sDate As String[] = Split(tDate, "+")
Dim iOffset As Integer = Val(sDate[1]) / 100

Then I could use the iOffset parameter to add to the hour part of the time stamp read from file and then it is adjusted to my local timezone.

Thanks to all of you!
Aah I see
How about just this one liner?

Dim iOffset As Integer = Val(Format(Now, "tt")) / 100


Only using the tt with Format it only gets the time offset so don't need to Split()
and Val() changes the string +0100 to just integer 100
If at first you don't succeed , try doing something differently.
BruceS
toyman61
Posts: 4
Joined: Friday 26th April 2024 7:19am

Re: Converting UTC time

Post by toyman61 »

Thanks BruceS!
I'm using your one-liner in my program. :D
Post Reply