Link to home
Start Free TrialLog in
Avatar of donschli
donschli

asked on

UTC int Convert to DateTime How?

I have a UTC int, how do I convert this to a standard datetime.  Example, the UTC int 1104178047 is the date of 2005-03-02.    I need both date and time.  There are several proposed solutions I've found on the web, but they are incorrect.  Most involve counting seconds, but this does not work.  C++ has a function GMTime, which works great.  I'm looking for something similar in c#.  This problem requires an understanding of UTC, a typical date solution will not work.
Avatar of eternal_21
eternal_21

Where are you getting this UTC int value from?
I take it you've already looked at the DateTime.ToUniversalTime() without success...

Without knowing which format your UTC int is, it's impossible to tell.
Avatar of donschli

ASKER

Here is a UTC int example, 1104178047 .
DateTime.ToUniversalTime() seems to convert std time to UTC.  I'm looking for UTC int to datetime.  If I'm incorrect, please example.
Thanks
Well, maybe not totally impossible. You said that it worked with the c++ gmtime function. The input parameter of the gmtime function is a t_time value, which is expressed as the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC (from a c++ reference)

Is this correct?

Then it should be:
DateTime dt = new DateTime(1970, 1, 1);
dt = dt.AddSeconds(1104178047); // your UTC int

But this returns 2004-12-27 20:07:27

Are you sure 1104178047 is the date of 2005-03-02?
>>Are you sure 1104178047 is the date of 2005-03-02?
Yes, C++ GMTime function also confirms.
There turns out to be more to UTC than just a seconds counter.  I dont not know the algorithm though.
Where are you getting this UTC int value from?
A Cisco phone system.
Well, the C++ reference says it's just a seconds counter, and there is nothing special about UTC, its just a universal timezone, same as the old GMT.

Have a look at this:
http://msdn.microsoft.com/library/en-us/vccore98/HTML/_crt_gmtime.asp
I tried this in a c++ compiler:

int main(int argc,char *argv[])
{
   struct tm *newtime;
   long ltime = 1104178047; // your UTC int

   /* Obtain coordinated universal time: */
   newtime = gmtime( &ltime );
   printf( "Coordinated universal time is %s\n",( newtime ) );
}

this returns Mon Dec 27 20:07:27 2004

I think your error must be elsewhere...
ASKER CERTIFIED SOLUTION
Avatar of klingzor
klingzor

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
klingzor, you are correct.  At the begining, my sample number 1104178047, was incorrect.  After your post, I created new data this morning, 1110216826, C++ correctly returns 3/7/05 11:33:46.  Also TSQL select dateadd(s, 1110216826 , '1/1/1970') returns correct.  I have tons of scripts to deal with GMT offsets now that I can see the forest.  Thanks again!!