none
TimeSpan from DateTime RRS feed

  • Question



  • Hi,

    I'm trying to create a TimeSpan based on the value of a DateTime.
    I'm creating the TimeSpan from the Ticks property of the DateTime like this (using c++):

    DateTime^ timeObj;

    DebugMsg("Creating DateTime from Struct...");

    timeObj=

    gcnew DateTime(in_timespan.year,in_timespan.month,in_timespan.day,in_timespan.hour,in_timespan.minute,in_timespan.second);

    DebugMsg(

    "Creating TimeSpan from DateTime ticks...");

     

    unsigned long ticks = timeObj->Ticks;

    DebugVar(

    "Ticks",ticks);

    timeSpanObj=

    gcnew TimeSpan(ticks);


    This works fine for small values: with 0001-01-01 00:01:00 I get 600000000 hundreds of nano-seconds as expected.
    However, when I use a DateTime with a value greater than one year (i.e. [0001-01-02 00:01:00]) the number of ticks is 1311573504 which is about 131,16 seconds.

    I can't find Any sense to explain the 131,16 seconds returned.


    Below is the output of the code above with the given value

    [16:46:26.975] :..Creating DateTime from EasyDateTime...
    [16:46:26.975] :..Creating TimeSpan from DateTime ticks...
    [16:46:26.975] :..Ticks:[1311573504]


     

    Tuesday, July 21, 2009 4:26 PM

Answers

  • The problem is due to C++'s difference in data types when compared to C#, which is probably why the documentation confused you here..


    Ticks is a "long long" datatype (__int64, which is 8 bytes).  You're saving this as an unsigned long (4 bytes).  Unfortunately, unsigned long has a max of 4,294,967,295 , so when you do this assignment, your value is wrapping around to 1,311,573,504, since it's going beyond the max allowable in an unsigned long.

    Change your declaration to "long long ticks = timeObj->Ticks;", and your code should work as expected.

    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by Zhi-Xin Ye Tuesday, July 28, 2009 1:58 PM
    Tuesday, July 21, 2009 4:47 PM
    Moderator

All replies

  • The problem is due to C++'s difference in data types when compared to C#, which is probably why the documentation confused you here..


    Ticks is a "long long" datatype (__int64, which is 8 bytes).  You're saving this as an unsigned long (4 bytes).  Unfortunately, unsigned long has a max of 4,294,967,295 , so when you do this assignment, your value is wrapping around to 1,311,573,504, since it's going beyond the max allowable in an unsigned long.

    Change your declaration to "long long ticks = timeObj->Ticks;", and your code should work as expected.

    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by Zhi-Xin Ye Tuesday, July 28, 2009 1:58 PM
    Tuesday, July 21, 2009 4:47 PM
    Moderator
  • Yes, that's it.  You ought to use Int64.  Also note that you should not use the "hat" for the timeObj variable declaration, DateTime is a value type.  Converting from TimeSpan to DateTime is fairly iffy too, time spans can be negative, dates can't.
    Hans Passant.
    Tuesday, July 21, 2009 6:12 PM
    Moderator