emulator

  • Windows Phone Emulator Time Skew When Computer Sleeps

    TLDR: If you put your computer to sleep, the Windows Phone emulator might have the wrong time when you resume. Restart the emulator to get it to have the current time in its clock.

    I was working on my fork of an OAuth library for Windows Phone this weekend, and I ran across a really weird issue. I’m posting this in case someone else Googles this problem, since I couldn’t find anything.

    I have a UNIX epoch timestamp generator (necessary for Twitter) class that looks like this:

        public class TimestampGenerator
        {
            public string Generate()
            {
                var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0);
                var now = DateTime.UtcNow;
    
                long stamp = ((now.Ticks - unixEpoch.Ticks) / 10000000);
    
                return stamp.ToString();
            }
        }

    I worked for hours on this on my desktop and everything was working fine. Then, when I did a git pull on my laptop, it was suddenly broken. When I’d make a request to the term.ie example OAuth service, it said my timestamp was expired every time. I set breakpoints and compared it to the current epoch timestamp, and it was definitely wrong. I tried many ways of calculating it (that division is to get to seconds from 100 microsecond increments (ticks); I haven’t benchmarked to see which is faster), but nothing made a difference.

    I calculated a time difference of about 102 hours from my current time based on the timestamps. I finally figured out that my emulator could have the wrong time, since I was thinking computer time == emulator time. I killed the emulator, hit F5, and it worked like a charm. Then, I thought about the time difference, and it was about the last time I put my laptop to sleep by closing its lid.

    Apparently, when the computer comes out of sleep, the computer clock stops matching the emulator clock; the emulator clock freezes when the computer sleeps and just resumes from where it left off. I’m not sure if this is a bug or not, or if it happens on a regular basis. I may try to reproduce it later, but probably not.