tags:

views:

1027

answers:

5

Hi,

How can I get the time that is not based on my PC time? I use the code below to get time and it works, but when I change my computer time, the time is no longer correct, so is there a way that I can get the actual real time but not from PC time? Maybe GPS time?

    private void btn_DateTime_Click(object sender, EventArgs e)
    {
        DateTime dateTime = DateTime.Now;
        label_localTime.Text = dateTime.ToString();
        DateTime utcNow = DateTime.UtcNow;
        label_utc.Text = utcNow.ToString();


        TimeZone timeZone = TimeZone.CurrentTimeZone;
        if (timeZone.IsDaylightSavingTime(dateTime) == true)
        {
            label_dayLightSaving.Text = "YES";
            label_timeZone.Text = timeZone.GetUtcOffset(dateTime).ToString();
        }
        else
        {
            label_dayLightSaving.Text = "NO";
            label_timeZone.Text = "N//A";
        }                                  
    }

EDIT: Target device is running on WM6 and it has GPS hardware.

thanks.

A: 

Since DateTime is based off of your CPU clock and your computer internal clock, there is no way you can get the DateTime not based on your CPU or computer time.

The best way would be to find a webservice to call that will give you, for example, the Navy Master Clock.

I don't know if there is a web service but you could get it form the site.

David Basarab
I check Navy Master Clock, but I don't think I can get any time from it.
Bopha
I think you are right, I was looking at it too, it is an ajax function.
David Basarab
+1  A: 

You could use ntp but it takes time to access a time source that is not on the computer running the program and accessing Tier 1 or Tier 2 servers too many times in a given time period is probably frowned upon.

Sinan Ünür
thanks for the infor
Bopha
+7  A: 

It sounds like you're looking for a network time server. Unless you have local hardware (GPS receiver, atomic time radio) to support other methods, network time is your best bet.

The Network Time Protocol (NTP) is a protocol for synchronizing the clocks of computer systems over packet-switched, variable-latency data networks.

Here's a client implementation using C#; it might help.

Michael Petrotta
well at the moment I have device running on WM6 and it has GPS hardware so can you tell me how can I go from there? Thanks.
Bopha
@Bopha I though http://msdn.microsoft.com/en-us/library/bb202076.aspx might be useful but http://msdn.microsoft.com/en-us/library/bb201937.aspx says the returned structure member ftLastDataReceived refers to time "according to the local system clock".
Sinan Ünür
Thank you Sinan Unur, I will take a look. Very appreciated.
Bopha
RE: "well at the moment I have device running on WM6 and it has GPS hardware so can you tell me how can I go from there? Thanks."I would move that info into the question.
Justin
@Bopha added the edit based on Justin suggestion, hope its k for u :)
eglasius
A: 

There are also Win32 API's to get uncorrected CPU time, which you have to localise your self.

When have an embedded GPS system running on Win32 platform, where we get GPS time and correct the local PC time while we are running. So for logging we just use local Windows time, but for timers we use the non-correct CPU time, to avoid timers running short or long when the system time is altered.

Simeon Pilgrim
A: 

If you have access to the GPS serial stream, one of the sentences has GMT/UTC time.

UPDATE: the $GPRMC sentence is the one. http://www.codepedia.com/1/The+GPRMC+Sentence

$GPRMC,UTC,POS_STAT,LAT,LAT_REF,LON,LON_REF,SPD,HDG,DATE,MAG_VAR,MAG_REF*CS

kenny