How to get iso_date (YYYYMMDD) with time_t / timeval

Given time_thow 1291121400, how do I get the date of this day, formatted as 20101130?

+3
source share
4 answers

The following worked for me:

int iso_date_from_time_t ( const time_t & in_time_t_ ) 
{
     tm temp_this_tm_;

     { // the following to set local dst fields of struct tm ?
         time_t tvsec_ = time(NULL);
         localtime_r ( & tvsec_, & temp_this_tm_ ) ;
     }
     localtime_r ( & in_time_t, & temp_this_tm_ ) ;

     return ( ( ( ( 1900 + temp_this_tm_.tm_year ) * 100 + ( 1 + temp_this_tm_.tm_mon ) ) * 100 ) + temp_this_tm_.tm_mday ) ;
}

Thank you for your help.

0
source

Use gmtime(3)or localtime(3)to convert it to struct tm (Or, better, reentrant versions of gmtime_ror localtime_r), and then use strftime(3)to turn it into a string. For example, if you want to output in UTC format:

struct tm tm;
char buf[9];
gmtime_r(&my_time_t, &tm);
strftime(buf, sizeof(buf), "%Y%m%d", tm);
printf("The date is: %s\n", buf);
+5
source

Use the functions gmtimeor localtimeand strftime.

0
source
void function () 
{
    time_t     current_time;
    struct tm *struct_time;

    time( &current_time);

    struct_time = gmtime( &current_time);

    /* Now, you can get the ISO date by 
     * YYYY 'struct_time->tm_year+1900' 
     * MM 'struct_time->tm_mon+1'
     * DD 'struct_time->tm_mday' */
}

Please take a look inside the struct tm structure.

0
source

Source: https://habr.com/ru/post/1790301/


All Articles