# DateTime to time_t

John J. Hughes II
Guest
Posts: n/a

 28th Apr 2004
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
Guest
Posts: n/a

 28th Apr 2004
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" <(E-Mail Removed)> wrote in message
news:O%(E-Mail Removed)...
> 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
Guest
Posts: n/a

 28th Apr 2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:O%(E-Mail Removed)...
> > 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
> >
> >

>
>

Jon Skeet [C# MVP]
Guest
Posts: n/a

 29th Apr 2004
John J. Hughes II <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet

Jeffrey Tan[MSFT]
Guest
Posts: n/a

 3rd May 2004
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.

John J. Hughes II
Guest
Posts: n/a

 12th May 2004
Thanks for the feedback, I stand correct.

Regards,
John

John J. Hughes II
Guest
Posts: n/a

 12th May 2004
Yea it makes sense.
Regards,
John

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post PeteOlcott Microsoft VC .NET 4 23rd Sep 2008 01:09 AM =?Utf-8?B?TWFyaw==?= Microsoft ASP .NET 2 18th May 2004 07:11 AM Michael Bray Microsoft C# .NET 1 16th Apr 2004 07:06 PM trellow Microsoft C# .NET 1 31st Dec 2003 05:11 PM Claire Microsoft Dot NET 1 3rd Dec 2003 02:18 AM

Features