65.9K
CodeProject is changing. Read more.
Home

Exchange Date Time between Native C++ and C#

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.84/5 (12 votes)

Dec 10, 2013

CPOL

2 min read

viewsIcon

41687

How to exchange date time between C# and C++

Introduction

A coworker asked me an interesting question. Here is his question:

Quote:
We need to pass a Date (and potentially time) between C++ and C#. What way do you think would be best for this/what is built in C++? Since time (potentially milliseconds) is a component to the date, I don’t want to pass a string, nor do I want to decompose the date time into elements (i.e. month, day, hour, year, minutes, etc.).
Just wondering if there is a common convention between C# and C++ we could use.
OK. Well I didn't know the answer right away. After some digging and searching around, here is my solution.

Background

We know that raw data types like integer type (32-bit or 64-bit) are the same across C++ and C# and their date time are nothing but different formats of timestamps, so it's possible to use an integer number to finish up the job.

Using the Code

What is a .NET DateTime? Here is what from MSDN:

Quote:
Represents an instant in time, typically expressed as a date and time of day.
How does it get constructed? There are many constructors to create a DateTime object. For the purpose of exchanging data between C# and C++, the following constructor is crucial.
Quote:
DateTime(Int64) initializes a new instance of the DateTime structure to a specified number of ticks.
// This example demonstrates the DateTime(Int64) constructor. 
using System;
using System.Globalization;

class Sample 
{
    public static void Main() 
    {
        // Instead of using the implicit, default "G" date and time format string, we 
        // use a custom format string that aligns the results and inserts leading zeroes. 
        string format = "{0}) The {1} date and time is {2:MM/dd/yyyy hh:mm:ss tt}";

        // Create a DateTime for the maximum date and time using ticks.
        DateTime dt1 = new DateTime(DateTime.MaxValue.Ticks);

        // Create a DateTime for the minimum date and time using ticks.
        DateTime dt2 = new DateTime(DateTime.MinValue.Ticks);

        // Create a custom DateTime for 7/28/1979 at 10:35:05 PM using a  
        // calendar based on the "en-US" culture, and ticks. 
        long ticks = new DateTime(1979, 07, 28, 22, 35, 5, 
            new CultureInfo("en-US", false).Calendar).Ticks;
        DateTime dt3 = new DateTime(ticks);

        Console.WriteLine(format, 1, "maximum", dt1);
        Console.WriteLine(format, 2, "minimum", dt2);
        Console.WriteLine(format, 3, "custom ", dt3);
        Console.WriteLine("\nThe custom date and time is created from {0:N0} ticks.", ticks);
    }
}
/*
This example produces the following results:

1) The maximum date and time is 12/31/9999 11:59:59 PM
2) The minimum date and time is 01/01/0001 12:00:00 AM
3) The custom  date and time is 07/28/1979 10:35:05 PM

The custom date and time is created from 624,376,461,050,000,000 ticks.
*/ 

The number (624,376,461,050,000,000) will be passed to C++ to construct a C structure. The following snippet is taken from here. Thanks!

// Convert C# DateTime to C++ date time
{
    // 624376461050000000 (dec)  07/28/1979 10:35:05 PM
    unsigned __int64 DTFromCDull = 0x8AA3B154F003280; 
   // Thursday, January 01, 1970 12:00:00 AM  
   unsigned __int64 UnixEpoch = 0x089f7ff5f7b58000;  

   time_t t;
   struct tm *tm;

   t=(time_t)((DTFromCDull-UnixEpoch)/10000000);
   tm=gmtime(&t); 
   printf("%s \n",asctime(tm));
}
Output: Sat Jul 28 22:35:05 1979  

How do we convert C++ ticks back to C#?

// Assuming tick is passed in by C++ which is:
// time_t ticks = time(0);
long timeNow = ticks * 10000000 + 0x089f7ff5f7b58000;
DateTime dt = new DateTime(timeNow); 

How do you deal with the milliseconds within C# DateTime? The answer is do exactly what .NET does.

C# source code for DateTime can be found from Microsoft website or search within this website:
http://www.dotnetframework.org/Search.aspx[^].

C++ code for converting ticks into C# DateTime milliseconds:

__int64 tick = 0x8AA3B154F003280;
int millisecond = ((ticks & 0x3FFFFFFFFFFFFFFF) / 10000) % 1000; 

If you want to convert between different timestamps, the following references will be intensively helpful.

It's quite possible I might have missed something, please let me know. Many thanks to Jeff for inspiring me to write this up.

References