How do I convert a four byte long to time/date?

Hello,
I am reading a Long (4 bytes) from a file and I need to know how to convert that to time/date. ie:
Wed Jan 02 02:03:55 1980
I know I should be looking at ctime, I just can not figure out how to make it work. Here is what I have so far:

Test::ReadHeader(const char *filename) {
            ifstream inFile( filename, ios::nocreate) ;
            unsigned long creationTimeVal ;
            inFile.read( reinterpret_cast<char *>(&creationTimeVal), 4) ;
            creationTime = creationTimeVal ;
}

Test::PrintXml(const char *filename) {
XmlFile << "\t<CreationTime>" << creationTime << "</CreationTime>\n"
}

This is the data I am currently working with:
"946441724" or "38 69 8D FC" in hex
cliff_mAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
time_t t;
t=946441724;
XmlFile << asctime(localtime(&t));
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KangaRooCommented:
In standard C library, date/times are represented by a time_t, usually an unsigned, 32 bit integer (unsigned long). It represents the time in seconds elapsed since 00:00:00 GMT, January 1, 1970
Assuming that this is the same representation as in the file you are reading from you read it in ok, but you write the long value in the output, not what you want.

ctime() will convert this time_t value into a human readable date/time string.
localtime() converts the time_t value to a structure tm representing the time.
asctime() converts a tm structure to a human readable date/time string

=============================================================================
time_t (type)

Defined In

time.h
sys\types.h

Description

This variable type defines the value used by the time functions declared in time.h.
The time_t value will not work after the hour of 3:14:07 on the year 01/19/2038.
The functions that use the time_t value are as follows:

char ctime(const time_t  *time);
double difftime(time_t time2, time_t time1);
struct tm * gmtime(const time_t  *timer);
struct tm * localtime(const time_t  *timer);
time_t time(time_t  *timer);
time_t mktime(struct tm  *timeptr);
int    stime(time_t  *tp);

0
carldeanCommented:
If you're using VC++, and if you are thinking about CTime then it sounds like you might be, you should look at serialisation - see CArchive and CObject.

It makes the reading and writing of objects to a file quite simple.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

sumant032199Commented:
If the 32 bit long integer is a Bit-Field then please inform me how the bits are divided into number of groups. This info may be available in HELP of the library function you are using to find time.
0
KangaRooCommented:
>> ...  look at serialisation ...
Looking at the names cliff_m choose for his objects it seems serialization is not an option....
>> ... the 32 bit long integer is a Bit-Field ...
The 32 bit time_t used in C is a common unsigned long which 'counts' the seconds that have elapsed since Jan 1, 1970. This doesn't mean the value that cliff_m is reading from the file is in that 'format'
0
cliff_mAuthor Commented:
Thank you ozo for answering my question so fast. I appreciate everyone else's input, but ozo was first and was really all I needed. The format that is in the file is in the "epoch" format which I think is synonomous with the 32bit time_t.

Kangaroo - Thank you for all of your info.

carldean - You were right. I am using Win32 and Visual Studio. I will look into serialization.

As you all have probably noticed, I am very new to C and C++ programming. I have a lot to learn and I appreciate all of your help.

I ended up using this in my program:

time_t creationTime ;
long creationtimeVal ;

inFile.read( reinterpret_cast<char *>(&creationTimeVal), 4) ;
creationTime = creationTimeVal ;

cout << ctime(creationTime) ;
0
cliff_mAuthor Commented:
$MyComment =~ s/synonomous/synonymous/ ;

:^)

Cliff
0
cliff_mAuthor Commented:
Ozo if you want the points, post an answer.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.