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
  

2 Responses

  1. tiggsy Says:

    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 :

  2. morgan Says:

    "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 :

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Posted on August 14th, 2008 by admin and filed under unix for |

|