none
Converting a managed DateTime to unmanaged _timeb RRS feed

  • Question

  • Hi,

    In my C++/CLI app I'm trying to convert a managed DateTime object into the unmanaged struct _timeb.  What is the best and most efficient way to get this done?

     

    Thanks for the help,

    Dan

    Thursday, May 8, 2008 6:02 PM

Answers

  • Try something like this:

    Code Snippet

    _timeb DateTimeToTimeB(DateTime dateTime)

    {

        _timeb result = _timeb();

     

        DateTime zeroTime(1970, 1, 1, 0, 0, 0, DateTimeKind::Utc);

        TimeSpan delta = dateTime.ToUniversalTime() - zeroTime;

     

        if (delta >= TimeSpan::Zero)

        {

            result.time = delta.Ticks / TimeSpan::TicksPerSecond;

            result.millitm = delta.Milliseconds;

        }

     

        if (dateTime.Kind != DateTimeKind::Utc)

        {

            TimeZone^ timeZone = TimeZone::CurrentTimeZone;

            result.dstflag = timeZone->IsDaylightSavingTime(dateTime);

     

            __int64 offset = result.dstflag ?

                timeZone->GetDaylightChanges(dateTime.Year)->Delta.Ticks : 0;

     

            result.timezone = (short)(

             (offset - timeZone->GetUtcOffset(dateTime).Ticks) / TimeSpan::TicksPerMinute);

        }

     

        return result;

    }

     

     

    I'm not sure about the correctness of the DST calculations, though

     

     

    Thursday, May 8, 2008 8:08 PM