SetTimeZone(0) before every DateTime methode?

Pantelis

Member
Licensed User
Longtime User
SetTimeZone(0) before every DateTime method?

Hi all
I am trying this code and the result of c is not 01:00:00 if the timezone of my device is not 0.
B4X:
Dim a As Long
   Dim b As Long
   Dim c As Long
   a = DateTime.Now
   b = a - DateTime.TicksPerHour

   Log (DateTime.Time(a))
   
   Log (DateTime.Time(b))
   
   c = a - b
   
   Log (DateTime.Time(c))

I order to have correct result i change the code like this:

B4X:
Dim a As Long
   Dim b As Long
   Dim c As Long
   Dim Timezone As Double
   Timezone = DateTime.GetTimeZoneOffsetAt(DateTime.Now)
   
   a = DateTime.Now
   b = a - DateTime.TicksPerHour
   
   DateTime.SetTimeZone(0)
   
   Log (DateTime.Time(a))
   
   Log (DateTime.Time(b))
   
   c = a - b
   
   Log (DateTime.Time(c))
   
   DateTime.SetTimeZone(Timezone)

Is this correct way?

In what methods of the DateTime And the DateUtils this trick must be done in order to have correct results from them? What methods are affected by the timezone?
 
Last edited:

Pantelis

Member
Licensed User
Longtime User
Thanks Erel. It was a bad example.

I read on forum that in order to have corrrect result with date and time calculations we must set timezone to 0. I try to see if this happens in any of datetime method and as i see i have wrong results from alot of them if timezone in not 0. So i set always the timezone to 0. The problem is that if i do that the DateTime.Now gives me UTC values.

How to use the DateTime.Now to get the real date-time value depending on device's settings TimeZone and DST?
 
Upvote 0

Pantelis

Member
Licensed User
Longtime User
Erel i try To understand why the old method while looks ok gives wrong result and found this.

I set the device with this settings:
Device settings:
date: 01/Jan/2013 (No DST on this time)
TimeZone: +3
Time: 10:00:00 AM
(Offset from UTC: +3)
B4X:
    Dim InputDate, OnlyTime As Long
    InputDate = DateTime.Now

 
    OnlyTime = InputDate Mod DateTime.TicksPerDay
 
    Log(DateTime.time(OnlyTime))                'Original time 10:00:00 AM and Returns 10:00:00 AM. OK.
    Log(DateTime.GetHour(OnlyTime))                '10

Then i change the settings of device with:
Device settings:
date: 01/Jul/2013 (We have DST on this time)
TimeZone: +4
Time: 10:00:00 AM
(Offset from UTC: +4, (timezone +3 and DST +1))
and try the same code
B4X:
Dim InputDate, OnlyTime As Long
    InputDate = DateTime.Now

 
    OnlyTime = InputDate Mod DateTime.TicksPerDay
 
    Log(DateTime.time(OnlyTime))                'Original time 10:00:00 AM and Returns 09:00:00 AM.
    Log(DateTime.GetHour(OnlyTime))                '9

DateTime.Time ignores the DST? The same happend with DateTime.Hour
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I have the same problem (#1).

I can not use DateUtils because I need milliseconds.

(Why if I use:
B4X:
DateTime.TimeFormat = "HH:mm:ss:SSS"
Dim NowStart as long
NowStart = DateTime.Now
' ...
Label1.Text = DateTime.Time(DateTime.Now - NowStart)
'It returns +1 hour

it does not work?)

The problem can not be related to "time zone" or other settings,
since both the initial and final time are used with the same settings.

If DateTime.Now is a Long value in milliseconds, the difference should be ... the difference in milliseconds!
 
Last edited:
Upvote 0
Top