B4J Question Inconsistency in DateTime

aminoacid

Active Member
Licensed User
Longtime User
I wonder why, when the following code is run on a Windows PC it returns the LOCAL time and when the same code is run on a LINUX machine, it returns UTC time. Is there any way to resolve this inconsistency? I understand that DateTime.Now is supposed to return the UTC time but on a PC it returns the Local time.


B4X:
    ' Calculate Time Stamps
    hrmin=(DateTime.GetHour(DateTime.Now)) * 100 + DateTime.GetMinute(DateTime.Now)
    moday=(DateTime.GetMonth(DateTime.Now)) * 100 + DateTime.GetDayOfMonth(DateTime.Now)
 

OliverA

Expert
Licensed User
Longtime User
Are you sure DateTime.Now is not returning UTC time? Why don't you just log DateTime.Now, and use https://currentmillis.com/ to determine if it is UTC or not. On my Windows 10 system, I get UTC. Please note that GetHour, GetMonth should return your local time information (those methods take into account your time zone settings).
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
If you want to use UTC with the GetHour/GetMonth methods, you'll have to save the current timezone offset, set the timezone offset to 0 (zero), use the methods and then restore your timezone offset.
B4X:
    Dim aTime As Long = DateTime.Now
    Log(aTime)
    Dim offset As Double = DateTime.GetTimeZoneOffsetAt(aTime)
    Log($"GetHour with current timezone offset of ${offset}: ${DateTime.GetHour(aTime)}"$)
    DateTime.SetTimeZone(0)
    Log($"GetHour with no timezone offset: ${DateTime.GetHour(aTime)}"$)
    DateTime.SetTimeZone(offset)
    Log($"GetHour after restoring timezone offset to ${offset}: ${DateTime.GetHour(aTime)}"$)
 
Upvote 0

aminoacid

Active Member
Licensed User
Longtime User
Thanks OliverA! I should have said the code (not DateTime.Now) is returning Local time on a Windows PC and UTC time on a Linux Machine.
OK, so I understand that the GetHour/GetMonth methods will return Local Time. But then why the inconsistency on a Linux machine where they return UTC time instead? That's what's confusing to me.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Have you tried running the code on the Linux box? Could be that the system timezone is set to UTC (if the current timezone offset logs to 0 (zero)), which at that point is then a configuration issue on the Linux box, not a JVM/Java/B4J issue.
 
Upvote 0

aminoacid

Active Member
Licensed User
Longtime User
Yes, I developed the program on a Windows 10 PC and noticed that the date/time was showing local as expected when the JAR file was run. Then taking the same JAR file unmodified to a Linux box running Ubuntu, I noticed that the date/time was UTC. So the first thing I checked was the system timezone settings on the Linux box and it was set to UTC. I changed it to the local time zone, but still get this inconsistency.
 
Upvote 0

aminoacid

Active Member
Licensed User
Longtime User
Ignore the last message .... I guess that after you change the time zone on a Linux box you need to reboot even though the new time shows up immediately. I rebooted and now the applications displays the local time as it should.

Thanks for your help OliverA, your sample code above should however be very useful for future reference.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

aminoacid

Active Member
Licensed User
Longtime User
This code looks very wrong:
B4X:
  hrmin=(DateTime.GetHour(DateTime.Now)) * 100 + DateTime.GetMinute(DateTime.Now)
    moday=(DateTime.GetMonth(DateTime.Now)) * 100 + DateTime.GetDayOfMonth(DateTime.Now)
Watch the date and time video tutorial: https://www.b4x.com/etp.html?vimeography_gallery=1&vimeography_video=256970493

Erel, I looked at the video and could not find anything wrong. The code works fine. The problem was with the Linux box not applying the timezone setting change immediately. What I am doing here is getting the current hours, minutes, month and day, formatted as two 16-bit integers. The way it's formatted may look strange but this is the requirement for the application. And it does what it was intended to do. Do you see a potential issue? Thanks!
 
Upvote 0
Top