I am building a windows store clock app to show the current time of the user and also for different cities around the world.
Initially, I had planned on doing this completely using time zone web services from Google etc., but after looking at the number of requests allowed in free accounts and the cost involved in getting a paid account, I felt it was better to find an alternate solution without having to mortgage the house.
Searching around, I found the excellent NodaTime library by John Skeet and team. After digging through the documentation and here on stackoverflow, my head is still buzzing with all the date time and timezone related terminology and conversion methods. Anyway, I thought I could do this with 2 options:
Option 1: Using DateTime.Now to get the current system time and then getting time for other zones using nodatime like this (based on code provided by Matt Johnson in reply to another question on SO):
DateTimeZone homeZone = DateTimeZoneProviders.Tzdb["Asia/Colombo"];
LocalDateTime homeTime = LocalDateTime.FromDateTime(DateTime.Now);
ZonedDateTime homeTimeInZone = homeTime.InZoneStrictly(homeZone);
TbTime1.Text = "Home time is: " + homeTimeInZone.ToDateTimeOffset();
DateTimeZone timeZone1 = DateTimeZoneProviders.Tzdb["Australia/Perth"];
ZonedDateTime timeZone1Time = homeTimeInZone.WithZone(timeZone1);
TbTime2.Text = "Timezone 1 time is: " + timeZone1Time.ToDateTimeOffset();
Option 2: After looking further, I found this solution and felt it would also work nicely like this:
public ZonedDateTime GetTimeInTimeZone(string timeZone)
{
// Timezone is olson timezone e.g. "Asia/Colombo"
Instant now = SystemClock.Instance.Now;
var zone = DateTimeZoneProviders.Tzdb[timeZone];
ZonedDateTime zonedDateTime = now.InZone(zone);
return zonedDateTime;
}
Now let me try to get to the question: In both options above, there is a dependency on either DateTime.Now or SystemClock.Instance.Now to first identify the user's system time and THEN convert it to the required timezone's city time. But what happens if the user's system is not set to the correct time? I believe (correct me if wrong) DateTime.Now and SystemClock.Instance.Now are both using the system clock to get the current time? Assuming that the system time is not set correctly, then any timezone conversion will simply show the wrong time for other cities due to our dependency on the user's system time.
In such cases, how do I establish the user's current time without relying on their system clock? Should I revert to using a web service to get the user's current time zone using lat/long or is there a better option using NodaTime etc that can work offline? Thanks.