Time Conversion

M

moni

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
M

moni

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.



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]
- (e-mail address removed)



moni said:
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.
 
J

John B

moni said:
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.Date.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

<...>
 

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