0

Program:

#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
void main()
{
    struct stat stbuf;
    stat("alphabet",&stbuf);
    printf("Access time  = %d\n",stbuf.st_atime);
    printf("Modification time  = %d\n",stbuf.st_mtime);
    printf("Change time  = %d\n",stbuf.st_mtime);
}

The above program gives the following output:

Output:

$ ./a.out 
Access time  = 1441619019
Modification time  = 1441618853
Change time  = 1441618853
$

It print the date in seconds. In C, what is the way to print the time as human readable format which is returned by stat function. Return type of stbuf.st_atime is __time_t.

Thanks in Advance...

mohangraj
  • 9,842
  • 19
  • 59
  • 94

1 Answers1

7

Try to use char* ctime (const time_t * timer); function from time.h library.

#include <time.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main(void)
{
    struct stat stbuf;
    stat("alphabet", &stbuf);
    printf("Access time  = %s\n", ctime(&stbuf.st_atime));
    printf("Modification time  = %s\n", ctime(&stbuf.st_mtime));
    printf("Change time  = %s\n", ctime(&stbuf.st_mtime));
}

It will give your following result:

$ ./test
Access time  = Mon Sep 07 15:23:31 2015

Modification time  = Mon Sep 07 15:23:31 2015

Change time  = Mon Sep 07 15:23:31 2015
greybeard
  • 2,249
  • 8
  • 30
  • 66
ales-by
  • 86
  • 3