Click here to Skip to main content
15,888,181 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Consider a simplee code:
C#
static void Main(string[] args)
{
    while (true)
    {
        Console.ReadKey();
        Console.WriteLine(DateTimeOffset.Now.ToString());
    }
}


Each time I press any key, I get the current time with timezone.
Now when I change the timezone, and again "ask the date" - I get the same time, i.e. no changes are applied. After running the application again, the changes take place and I see at last the time from the timezone, I set.

Could somebody explain me, how to get the actual time+timezone without reloading the application?

Thanks
Posted
Comments
Peter_in_2780 3-Jul-12 0:30am    
My guess is that the framework reads the timezone information from the OS when your application starts. Maybe a call to one of the TimeZone routines after your ReadKey() might refresh it. Try it and tell us what happens.

Check with TimeZone.CurrentTimeZone[^] to see if the process got notified about the change. Timezone is thread context, it will not follow the system timezone changes. You have to catch such events. As I know there is no specific event, but near: see this topic: http://stackoverflow.com/questions/9725180/c-sharp-event-to-detect-daylight-saving-or-even-manual-time-change[^]
 
Share this answer
 
 
Share this answer
 
DateTimeOffset.Now uses TimeZoneInfo and it caches timezone on startup.

To clear the cache call:
C#
TimeZoneInfo.ClearCachedData();


This should solve your problem :-)
 
Share this answer
 
Well, the problem is, that without restart of the application, DateTimeOffset.Now will return the time from the start timezone. I can`t afford restart of mine application, but I still need get the exact time after the timezone was changed.
The possible solution (a bad one, but it works) - is to run a process, that returns a DateTimeOffset.Now and that`s all.

See the code of this process:

C#
using System;
using Microsoft.Win32;

namespace DateTimePick
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTimeOffset offset = DateTimeOffset.Now;
            Console.WriteLine(offset.Offset.TotalHours);
        }
    }
}


Now I`ve got an executable, that prints offset hours as a double.
In my "main" program I add a class, which runs this process each time the system time is changed (thanks to Zoltán Zörgő for his advice).

C#
public class DateTimeExactPicker
    {
        static TimeSpan currentOffset;

        private static string getExactDateTime()
        {
            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = "DateTimePick.exe";
            p.Start();
            string output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            return output;

        }

        public static DateTimeOffset Now
        {
            get { return new DateTimeOffset(DateTimeOffset.UtcNow.Ticks + currentOffset.Ticks, currentOffset); }
        }

        static DateTimeExactPicker()
        {
            SystemEvents.TimeChanged += new EventHandler(SystemEvents_TimeChanged);
            currentOffset = DateTimeOffset.Now.Offset;
        }

        static void SystemEvents_TimeChanged(object sender, EventArgs e)
        {
            double hoursOffset = double.Parse(getExactDateTime());
            TimeSpan offset = TimeSpan.FromHours(hoursOffset);
            currentOffset = offset;
        }
    }

Now my test program looks like this:
C#
static void Main(string[] args)
       {
           while (true)
           {
               Console.WriteLine(DateTimeExactPicker.Now);
               Console.ReadKey();
           }
       }


The output is:

03.07.2012 12:03:03 +03:00
03.07.2012 12:03:04 +03:00
03.07.2012 12:03:04 +03:00
03.07.2012 12:03:04 +03:00


...after I change the timezone

03.07.2012 15:33:16 +06:30
03.07.2012 15:33:16 +06:30
03.07.2012 15:33:17 +06:30
03.07.2012 15:33:17 +06:30
03.07.2012 15:33:17 +06:30


...now I`ve changed it back

03.07.2012 12:03:29 +03:00
03.07.2012 12:03:29 +03:00


Listening to TimeChanged event is a good idea, because running the process each time, I need the exact time value might be too expensive.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900