12 Replies - 41854 Views - Last Post: 09 August 2007 - 01:15 PM Rate Topic: -----

#1 cygnusX  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 159
  • Joined: 19-May 07

Convert UTC to local time

Posted 29 May 2007 - 10:33 AM

How to convert universal time to local time.As far as i know the universal time is the total number of seconds elapsed since January 1st, 1970.Sorry if i'm wrong.In my application in some point of the execution i need to retrieve a date,but instead of date i get some meaningless numbers,for example "1179695533".How to convert these numbers to a date,more specifically to local time?

This post has been edited by cygnusX: 29 May 2007 - 10:42 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Convert UTC to local time

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Convert UTC to local time

Posted 29 May 2007 - 08:33 PM

One way is to create a DateTime object and then supply your parameter to the Date.FromFileTimeUtc method to set the DateTime object.

Example:
Dim time As DateTime = New DateTime()

time = Date.FromFileTimeUtc(1179695533)

MessageBox.Show(time)


This will display 1/1/1601 12:01:57 AM in a MessageBox.
Was This Post Helpful? 0
  • +
  • -

#3 cygnusX  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 159
  • Joined: 19-May 07

Re: Convert UTC to local time

Posted 30 May 2007 - 12:29 AM

Ok,this works,but the problem is that must shows some current date,not "1/1/1601 12:01:57 AM",i mean in my application i get "117969553301" ,which must shows something like "5/21/2007 00:12:13",i check that with other appication.How to do that?Thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#4 JellyBean  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 60
  • Joined: 25-April 07

Re: Convert UTC to local time

Posted 30 May 2007 - 03:52 AM

The closest I've got to the answer is this:

public static DateTime ConvertTime(long timeAsInteger)
{
	double time = Convert.ToDouble(timeAsInteger) / 100;
	DateTime baseDate = new DateTime(1970, 1, 1);
	DateTime result = baseDate + TimeSpan.FromSeconds(time);
	return result;
}


If I pass in 117969553301 I get the answer 20/05/2007 21:12:13 (that's UK date format...)

Not sure whether it will help, but it's very close to 5/21/2007 00:12:13.

It's a very strange DateTime encoding though!!
Was This Post Helpful? 0
  • +
  • -

#5 cygnusX  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 159
  • Joined: 19-May 07

Re: Convert UTC to local time

Posted 30 May 2007 - 04:36 AM

Thanks,that really help me,but what bothers me now is that difference of 3 hours...
Was This Post Helpful? 0
  • +
  • -

#6 JellyBean  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 60
  • Joined: 25-April 07

Re: Convert UTC to local time

Posted 30 May 2007 - 04:38 AM

View PostcygnusX, on 30 May, 2007 - 12:36 PM, said:

Thanks,that really help me,but what bothers me now is that difference of 3 hours...

What time zone are you in?
Was This Post Helpful? 0
  • +
  • -

#7 JellyBean  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 60
  • Joined: 25-April 07

Re: Convert UTC to local time

Post icon  Posted 30 May 2007 - 04:46 AM

Do you have any other example times (like 117969553301 I mean)? If you test the method with other values do you get the same 3 hours discrepency?
Was This Post Helpful? 0
  • +
  • -

#8 cygnusX  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 159
  • Joined: 19-May 07

Re: Convert UTC to local time

Posted 30 May 2007 - 05:08 AM

ok,other example,when i have "1180249567" i get the date "17.5.1970 14:28:15" but the real date is "27.5.2007 10:06:06"(checked with other application),i'm really confused...

and my time zone is GMT +2
Was This Post Helpful? 0
  • +
  • -

#9 JellyBean  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 60
  • Joined: 25-April 07

Re: Convert UTC to local time

Posted 30 May 2007 - 05:25 AM

View PostcygnusX, on 30 May, 2007 - 01:08 PM, said:

ok,other example,when i have "1180249567" i get the date "17.5.1970 14:28:15" but the real date is "27.5.2007 10:06:06"(checked with other application),i'm really confused...

and my time zone is GMT +2


If you change the method this time to remove the division by 100, so:

public static DateTime ConvertTime(long timeAsInteger)
{
	double time = Convert.ToDouble(timeAsInteger);
	DateTime baseDate = new DateTime(1970, 1, 1);
	DateTime result = baseDate + TimeSpan.FromSeconds(time);
	return result;
}


We get the result 27/05/2007 07:06:07 (again UK format). 3 hours and 1 second out. This is too close to be chance (twice!), but I have no idea for the logic behind the original date/time encoding...

Where are you getting the data from?

This post has been edited by JellyBean: 30 May 2007 - 05:26 AM

Was This Post Helpful? 0
  • +
  • -

#10 cygnusX  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 159
  • Joined: 19-May 07

Re: Convert UTC to local time

Posted 30 May 2007 - 05:29 AM

i get it from IRC server
Was This Post Helpful? 0
  • +
  • -

#11 JellyBean  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 60
  • Joined: 25-April 07

Re: Convert UTC to local time

Posted 30 May 2007 - 05:40 AM

View PostcygnusX, on 30 May, 2007 - 01:29 PM, said:

i get it from IRC server

Are both examples from the same IRC server? If you are retrieving the time via .NET could you post some code?

The location if the IRC server might give some insight into this 3 hour time discremency.... Which server have you tested this on?

This post has been edited by JellyBean: 30 May 2007 - 05:42 AM

Was This Post Helpful? 0
  • +
  • -

#12 cygnusX  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 159
  • Joined: 19-May 07

Re: Convert UTC to local time

Posted 30 May 2007 - 07:09 AM

yeah,they are from the same irc server and the server is "irc.omega.bg"

this is how i get the time from the server,actually that's not exactly the time,but it's a string which contains the time

string s = e.Data.RawMessage;
					int remove = 4;
					char separator = ' ';
					int index = 0;
					for (int i = 0; i < remove; i++)
						index = s.IndexOf(separator, ++index);
					s = s.Substring(++index);


Was This Post Helpful? 0
  • +
  • -

#13 lianaent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-August 07

Re: Convert UTC to local time

Posted 09 August 2007 - 01:15 PM

View PostcygnusX, on 30 May, 2007 - 07:09 AM, said:

yeah,they are from the same irc server and the server is "irc.omega.bg"

this is how i get the time from the server,actually that's not exactly the time,but it's a string which contains the time

string s = e.Data.RawMessage;
					int remove = 4;
					char separator = ' ';
					int index = 0;
					for (int i = 0; i < remove; i++)
						index = s.IndexOf(separator, ++index);
					s = s.Substring(++index);




Call me naive, but can't you just do:

DateDiff(DateInterval.Hour,now, now.ToUniversalTime)

to get the time difference, and add/subtract (depending on which way you're going) that to your current time?

Larry
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1