• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

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
0
cliff_m
Asked:
cliff_m
1 Solution
 
ozoCommented:
time_t t;
t=946441724;
XmlFile << asctime(localtime(&t));
0
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now