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.
Converting UTC time
- BruceSteers
- Posts: 1603
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Converting UTC time
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
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
BruceS
Re: Converting UTC time
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.
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.
- BruceSteers
- Posts: 1603
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Converting UTC time
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
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
BruceS
- BruceSteers
- Posts: 1603
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Converting UTC time
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"
Not sure if that's exactly what you want but should help you towards getting to the correct data.
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
BruceS
- cogier
- Site Admin
- Posts: 1129
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Converting UTC time
What does this date refer to? It looks like it's the file date. What time zones are involved?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).
Re: Converting UTC time
Maybe this can help you: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.
https://gambaswiki.org/wiki/comp/gb/system/timezone
Europaeus sum !
Amare memorentes atque deflentes ad mortem silenter labimur.
Amare memorentes atque deflentes ad mortem silenter labimur.
Re: Converting UTC time
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!
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!
- BruceSteers
- Posts: 1603
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Converting UTC time
Aah I seetoyman61 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!
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
BruceS
Re: Converting UTC time
Thanks BruceS!
I'm using your one-liner in my program.
I'm using your one-liner in my program.