Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

UTC int Convert to DateTime How?

Posted on 2005-03-03
Medium Priority
2,425 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
• 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);
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?
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 ));

Sorry for bad copy/paste ;)

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applicationsâ€¦
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Loops Section Overview
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calculâ€¦
Suggested Courses
Course of the Month12 days, 3 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.