Converting UTC time

Post

Posted
Rating:
#1 (In Topic #1233)
Trainee
 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.
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
See Format() command
/lang/format - Gambas Documentation

Also see Date() command as you don't really want to be running shell timedatectl when gambas has built in date functions.
/lang/date - Gambas Documentation

There is lots of info in the gambas help system.  / - Gambas Documentation
Online now: No Back to the top

Post

Posted
Rating:
#3
Trainee
 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.
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
and Format?
Format will convert local time from UTC

And if you need to show the timezone there is t or tt
/cat/userformat - Gambas Documentation
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’
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"

Code (gambas)

  1.  
  2. Public Sub Form_Open()
  3.  
  4.   Print ConvertUTCDate("2020-02-25T20:15:10Z")
  5.  
  6.  
  7. Public Sub ConvertUTCDate(sDate As String) As String
  8.  
  9.   Dim hDate As Date
  10.  
  11.   ' turn your time format into a date object
  12.   Dim sDay As String = Split(sDate, "T")[0]
  13.   Dim sTime As String = Left(Split(sDate, "T")[1], -1)  ' get Time part and remove the Z
  14.   Dim aYMD As String[] = Split(sDay, "-")
  15.  
  16.   ' convert date values to american format by reversing and swapping month/day
  17.   aYMD = aYMD.Reverse()
  18.   Swap aYMD[0], aYMD[1]
  19.  
  20.   Dim s As String = aYMD.Join("/") & " " & sTime  ' now make a non localised date object
  21.   hDate = CDate(s)
  22.  
  23.   Return Format(hDate, "yyyy-mm-ddThh:nn:sst")
  24.  
  25.  

Not sure if that's exactly what you want but should help you towards getting to the correct data.
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Guru
cogier is in the usergroup ‘Guru’
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?
Online now: Yes Back to the top

Post

Posted
Rating:
#7
Regular
vuott is in the usergroup ‘Regular’

toyman61 said

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:
/comp/gb/system/timezone - Gambas Documentation

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#8
Trainee
 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!
Online now: No Back to the top

Post

Posted
Rating:
#9
Guru
BruceSteers is in the usergroup ‘Guru’

toyman61 said

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?

Code (gambas)

  1.  
  2. Dim iOffset As Integer = Val(Format(Now, "tt")) / 100
  3.  
  4.  
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
Online now: No Back to the top

Post

Posted
Rating:
#10
Trainee
Thanks BruceS!
I'm using your one-liner in my program.  :D
Online now: No Back to the top
1 guest and 0 members have just viewed this.