B4J Question DateTime.Now problem on raspberry p3

uniplan

Active Member
Licensed User
Longtime User
I'm having a problem with the date on a raspberry p3 device.

To extract the date and time I use the following code:

B4X:
Public Sub estrai_data_ora_attuale As String
    Dim date_format As String = DateTime.DateFormat
    Dim time_format As String = DateTime.TimeFormat
    DateTime.DateFormat = "yyyy-MM-dd" '"dd/MM/yyyy"
    DateTime.TimeFormat = "HH:mm:ss"
    Dim data_ora As String = DateTime.Date(DateTime.Now) & " " & DateTime.Time(DateTime.Now)
    DateTime.TimeFormat = time_format
    DateTime.DateFormat = date_format
    Return data_ora
End Sub

The result I get is a date that is exactly one hour less than the actual date.

I'm sure the system date on the device is correct because I checked it with the date command.

The strangest thing is that I've tried the code on 5 different raspberry devices.
On 4 I get the correct date and on the fifth the date is an hour back.
This although checking the date with the date command by console, the date is correct on all 5 devices.

What could be the problem?

Thanks.
 

inakigarm

Well-Known Member
Licensed User
Longtime User
I had exactly this problem on AWS VPS and the issue was a different timezone; I solved setting the correct zone with datetimectl command and set the correct timezone.

Don't know if it's the same issue as yours
 
Upvote 0

Knoppi

Active Member
Licensed User
Longtime User
Do you get the same results with this function?
Log( GetISO8601( DateTime.Now))

B4X:
' convert the current date/time into an ISO 8601 format string
' yyyy-MM-ddTHH:mm:ss.SSSZ
' 2017-04-24T18:30:59.100+0200
'<code>Log( GetISO8601( DateTime.Now))</code>
Sub GetISO8601( Ticks As Long) As String
    ' https://www.b4x.com/android/forum/threads/iso-8601-date-time-formatting.45373/
    Dim df As String
    df = DateTime.DateFormat
    DateTime.DateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
    Dim res As String = DateTime.Date( Ticks)
    DateTime.DateFormat = df
    Return res
End Sub

Mostly it is a timezone problem.
Is your timezone one hour different to greenwich?
 
Last edited:
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
Yes my timezone is different to greenwich.

I have runned your GetISO8601 function and actually the timezone is not the right one, though with the 'date' command the date and time is displayed correctly.

I think there is some problem with the SO. In fact I tried with "raspi-config" to change the timezone but it goes wrong.

Thanks.
 
Upvote 0

Knoppi

Active Member
Licensed User
Longtime User
Also a 'daylight saving time' problem is possible.

I has the same problem with my debian server 2 years ago, but today i dont know how i fix it.
 
Upvote 0

Knoppi

Active Member
Licensed User
Longtime User
Linux server run with Greenwich DateTime.
The date command calculate the DateTime to local time (timezone, 'daylight saving time').
A Raspberry Pi has no internal clock, use NTP to sync your pi.

I'm sure that it is a config problem with your Pi.
 
Upvote 0
Top