Solved

# UTC int Convert to DateTime How?

Posted on 2005-03-03
Medium Priority
2,424 Views
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.
0
Question by:donschli
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 4
• 2

LVL 10

Expert Comment

ID: 13454541
Where are you getting this UTC int value from?
0

LVL 2

Expert Comment

ID: 13454873
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.
0

Author Comment

ID: 13454960
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
0

LVL 2

Expert Comment

ID: 13455032
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);

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

Are you sure 1104178047 is the date of 2005-03-02?
0

Author Comment

ID: 13455097
>>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.
0

LVL 10

Expert Comment

ID: 13455253
Where are you getting this UTC int value from?
0

Author Comment

ID: 13455257
A Cisco phone system.
0

LVL 2

Expert Comment

ID: 13455266
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
0

LVL 2

Expert Comment

ID: 13455275
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...
0

LVL 2

Accepted Solution

klingzor earned 2000 total points
ID: 13455363
If you're trying this in your c++ compiler it should be
printf( "Coordinated universal time is %s\n", asctime( newtime ));

I suspect that when you tried the gmtime function, you accidentally passed the current datetime to it. If you tried it the day before you posted your question, that will explain the 2005-03-02 date...
0

Author Comment

ID: 13478768
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!!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeâ€¦
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as formâ€¦
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a qâ€¦
###### Suggested Courses
Course of the Month13 days, 15 hours left to enroll