How to convert Android time to NETMF time?

I have data logged on an Android system that records time as milliseconds and I want to use this time on a NETMF device to show a graph.

I found some information on a search that shows the diffences in the epoch for .NET and JAVA.

epoch for .Net
Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calendar.

epoch for java
specified number of milliseconds since the standard base time known as “the epoch”, namely January 1, 1970, 00:00:00 GMT.

Has anyone come across a library or source that could convert from one to the other? I need only to go from Android to NETMF.

They both have .ToUTCTime, would that not work?

The count of Ticks for 1970-01-01 at 00:00:00 is 621355968000000000

        /// <summary>
        /// Convert a native DateTime to a Unix Time.
        /// </summary>
        /// <param name="msTime">The DateTime to convert</param>
        /// <returns></returns>
        public static int ConverttoUnixTime(DateTime msTime)
        {
            DateTime _epochTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            TimeSpan span = (msTime - _epochTime);

            return (int)(span.Ticks / TimeSpan.TicksPerSecond);
        }

        /// <summary>
        /// Convert a Unix Time back to a native DateTime
        /// </summary>
        /// <param name="unixTime"></param>
        /// <returns></returns>
        public static DateTime ConverttoDateTime(long unixTime)
        {
            DateTime unixDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(unixTime);
            return unixDateTime;
        }

3 Likes

Thanks Sprigo. That worked perfectly.