Android Question TimeZoneOffset in Greece

db0070

Active Member
Licensed User
Longtime User
I have an app that calculates sunrise. It gives me the correct sunrise times in the UK, where the Time Zone Offset is 0. My daughter has just gone on holiday to Greece, and she reports back that the sunrise time my app shows is one hour ahead. I calculate sunrise using GPS location, and then add TZ which is worked out as

B4X:
TZ = DateTime.TimeZoneOffset

TZ should come to 2 hours in Greece, which indeed it does, when I test by manually setting my phone here in the UK, to locations Athens.

The phone on my daughter's phone has automatically adjusted to Athen's time, which I have confirmed with her is 2 hours ahead of GMT.

Anyone got any idea why the DateTime.TimeZoneOffset appears to show 3 hours, when it is showing 2 hours on the phone time?

IOS app on a iPhone 5 is also showing the same as the android app.
 

db0070

Active Member
Licensed User
Longtime User
I just checked on my phone here in UK. No matter what time zone I select on my phone, DateTime.TimeZoneOffset always returns 0. That can't be right
 
Upvote 0

db0070

Active Member
Licensed User
Longtime User
I think I have found the problem in my code, but can't make sense of it - perhaps because I still have not understood DateTime functions. The problem I think is here:-

B4X:
  sunrise = DateTime.Time((Y2-1) *60*60*DateTime.TicksPerSecond + TimeZone*DateTime.TicksPerHour +DST*DateTime.TicksPerHour)

Y2 for example is 5.93 which represents 5 hours and 60*0.93 minutes, making sunrise 5:55 am. So with TimeZone and DST zero, I don't see the problem, but when Timezone is a 1 or 2 then the problem appears. What is wrong in the code above? (I had to put 'Y2-1', and I don't know why, otherwise the time was one hour out)
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Interesting @db0070 that you have written an app to calculate sunrise. I've got an app on the Play Store that manipulated sunrise, sunset etc wherever you are in the world. I used the following library to make my life easier and according to users dotted around the globe the sunrise/sunset image changer is basically spot on for/

https://www.b4x.com/android/forum/threads/astro-library.15975/

demo-jpg.10936
 
Upvote 0

db0070

Active Member
Licensed User
Longtime User
Thanks for that, but its not just sunrise or sunset times. I also calculate sun's position at various degrees below the horizon and if twilight exists in a location, hence the reason for calculating. But, that is an interesting library, and I could use it to add moon data to my app.

The question I ought to be asking, is how to convert a number like 5.5 to a time to give 5:30am. The code DateTime(number * TicksPerHour) does not appear to give me the correct answer.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

db0070

Active Member
Licensed User
Longtime User
In 1970 in the UK it was UTC + 1 hour all year - that explains why I was getting 1 hour more! So 5 hours after midnight in 1970 will be 1 hour more in 2016, until DST comes in play.
DateTime library is pretty clever!
 
Upvote 0
Top