471,851 Members | 979 Online

# DateTime to time_t

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
Nov 16 '05 #1
7 19933
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" <no@invalid.com> wrote in message
news:O%****************@TK2MSFTNGP09.phx.gbl...
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

Nov 16 '05 #2
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" <bo*@bobgrommes.com> wrote in message
news:O0**************@TK2MSFTNGP12.phx.gbl...
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" <no@invalid.com> wrote in message
news:O%****************@TK2MSFTNGP09.phx.gbl...
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

Nov 16 '05 #3
John J. Hughes II <no@invalid.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
Nov 16 '05 #4
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.

Nov 16 '05 #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.

Nov 16 '05 #6
Thanks for the feedback, I stand correct.

Regards,
John
Nov 16 '05 #7
Yea it makes sense.
Regards,
John
Nov 16 '05 #8

### This discussion thread is closed

Replies have been disabled for this discussion.