How do I convert a Unix timestamp into a date?
I used the fstat function to get information about the modification date of a file and it returned a very large number (a unix timestamp). What function converts this into a date?
I am using C
and the bash shell
"ctime" is one reasonable choice.
It takes a "time_t" structure as input and creates
an ascii string of the date.
Consider the following program:
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int
main(int argc, char* argv[])
{
struct stat buff;
int fd = -1;
if (argc<2 || ((fd = open(argv[1], O_RDONLY)) < 0))
exit(-1);
if (fstat(fd, &buff) < 0)
perror("fstat"), exit(-1);
printf("File '%s', mod time is %s\n",
argv[1],
ctime(&buff.st_mtime));
return 0;
}
This is what it generates:
$ cc s.c
$ a.out /etc/passwd
File '/etc/passwd', mod time is Wed Feb 21 13:28:38 2007
October 1st, 2007 at 4:37 pm
You need to tell us what language you are using. It's not php, because fstat() in php gets an array containing file stats.
References :
October 5th, 2007 at 2:46 pm
"ctime" is one reasonable choice.
It takes a "time_t" structure as input and creates
an ascii string of the date.
Consider the following program:
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int
main(int argc, char* argv[])
{
struct stat buff;
int fd = -1;
if (argc<2 || ((fd = open(argv[1], O_RDONLY)) < 0))
exit(-1);
if (fstat(fd, &buff) < 0)
perror("fstat"), exit(-1);
printf("File '%s', mod time is %s\n",
argv[1],
ctime(&buff.st_mtime));
return 0;
}
This is what it generates:
$ cc s.c
$ a.out /etc/passwd
File '/etc/passwd', mod time is Wed Feb 21 13:28:38 2007
References :