LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-08-2005, 12:01 PM   #1
skie_knite007
Member
 
Registered: Dec 2004
Location: India
Distribution: Fedora Core 4
Posts: 145

Rep: Reputation: 15
how to get the time of last access frm stat


how can we obtain the time of last access frm stat.

if i declared time as integer..I'm getting an unrecognized format..


like if

int time
stat struct buff

stat("file",& buff)

time=buff.st_atime;


The stat is showing time_t datatype for time......

suppose I'm declaring time as

time_t time;
time=buff.st_atime

If I get this frm the structure,,,how can I print it like we use %d for int.

instead of %d wat shld be used.....?
 
Old 08-08-2005, 12:23 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
st_atime is a time_t which is usually defined as an unsigned long. This seems to work:
Code:
itsme@itsme:~/C$ cat atime.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main(int argc, char **argv)
{
  struct stat stbuf;

  stat(argv[0], &stbuf);

  printf("%lu\n", stbuf.st_atime);
  return 0;
}
itsme@itsme:~/C$ gcc -Wall atime.c -o atime
itsme@itsme:~/C$ ./atime
1123521643
itsme@itsme:~/C$
[/code]
You could do this too to make sure:
Code:
  printf("%lu\n", (unsigned long)stbuf.st_atime);
 
Old 08-08-2005, 12:30 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Itsme86's code outputs the time as the number of seconds since 00:00:00 on January 1, 1970 ("the epoch").

If you're looking for something like "Mon Jul 25 18:25:25 2005", use ctime() to convert the large number to a string:
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>

int main(int argc, char *argv[])
{
    struct stat stbuf;
    char *timestring;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s <path>\n", argv[0]);
        return 1;
    }
    if (stat(argv[1], &stbuf) < 0) {
        perror("Error while reading file information");
        return 1;
    }
    timestring = ctime(&stbuf.st_atime);
    printf("Last access time of %s is: %s\n", argv[1], timestring);
    return 0;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Different ip access at the same time cwhon Linux - Security 6 01-26-2005 09:36 PM
Linux stat to Windows Stat sridurai Programming 3 09-24-2004 04:07 PM
Internet access time MauricioTulua Linux - General 1 09-17-2004 02:58 PM
about /proc/$pid/stat 's start time henryluo Programming 2 06-21-2004 09:15 AM
How to access two OS the same time ? hitesh_linux Linux - Software 2 01-09-2002 01:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration