Converting from time_t to System.DateTime

I was busy reviewing my minidump source code before publishing it to codeplex and I noticed that there was one field in the MINIDUMP_MODULE structure that I didn’t mention previously: MINIDUMP_MODULE.TimeDateStamp.

The documentation for MINIDUMP_MODULE says this field is stored as a time_t, which turns out to be a C/C++ type. Trying to find out how to convert that into a System.DateTime in .Net has been a painful experience. The amount of different date/time formats in the Windows API is quite scary. You can find a list of some of them here.

So to save you the time and effort of trying to figure out how to deal with time_t’s here the code to convert them to a System.DateTime:

public static DateTime TimeTToDateTime(UInt32 time_t)
    // 10 000 000 * january 1st 1970
    long win32FileTime = 10000000 * (long)time_t + 116444736000000000;

    // FromFileTimeUtc is the UCT time, FromFileTime adjusts for the local timezone
    return DateTime.FromFileTime(win32FileTime); 

And here’s why it works.

Another route that (should) work is to first convert the time_t to a system time using FileTimeToSystemTime.

This entry was posted in Crash Dumps and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s