Get full time without seconds

Hello,

I have a code to check the last time modification of a file using "gmtime".
Is it possible to remove the seconds in the result?

Here is my code:
1
2
3
4
5

struct tm* clock;				// create a time structure	
struct stat attrib;			// create a file attribute structure 
stat("test.txt", &attrib);		// get the attributes of afile.txt	
clock = gmtime(&(attrib.st_mtime));	// Get the last modified time and put it into the time structure 
1
2
3
4
5
6
7
8
struct stat attrib ;
::stat("test.txt", &attrib);

// make a copy of the std::tm
std::tm time_modified = *std::gmtime( &(attrib.st_mtime) ) ;

// modify the copy as you please
time_modified.tm_sec = 0 ; // set seconds to zero 
Is it possible to remove the seconds in the result?

An alternative approach to JBorges's suggestion is to exploit integer division and the fact that a time_t is in units of seconds to truncate the time_t rather than the struct tm, e.g.

attrib.st_mtime = (attrib.st_mtime / 60) * 60; // truncate

The two approaches are no better or worse if you want to truncate the time to the minute. But if you want to round instead, the time_t based approach is better than the struct tm approach as you don't have to worry about times within a second of midnight.

attrib.st_mtime = (time_t)(attrib.st_mtime / 60.0 + 0.5) * 60; // round

(I assume you are not just wanting to lose the seconds for display purposes.)

Andy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <ctime>
using namespace std;

void display_time(const char* label, time_t the_time);

int main()
{
    time_t time_now = time(0);
    time_t time_trunc = (time_now / 60) * 60;
    time_t time_round = (time_t)(time_now / 60.0 + 0.5) * 60;

    display_time("time now  ", time_now);
    cout << endl;
    display_time("time trunc", time_trunc);
    cout << endl;
    display_time("time round", time_round);
    cout << endl;

    return 0;
}

void display_time(const char* label, time_t the_time)
{
    if(0 == label)
        label = "the time";
    struct tm* tm_ptr = gmtime(&the_time);
    char buffer[256] = "";
    strftime(buffer, sizeof(buffer), "%c", tm_ptr);
    cout << label << " : " << buffer << endl;
}


Output:

time now   : 06/21/13 09:47:42

time trunc : 06/21/13 09:47:00

time round : 06/21/13 09:48:00
Last edited on
JLBorges your code give me an error :
error C2039: 'gmtime' : is not a member of 'std'

I am on Visual c++ 2008
PurpleKush, have you included the correct header for gmtime?

Both gmtime and tm are listed under <ctime> in the following:
http://www.cplusplus.com/reference/ctime/gmtime/
http://www.cplusplus.com/reference/ctime/tm/
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <ctime> // ******* C++ header for std::gmtime() etc.

int main()
{
    std::time_t now = std::time(nullptr) ;

    // make a copy of the std::tm
    std::tm modified_time = *std::gmtime( &now ) ;

    // modify the copy as you please
    modified_time.tm_sec = 0 ; // set seconds to zero
    modified_time.tm_year += 6 ; // move to six years later
    // etc...

    std::cout << "UTC: " << std::asctime( &modified_time ) ;
}

http://ideone.com/DBgoq2
make a copy of the std::tm

For those who are new to the gmtime call, be aware that:

The returned value points to an internal object whose validity or
value may be altered by any subsequent call to gmtime or localtime.

From:

gmtime
http://www.cplusplus.com/reference/ctime/gmtime/

So if you are making multiple calls to gmtime or localtime, you might want to make an actual copy of the struct pointed to be the returned pointer for safety.

Or use one the safer, but not so standard, alternative functions (these both take the pointer to a struct tm which they then return to you):

POSIX

1
2
struct tm *gmtime_r(const time_t *restrict timer,
       struct tm *restrict result);


gmtime
http://pubs.opengroup.org/onlinepubs/009695399/functions/gmtime.html

Microsoft CRT

1
2
3
4
errno_t gmtime_s(
   struct tm* _tm,
   const __time_t* time
);


gmtime_s, _gmtime32_s, _gmtime64_s
http://msdn.microsoft.com/en-us/library/3stkd9be%28v=vs.100%29.aspx

Andy
Last edited on
Topic archived. No new replies allowed.