DateTime to time_t

Discussion in 'Microsoft C# .NET' started by John J. Hughes II, Apr 28, 2004.

  1. Is there a better way of doing this?

    DateTime startTime = new DateTime(1970,1,1,0,0,0,0);
    TimeSpan currTime = DateTime.Now - startTime;
    UInt32 time_t = Convert.ToUInt32(Math.Abs(currTime.TotalSeconds));


    Regards,
    John
     
    John J. Hughes II, Apr 28, 2004
    #1
    1. Advertisements

  2. John J. Hughes II

    Bob Grommes Guest

    If you're not interested in hrs/mins/secs/ms you can just use the override
    that leaves those out:

    DateTime startTime = new DateTime(1970,1,1);

    I suppose that if you don't need currTime for anything else you could save
    one intermediate step:

    UInt32 time_t = Convert.ToUInt32(Math.Abs((DateTime.Now -
    startTime).TotalSeconds));

    .... although possibly the compiler may optimize that in anyhow. And of
    course if you're doing this repeatedly, startTime should be calculated once,
    outside the loop.

    Why use Math.Abs()? If startTime will always be Jan 1 1970, the result of
    the subtraction will always be positive unless you plan to set your system
    clock prior to Jan 1 1970:

    UInt32 time_t = Convert.ToUInt32((DateTime.Now - startTime).TotalSeconds);

    --Bob

    "John J. Hughes II" <> wrote in message
    news:O%...
    > Is there a better way of doing this?
    >
    > DateTime startTime = new DateTime(1970,1,1,0,0,0,0);
    > TimeSpan currTime = DateTime.Now - startTime;
    > UInt32 time_t = Convert.ToUInt32(Math.Abs(currTime.TotalSeconds));
    >
    >
    > Regards,
    > John
    >
    >
     
    Bob Grommes, Apr 28, 2004
    #2
    1. Advertisements

  3. Thanks for the response.

    It's not in a loop and only really used once so....

    I put the hours/minutes/seconds so the compiler would not do anything
    stupid, I try never to assume it will default to zero. That is when time_t
    starts so I want it to be correct.

    I am using abs because according to the documents the total seconds function
    return a decimal with the partial seconds. I figured it would be safer to
    strip them before converting. I needed the unsigned 32 bit value and was
    concerned the runtime might not like the values after the decimal. Would
    think it would strip them but you never know, been bitten before.

    Was more looking for a better way of getting the time_t value then
    optimizing my code. Would sort of be nice if the DateTime class had such a
    function: DateTime.Now.Time_t

    Regards,
    John

    "Bob Grommes" <> wrote in message
    news:...
    > If you're not interested in hrs/mins/secs/ms you can just use the override
    > that leaves those out:
    >
    > DateTime startTime = new DateTime(1970,1,1);
    >
    > I suppose that if you don't need currTime for anything else you could save
    > one intermediate step:
    >
    > UInt32 time_t = Convert.ToUInt32(Math.Abs((DateTime.Now -
    > startTime).TotalSeconds));
    >
    > ... although possibly the compiler may optimize that in anyhow. And of
    > course if you're doing this repeatedly, startTime should be calculated

    once,
    > outside the loop.
    >
    > Why use Math.Abs()? If startTime will always be Jan 1 1970, the result of
    > the subtraction will always be positive unless you plan to set your system
    > clock prior to Jan 1 1970:
    >
    > UInt32 time_t = Convert.ToUInt32((DateTime.Now - startTime).TotalSeconds);
    >
    > --Bob
    >
    > "John J. Hughes II" <> wrote in message
    > news:O%...
    > > Is there a better way of doing this?
    > >
    > > DateTime startTime = new DateTime(1970,1,1,0,0,0,0);
    > > TimeSpan currTime = DateTime.Now - startTime;
    > > UInt32 time_t = Convert.ToUInt32(Math.Abs(currTime.TotalSeconds));
    > >
    > >
    > > Regards,
    > > John
    > >
    > >

    >
    >
     
    John J. Hughes II, Apr 28, 2004
    #3
  4. John J. Hughes II <> wrote:
    > Thanks for the response.
    >
    > It's not in a loop and only really used once so....
    >
    > I put the hours/minutes/seconds so the compiler would not do anything
    > stupid, I try never to assume it will default to zero. That is when time_t
    > starts so I want it to be correct.


    If you're going to assume that documentation is wrong, you're in for a
    world of pain. The documentation for DateTime(int,int,int) specifically
    states that the time is set to midnight.

    > I am using abs because according to the documents the total seconds function
    > return a decimal with the partial seconds.


    A double, actually.

    > I figured it would be safer to strip them before converting.


    But Abs is to do with making values non-negative, not to do with
    stripping anything.

    I'd just cast to uint or int, personally.

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet [C# MVP], Apr 29, 2004
    #4
  5. Hi John,

    Does the community's reply make sense to you? Do you still have any concern
    on this issue?

    Please feel free to feedback. Thanks

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
     
    Jeffrey Tan[MSFT], May 3, 2004
    #5
  6. Thanks for the feedback, I stand correct.

    Regards,
    John
     
    John J. Hughes II, May 12, 2004
    #6
  7. Yea it makes sense.
    Regards,
    John
     
    John J. Hughes II, May 12, 2004
    #7
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. trellow

    How do I convert time_t data to DateTime type

    trellow, Dec 31, 2003, in forum: Microsoft C# .NET
    Replies:
    1
    Views:
    420
    Patrick Steele [MVP]
    Dec 31, 2003
  2. Mark Worrall

    Re: converting Unix datetime count to .Net DateTime object

    Mark Worrall, Feb 4, 2004, in forum: Microsoft C# .NET
    Replies:
    1
    Views:
    252
    Aravind C
    Feb 4, 2004
  3. Guest

    (DateTime) or DateTime.Parse ?

    Guest, Apr 4, 2004, in forum: Microsoft C# .NET
    Replies:
    2
    Views:
    160
    Guest
    Apr 4, 2004
  4. Michael Bray
    Replies:
    1
    Views:
    414
    Guest
    Apr 16, 2004
  5. mimi
    Replies:
    1
    Views:
    1,282
    Jon Skeet [C# MVP]
    Aug 6, 2004
Loading...

Share This Page