473,320 Members | 2,020 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,320 software developers and data experts.

Time Conversion

Hi,

I wanted to convert a time value in the form of time_t into a readable
form in C# or vice versa, in order to be able to subtract two time
values and give the result in msecs.

eg.

I have a time value,

1013120149
which is Time in seconds since UTC 1/1/70 in theory.

I need to convert this into,

11:06:31 this form.

or vice vera , i.e from the readable form into the Time in seconds
since UTC 1/1/70.

ANy help would be gr8.

Thanks.

Sep 18 '06 #1
3 2738
moni,

You could do the conversion manually. You could do this to get the base
time:

DateTime base = new Date(1970, 1, 1);

And then convert the time from time_t to this:

// The date time in time_t format.
int time_t = ...;

// The time span.
TimeSpan span = TimeSpan.FromSeconds(time_t);

// The date time.
DateTime dateTime = base + span;

Note, you could call the AddSeconds method here on the base instance and
get a new DateTime as well.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"moni" <mo*******@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
Hi,

I wanted to convert a time value in the form of time_t into a readable
form in C# or vice versa, in order to be able to subtract two time
values and give the result in msecs.

eg.

I have a time value,

1013120149
which is Time in seconds since UTC 1/1/70 in theory.

I need to convert this into,

11:06:31 this form.

or vice vera , i.e from the readable form into the Time in seconds
since UTC 1/1/70.

ANy help would be gr8.

Thanks.

Sep 18 '06 #2

hey,

I cant exactly get wat you are trying to say,

Could u write a psedo code for conversion from

Time: 1158159610 to readable .

and then subtract that with 11:00:04 to get difference in msecs.

It would be really helpful Thanks.


Nicholas Paldino [.NET/C# MVP] wrote:
moni,

You could do the conversion manually. You could do this to get the base
time:

DateTime base = new Date(1970, 1, 1);

And then convert the time from time_t to this:

// The date time in time_t format.
int time_t = ...;

// The time span.
TimeSpan span = TimeSpan.FromSeconds(time_t);

// The date time.
DateTime dateTime = base + span;

Note, you could call the AddSeconds method here on the base instance and
get a new DateTime as well.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"moni" <mo*******@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
Hi,

I wanted to convert a time value in the form of time_t into a readable
form in C# or vice versa, in order to be able to subtract two time
values and give the result in msecs.

eg.

I have a time value,

1013120149
which is Time in seconds since UTC 1/1/70 in theory.

I need to convert this into,

11:06:31 this form.

or vice vera , i.e from the readable form into the Time in seconds
since UTC 1/1/70.

ANy help would be gr8.

Thanks.
Sep 18 '06 #3
moni wrote:
hey,

I cant exactly get wat you are trying to say,

Could u write a psedo code for conversion from

Time: 1158159610 to readable .

and then subtract that with 11:00:04 to get difference in msecs.

It would be really helpful Thanks.

TimeSpan span = new DateTime(1970, 1,
1).AddSeconds(1158159610).Subtract(DateTime.Now.Da te.AddHours(11).AddSeconds(4));
Console.WriteLine(span.TotalMilliseconds);

Presuming 11:00:04 is 11 am today

Which results in -503994000.0 milliseconds.

If you only wanted to subtract 11 hours from midnight on the same day
then you would need to use the same date as you got in the first part
(new DateTime(1970, 1, 1).AddSeconds(1158159610)) to add the 11 hours 4
seconds to.

JB

<...>
Sep 19 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dan glenn | last post by:
Say, I want to set a cookie and have it expire an hour after it's set. It's looking like this is only possible for browsers which are in the same time zone as my server?? In other words, if I...
2
by: learning_C++ | last post by:
I programmed this code with a function "get_current_time" in the begining. When I compiled with the command g++ -Wall -g xxx.xpp -o xxx there are so many errors. please help me and thanks, ...
1
by: heirou | last post by:
I'm a novice in this subject....I've made a database that requires a time conversion. For example, if local time is 1200, determine the time in Korea. I use two fields: a date field, and a time...
6
by: DCSudolcan | last post by:
I know that a program can create and properly initialize an array of pointers to functions at build time, but can something like the following be done at build time? void foo(void); unsigned...
5
by: Paulers | last post by:
Hello, I'm working on an app that requires the functionality to convert the time in Austrailia to the time in New York (EST). I am wondering, what is the bestway to approach this in vb.net? Is...
3
by: Jason S | last post by:
is there any way to use templates to bind integer/floating point constants to a template for compile-time use? e.g. template <double conversion> class meters { const factor = conversion;
3
by: Evan Klitzke | last post by:
Although it is not present in ANSI C, the GNU version of stftime supports the conversion character %z, which is a time offset from GMT. The four digit time offset is required in RFC 2822...
5
by: fimarn | last post by:
I am trying to get rid of compile time error that I am getting only in RHEL5 (not in RHEL4) apparently due to the changes in the stl_list.h file. The error that I am getting is coming from the...
5
by: Grey Alien | last post by:
I need to convert timestamps that are given as the number of seconds that have elapsed since midnight UTC of January 1, 1970, (not counting leap seconds). It seems all of the std C functions...
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...
1
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.