LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Weired ctime() asctime() output (https://www.linuxquestions.org/questions/programming-9/weired-ctime-asctime-output-199926/)

azi 07-01-2004 10:46 AM

Weired ctime() asctime() output
 
Erm the code firs :} :

Code:

#include <stdio.h>
#include <utmp.h>
#include <string.h>
#include <time.h>
 
int main(  )
 
{
 
 
 
 
        struct lastlog vsebina_lastloga ;
        FILE *datoteka ;
 
        if ((datoteka = fopen("/var/log/lastlog", "r+")) == NULL)
 
        {
                fprintf(stderr,"Napaka pri odpiranju datoteke\n");
                return (9);
 
        }
 
 
                struct tm faketime ;
 
                faketime.tm_sec = 40;
                faketime.tm_min = 54;   
                faketime.tm_hour = 21 ;
                faketime.tm_mday = 12 ;
                faketime.tm_mon = 3; 
                faketime.tm_year = 304 ;       
                faketime.tm_wday =  5 ;
                faketime.tm_yday = 300 ;
 
 
        datoteka = fopen("/var/log/lastlog", "r+") ;
        fread(&vsebina_lastloga, sizeof(struct lastlog), 1, datoteka);
 
        printf("%s\n%s\n" , asctime(&faketime), ctime(&vsebina_lastloga.ll_time)) ;
 
        fclose(datoteka) ;
        return 0 ;
 
}

Now what i don't understand is how can this printf " printf("%s\n%s\n" , asctime(&faketime), ctime(&vsebina_lastloga.ll_time)) " prints two IDENTICAL values like this :
bash-2.05b# ./a.out
Fri Apr 12 21:54:40 2204

Fri Apr 12 21:54:40 2204

Can somebody explain me how can this succede ?

jim mcnamara 07-01-2004 04:48 PM

This code:
Code:

#include <stdio.h>
#include <time.h>   
#include <errno.h>
int main(int argc, char *argv[]  )


    struct tm faketime ;
    time_t ll_time=86400 * 2000;
    faketime.tm_sec = 40;
    faketime.tm_min = 54;
    faketime.tm_hour = 21 ;
    faketime.tm_mday = 12 ;
    faketime.tm_mon = 3;
    faketime.tm_year = 304 ;
    faketime.tm_wday =  5 ;
    faketime.tm_yday = 300 ;   
    printf("%s\n", asctime(&faketime) );
    printf("errno =%d\n", errno);
    printf("%s\n" , ctime(&ll_time)) ;
    printf("errno =%d\n", errno);
    return 0 ;
}

Gives me this output:

Fri Apr 12 21:54:40 2204

errno =0
Mon Jun 23 18:00:00 1975

errno =0

You do know that the implementation of asctime creates allocates memory and creates a temporary struct tm


All times are GMT -5. The time now is 05:11 PM.