Page 1 of 1

Converting UTC time

Posted: Friday 26th April 2024 7:26am
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.

Re: Converting UTC time

Posted: Friday 26th April 2024 9:33am
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

Re: Converting UTC time

Posted: Friday 26th April 2024 10:24am
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.

Re: Converting UTC time

Posted: Friday 26th April 2024 10:56am
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

Re: Converting UTC time

Posted: Friday 26th April 2024 12:31pm
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.

Re: Converting UTC time

Posted: Friday 26th April 2024 2:41pm
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?

Re: Converting UTC time

Posted: Friday 26th April 2024 9:45pm
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

Re: Converting UTC time

Posted: Sunday 28th April 2024 8:07am
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!

Re: Converting UTC time

Posted: Sunday 28th April 2024 10:09am
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

Re: Converting UTC time

Posted: Monday 29th April 2024 9:41pm
by toyman61
Thanks BruceS!
I'm using your one-liner in my program. :D