Link to home
Start Free TrialLog in
Avatar of cliff_m
cliff_m

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

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
Avatar of KangaRoo
KangaRoo

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);

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.
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.
>> ...  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'
Avatar of cliff_m

ASKER

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) ;
Avatar of cliff_m

ASKER

$MyComment =~ s/synonomous/synonymous/ ;

:^)

Cliff
Avatar of cliff_m

ASKER

Ozo if you want the points, post an answer.