About DateTime and Zone Time

alfcen

Well-Known Member
Licensed User
Longtime User
A bit confused about DateTime.
This line should remove time from the date (eg. 12/22/2010 00:00:00)

B4X:
dt = DateTime.Now - DateTime.Now Mod DateTime.TicksPerDay

However it shows 12/22/2010 09:00:00
which is my time zone = 9hrs

Likewise, the calculation of Julian Date should compensate for the time zone,
yet, this code results into the correct Julian Date without deducting the time zone:

B4X:
jd = (DateTime.Now / DateTime.TicksPerDay + 2440587.5)

So far fine, but if you monitor astronomical events in real-time, such as sunrise/set and moonrise/set the data won't change at midnight local time, but 9 hours later.

Does Android handle the time zone automatically?

Thanks and Cheers
Robert
 

klaus

Expert
Licensed User
Longtime User
Hi Robert,

With
B4X:
[FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]dt = [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]DateTime[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].Now - [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]DateTime[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].Now [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Mod[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]DateTime[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].TicksPerDay[/SIZE][/FONT][/SIZE][/FONT]

I get a result similar to yours 12/22/2010 01:00:00
Time zone GMT +1

This code gives the right result.
B4X:
[FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]dt = [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]DateTime[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].DateParse([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]DateTime[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].Date([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]DateTime[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].Now))[/SIZE][/FONT][/SIZE][/FONT]
12/22/2010 00:00:00

Best regards.
 
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
Hi Klaus,

Your solution is far more elegant than mine:

B4X:
dt = DateTime.Now - DateTime.Now Mod DateTime.TicksPerDay
dt = dt - MyZone * DateTime.TicksPerHour

Thanks so much for your time, I will have to wait until midnight to see
whether my rise and set times will change at 00:00 my time.

My application computes rise/set times, twilight times and Sun/Moon
Ephemerides. Except for that "midnight problem" all results with B4A
are pin-point. At this stage, the output is just a simple list of data.
Some appealing graphical representations of all that data will follow
as I am learning B4A. Drawing on a canvas will probably do.

Cheers
Robert
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Some appealing graphical representations of all that data will follow
as I am learning B4A. Drawing on a canvas will probably do.
Shure, Canvas will do it, it has about all graphic functions as ImageLibEx in B4PPC. The bitmap rotation routines are even easier to use than in B4PPC.

Best regards.
 
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
Hi Klaus,

Just a brief note to let you know that you cured my "midnight problem".
At 00:00 o'clock the rise and set times switched to today's.

DateParse appears to ignore the time zone. It's too late now to dig deeper
into the nature of the Ticks.

Thanks to Andrew's DateInput I managed to add "time travelling".

Anyway, you are awesome. Thanks a lot!

Robert
 
Upvote 0
Top