File last access date in UNIX (Solaris)

I'm using the stat function in perl to obtain an array of elements that describe the files being examined.  One of the elements in the array is the file's date of last access made by anyone.  However, the act of reading the file's date of last access changes that value to the date/time that the file's date of last access is read, which is the current date and time.  Is there a way to read the file's date of last access without changing it in the process?
firebloodSAS ProgrammerAsked:
Who is Participating?
Brian UtterbackConnect With a Mentor Principle Software EngineerCommented:
You must be doing something other than just running stat on the file. I attached a test perl script and here is what I get when I run it:

# ~/
Create file and display the access time
-rw-r--r--   1         10 Apr 23 07:01 testfile

Get the atime and display it
Apr 23 07:01:46

Wait a while

Get the atime again and display it
Apr 23 07:01:46

What is the access time now?
-rw-r--r--   1          10 Apr 23 07:01 testfile

Now we really access it then display access time.
-rw-r--r--   1          10 Apr 23 07:02 testfile

As you can see, the access time did not update when run stat on the file.
robocatConnect With a Mentor Commented:
Why not modify the access time of the file back to the value you just read? Of course assuming you have enough permissions.
Gerwin Jansen, EE MVEConnect With a Mentor Topic Advisor Commented:
Using stat on a file handle in perl is not changing accessed, modified or changed timestamps. How are you reading the access timestamp of a file?
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Brian UtterbackPrinciple Software EngineerCommented:
Oops, somehow I missed the file. Here it is. I had to change the extension becase EE doesn't accept files with extension .pl.
Gerwin Jansen, EE MVETopic Advisor Commented:
@blu - Works fine in Linux for me as well, did you test Solaris?
Brian UtterbackPrinciple Software EngineerCommented:
My test was on Solaris.
firebloodSAS ProgrammerAuthor Commented:
Thanks to the three of you, the key insight for me here was that I was asking for a workaround to a problem that didn't exist.  My code was accessing the files in a way that I didn't recognize, and when you made it clear that just doing a stat would not change the last access time stamp I went back and then confirmed that you were correct.  Thanks very much to all three.
Gerwin Jansen, EE MVETopic Advisor Commented:
You're welcome!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.