Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# DateTime
Consider a simplee code:
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 2-Jul-12 19:16pm
kosmoh8.4K
Comments
Peter_in_2780 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

DateTimeOffset.Now uses TimeZoneInfo and it caches timezone on startup.
 
To clear the cache call:
TimeZoneInfo.ClearCachedData();
 
This should solve your problem Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
 
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).
 
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:
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.
  Permalink  

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



Advertise | Privacy | Mobile
Web01 | 2.8.150326.1 | Last Updated 14 May 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100