473,237 Members | 1,067 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,237 software developers and data experts.

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 20392
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
If replying to the group, please do not mail me too
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andy Turner | last post by:
I've just been trying to do this. I looked on Google and it seems to be a common problem with no obvious solution. I've seen various solutions which don't seem exactly elegant, so I figured I'd...
1
by: Claire | last post by:
How can I convert date-time data in time_t to C# DateTime class format? e.g. I read in a file a time_t value of 1070390676. How can I convert it to a DateTime? Any help appreciated. Thx, ...
1
by: trellow | last post by:
Does anyone know how to convert data that was written to a file in the "time_t" format to the C# DateTime type? Thanks in advance!
2
by: Chris | last post by:
Hello, How can i convert a C++ CTime (4 bytes) into a C# DateTime ? (my CTime is read in a file). Thanks, -- Chris
2
by: Mark | last post by:
Hi.. I'm writing a web service in C# and needs to produce some time_t values in the xml output, and near as I can figure there appears to be a bug in the C# datetime arithmetic. Below is an...
22
by: Merlin | last post by:
Hello everyone, I realize that I'm new to the group, but I'm hoping that someone might be able to help me out. What I'm doing is using the GNU 7zip command line utility to make a backup on my...
6
by: minboymike | last post by:
Hello everyone, I am working on a project when I have to convert a C# program into Native C for speed purposes. I am using a .dll that creates a DateTime object. Obviously, there is not...
45
by: loudking | last post by:
Hello, all I don't quite understand what does ((time_t)-1) mean when I execute "man 2 time" RETURN VALUE On success, the value of time in seconds since the Epoch is retu rned. On error,...
4
by: PeteOlcott | last post by:
I need to convert time_t to System::DateTime. All of the examples that I found are either in C# or in the obsolete managed C++ syntax. What is the current syntax for converting a (probably 64 bit)...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.