You are not logged in.

#1 2013-04-21 17:42:41

lspci
Member
From: Virginia, USA
Registered: 2012-06-09
Posts: 242

[SOLVED] (clisp) getting a file's last modification time as date

Okay, so I've searched both stackoverflow and google multiple times looking for an example of how to get the age of a file inside of common lisp, but I didn't find anything useful. 

EDIT: Oh, and I also took a look at the list of topics available on Rosetta Code for Common Lisp, but to no avail. 

I, however, did remember an apparition of lisp named NewLISP and quickly discovered a means of doing the above in NewLISP:

> (date (file-info "%home%palantir%repos%core%scripts%live.sh" -1))
"Sun Apr 21 01:16:53 2013"

If anyone knows how to do that in Common Lisp I would be most obliged.


NOTE: the above file's name is weird like that because it's an undofile created by vim.  I wrote a shell script to delete files that are exactly 20 days old, but I couldn't figure out how to say

Oh, and delete files that are older than 20 days too

because I was using grep and ls to collect files that are 20 days old.  However, I'm confident that I could rewrite it in lisp with this added functionality.

Last edited by lspci (2013-04-23 19:12:04)


Please don't be a help vampire. | Bitbucket

Give a little more for a little less today.  smile

Offline

#2 2013-04-21 21:49:31

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,932

Re: [SOLVED] (clisp) getting a file's last modification time as date

(Disclaimer: I don't actually know Common Lisp, but I was curious simply because I'm working with Clojure and am about to start SICP with some dialect of Scheme. So, hopefully this is useful information.)

I think that you're looking for file-write-date. It should tell you the last time that a file was either written to or created. From there, you should be able to write your 20 day function based on the output of that combined with something like get-universal-time.

Offline

#3 2013-04-22 21:20:08

lspci
Member
From: Virginia, USA
Registered: 2012-06-09
Posts: 242

Re: [SOLVED] (clisp) getting a file's last modification time as date

skottish wrote:

(Disclaimer: I don't actually know Common Lisp, but I was curious simply because I'm working with Clojure and am about to start SICP with some dialect of Scheme. So, hopefully this is useful information.)

I think that you're looking for file-write-date. It should tell you the last time that a file was either written to or created. From there, you should be able to write your 20 day function based on the output of that combined with something like get-universal-time.

File-write-date does the trick, thank you. 


SOLUTION

;; here's one way to access a file's write date:

;; Necessary functions START

;; GET NTH ITEM FROM UNIVERSAL TIME 
(defun get-nth-from-universal-time (date nth-item)
  (nth-value nth-item (decode-universal-time date)) )

;; GET DAY FROM UNIVERSAL TIME

(defun get-day-from-universal-time (date)
  (get-nth-from-universal-time date 3))

;; GET MONTH FROM UNIVERSAL TIME

(defun get-month-from-universal-time (date)
  (get-nth-from-universal-time date 4))

;; GET YEAR FROM UNIVERSAL TIME

(defun get-year-from-universal-time (date)
  (get-nth-from-universal-time date 5))

;; Necessary Functions END

(let ((file-write-day (get-day-from-universal-time (file-write-date (pathname "~/path/to/file/filename.moo")) ) ) ;; pathname resolves "~/" to "/home/$USER"
        (file-write-month (get-month-from-universal-time (file-write-date (pathname "~/path/to/file/filename.moo")) ) )
        (file-write-year (get-year-from-universal-time (file-write-date (pathname "~/path/to/file/filename.moo")) ) ))
;; now just place whichever variable you want the let to return here
file-write-day) ;;                                             <--'    

EDIT: it's worth noting that I was working in clisp when I wrote the above; I have no idea whether it is compatible with other implementations of common lisp.  (My main concern is that nth-value may not exist in other impls)

Last edited by lspci (2013-04-23 19:21:57)


Please don't be a help vampire. | Bitbucket

Give a little more for a little less today.  smile

Offline

#4 2013-04-23 16:55:55

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,932

Re: [SOLVED] (clisp) getting a file's last modification time as date

I don't know if multiple-value-list is common throughout CL, but I believe that you can achieve the same thing as nth-value here using it and nth:

(defun get-nth-from-universal-time (date nth-item)
  (nth nth-item (multiple-value-list (decode-universal-time date))))

Offline

#5 2013-04-23 17:33:58

Stefan Husmann
Member
From: Germany
Registered: 2007-08-07
Posts: 1,016

Re: [SOLVED] (clisp) getting a file's last modification time as date

lspci wrote:
skottish wrote:

(Disclaimer: I don't actually know Common Lisp, but I was curious simply because I'm working with Clojure and am about to start SICP with some dialect of Scheme. So, hopefully this is useful information.)

I think that you're looking for file-write-date. It should tell you the last time that a file was either written to or created. From there, you should be able to write your 20 day function based on the output of that combined with something like get-universal-time.

File-write-date does the trick, thank you. 


SOLUTION

;; here's one way to access a file's write date:

;; Necessary functions START

;; GET NTH ITEM FROM UNIVERSAL TIME 
(defun get-nth-from-universal-time (date nth-item)
  (nth-value nth-item (decode-universal-time date)) )

;; GET DAY FROM UNIVERSAL TIME

(defun get-day-from-universal-time (date)
  (get-nth-from-universal-time date 3))

;; GET MONTH FROM UNIVERSAL TIME

(defun get-month-from-universal-time (date)
  (get-nth-from-universal-time date 4))

;; GET YEAR FROM UNIVERSAL TIME

(defun get-year-from-universal-time (date)
  (get-nth-from-universal-time date 5))

;; Necessary Functions END

(let ((file-write-day (get-day-from-universal-time (file-write-date "~/path/to/file/filename.moo")) )
        (file-write-month (get-month-from-universal-time (file-write-date "~/path/to/file/filename.moo")) )
        (file-write-year (get-year-from-universal-time (file-write-date "~/path/to/file/filename.moo"))) )
;; now just place whichever variable you want the let to return here
file-write-day) ;;                                             <--'    

EDIT: it's worth noting that I was working in clisp when I wrote the above; I have no idea whether it is compatible with other implementations of common lisp.  (My main concern is that nth-value may not exist in other impls)

In sbcl this seems to fail, but in xcl it works.

Offline

#6 2013-04-23 19:19:57

lspci
Member
From: Virginia, USA
Registered: 2012-06-09
Posts: 242

Re: [SOLVED] (clisp) getting a file's last modification time as date

skottish wrote:

I don't know if multiple-value-list is common throughout CL, but I believe that you can achieve the same thing as nth-value here using it and nth:

(defun get-nth-from-universal-time (date nth-item)
  (nth nth-item (multiple-value-list (decode-universal-time date))))

Good point, and thanks.  smile

Stefan Husmann wrote:

In sbcl this seems to fail, but in xcl it works.

This should work in SBCL (I checked) as long as you use pathname to resolve for "~/"--I added the pathname thing to my original solution post thing.

Last edited by lspci (2013-04-23 22:25:29)


Please don't be a help vampire. | Bitbucket

Give a little more for a little less today.  smile

Offline

Board footer

Powered by FluxBB