By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

Time Conversion

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.