8 Replies - 19405 Views - Last Post: 31 July 2007 - 10:19 AM Rate Topic: -----

#1 cygnusX  Icon User is offline

  • D.I.C Head

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

Convert univerwsal time to date

Post icon  Posted 30 July 2007 - 05:38 AM

How to convert universal time to date.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 "117969553301" which must shows something like "5/21/2007 00:12:13",i check that with other appication.How to convert these numbers to a date?Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Convert univerwsal time to date

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,180
  • Joined: 18-April 07

Re: Convert univerwsal time to date

Posted 30 July 2007 - 10:22 AM

Universal time is known as UTC or another name for Greenwich Mean Time (GMT) which is the time on the prime meridian that passes through Greenwich in the UK. The time interval you are talking about since Jan 1, 1970 is known as the Epoch time or the time used by systems like Unix which represents the amount of seconds that have elapsed since that date.

Being that *nix environments use epoch for a majority of their timestamping, you can bet stuck up microsoft is not really going to want to accept that. They rather use UTC which is why you see it in a majority of objects dealing with DateTime.

So how do we get from epoch to datetime? Well... if that number you have there is the seconds since 1970 you can do the following trick...

double timestamp = 117969553301;

// First make a System.DateTime equivalent to the UNIX Epoch.
System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);

// Add the number of seconds in UNIX timestamp to be converted.
dateTime = dateTime.AddSeconds(timestamp);




What we are doing here (with the help of code from codeproject.com) is setting up a datetime object that defaults to Jan 1, 1970 and adding the number of seconds that have elapsed. This will set the datetime to the correct date and allow you to then manipulate it as an ordinary date.

This should get you close enough in most applications. To get immense pinpoint accuracy you may need to write a function which will actually parse the number of seconds, account for things like leap seconds etc. But unless you are designing something for an astrophysicist, I think the above solution will be good enough.

Now, I am assuming that is not an epoch time since it may have too many digits. In that case, we will need to know how you came to that number because you may have an error which is producing an incorrect result.

PS: Actually I just tried it and it is not coming out with the right date, so it is not an epoch date. We will need to know where you got that number from. Please post the code which gives you the number. Thanks

This post has been edited by Martyr2: 30 July 2007 - 10:27 AM

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 univerwsal time to date

Posted 30 July 2007 - 11:09 AM

i get that number from IRC server,and i doubt that my code will help you...

 string[] topicInfo = s.Split(new char[] { ' ' });  //"s" is the message from the server which contains the number(the time) among the other things
 string time = topicInfo[1];  



only thing that i know is that there is a function in C++ which convert these numbers to a readable date HERE
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,180
  • Joined: 18-April 07

Re: Convert univerwsal time to date

Posted 30 July 2007 - 11:36 AM

Oh just so you know, I am Martyr2, co-founder of Team-ClanX mIRC scripterz. I have been an IRCop on two networks and have about a decade of experience in mIRC scripting and IRC. ;)

Is this generated by a raw event? If so, which numeric and which network/IRCd? I do know that IRC servers use UTC time for most things, but if you are getting this from a script using $ctime, then it will be the epoch.

So feel free to speak in mIRClese if you need to.
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 univerwsal time to date

Posted 30 July 2007 - 11:44 AM

:D

yeah,it's numeric 333 on irc.omega.bg,and i do not code this in C++,just give this example with the hope that it may helps
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,180
  • Joined: 18-April 07

Re: Convert univerwsal time to date

Posted 30 July 2007 - 12:12 PM

View PostcygnusX, on 30 Jul, 2007 - 11:44 AM, said:

:D

yeah,it's numeric 333 on irc.omega.bg,and i do not code this in C++,just give this example with the hope that it may helps



Ok, so you are fetching the topic set time which will be the epoch. So the code I provided earlier will work. Now the problem turns to how you are getting that number to begin with. You are splitting it, but according to the numeric itself that number isn't going to be in the second element. It will be in either topicinfo[2] or topicinfo[3]. My first guess will be in topicinfo[3].

The numeric is formatted as so (I am assuming you have this line in your variable "s")...

333 #channel nick ctime

This should be the raw format you are getting back from the server and as you can see the ctime value will be split and put into topicinfo[3]. So try that and if it doesn't work, put in some break points on where you set your "s" variable and see what is coming in. Then step through until you get to where you set the string "time".

NOTE: Recheck your numeric value. You have too many numbers for it to be a current epoch. That epoch number would throw you about 3000 years into the future.

This post has been edited by Martyr2: 30 July 2007 - 12:13 PM

Was This Post Helpful? 0
  • +
  • -

#7 cygnusX  Icon User is offline

  • D.I.C Head

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

Re: Convert univerwsal time to date

Posted 31 July 2007 - 12:22 AM

View PostMartyr2, on 30 Jul, 2007 - 12:12 PM, said:

You are splitting it, but according to the numeric itself that number isn't going to be in the second element. It will be in either topicinfo[2] or topicinfo[3]. My first guess will be in topicinfo[3].


Actually it's really the second element because this is not the initial raw message from the server,i remove some of the information which i don't need.But really the numeric value was wrong,really sorry for that.I checked with the correct numeric value and finally things are OK.The only problem is the time zone,and there is a 3 hour difference between the date i get and the real date.BTW i'm GMT+2.The thing that i do not understand is that if i'm GMT+2 it would be 3 hour difference,not 2...?
Was This Post Helpful? 0
  • +
  • -

#8 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,180
  • Joined: 18-April 07

Re: Convert univerwsal time to date

Posted 31 July 2007 - 09:48 AM

Could you be currently observing daylight savings time? I am not sure if your country observes that right now. That would put you three hours ahead. You will want to look into the function ToLocalTime() on the datetime object which will take a UTC time and apply the UTC offset. This should get you the local time for your area.

Glad you got the date/ctime portion working now.
Was This Post Helpful? 0
  • +
  • -

#9 cygnusX  Icon User is offline

  • D.I.C Head

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

Re: Convert univerwsal time to date

Posted 31 July 2007 - 10:19 AM

Yes,you're right,using ToLocalTime() on the datetime i get the real date.Thanks for the help. :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1