DateTime to time_t

J

John J. Hughes II

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
 
B

Bob Grommes

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
 
J

John J. Hughes II

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
 
J

Jon Skeet [C# MVP]

John J. Hughes II said:
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.
 
J

Jeffrey Tan[MSFT]

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top