Search code examples
windows-phone-7unixtimestamp

Receive Unix Timestamp and convert time to milliseconds WP7


I receive a unix timestamp in my wp7 app and i want to convert it to milliseconds..

I make this:

time.Ticks / 10000;

Is this correct? this give the total time in milliseconds or only the milliseconds?

I want the total time in milliseconds..

my method to get the time is this:

void _ntpClient_TimeReceived(object sender, NtpClient.TimeReceivedEventArgs e)
{
    this.Dispatcher.BeginInvoke(() =>
    {
        DateTime time = e.CurrentTime;

        long milliseconds = time.Ticks / 10000;
    });
}

Solution

  • Unix generally stores time as either seconds, or a struct timespec that contains both seconds and microseconds for further precision. When referring to dates, it is the number of seconds (or seconds and microseconds) elapsed since January 1, 1970.

    However, these are never referred to as "ticks". "Ticks" generally refer to the Windows/.NET style time units - a "tick" is equal to 100 nanoseconds. When referring to dates, it is the number of hundred nanosecond units that have elapsed since January 1, 0001.

    If you have an object with "ticks", then yes, simply dividing by 10000 will convert the units to milliseconds. Alternately, you can use a TimeSpan:

    TimeSpan ts = new TimeSpan(ticks);
    millis = ts.TotalMilliseconds;