何秒前にさわったファイルなのかを調べる

30分プログラム、その586。何秒前にさわったファイルなのかを調べてみる。
おおむねhttp://gauc.no-ip.org/awk-users-jp/blis.cgi/DoukakuAWK_129にインスパイアされてます。ただ、10秒より前かどうかを出力するのでなく、何秒前に触られたのかを出力するようにしてみました。

使い方

# ファイルを作った直後は、0秒
$ touch hoge && gosh atime.scm hoge
hoge    0 sec before

# 10秒後に実行してみる
$ gosh atime.scm hoge
hoge    10 sec before

ソースコード

#! /opt/local/bin/gosh
;; -*- mode:scheme; coding:utf-8 -*-
;;
;; atime.scm -
;;
;; Copyright(C) 2009 by mzp
;; Author: MIZUNO Hiroki / mzpppp at gmail dot com
;; http://howdyworld.org
;;
;; Timestamp: 2009/05/17 20:39:09
;;
;; This program is free software; you can redistribute it and/or
;; modify it under MIT Lincence.
;;
(use srfi-1)

(define (path->atime path)
  (slot-ref (sys-stat path) 'atime))

(define (path->diff-time path)
  (let1 now (sys-time)
	(- now (path->atime path))))

(define (diff-time->string t)
  (cond
   [(< t 60)           (format #f "~d sec before"   t)]
   [(< t (* 60 60))    (format #f "~d min before"   (floor (/. t 60)))]
   [(< t (* 60 60 24)) (format #f "~d hour before"  (floor (/. t 60 60)))]
   [else               (format #f "~,2d day before" (floor (/. t 60 60 24)))]))


(define (main args)
  (for-each (lambda (xs) (print (car xs) "\t" (cadr xs)))
	    (zip (cdr args)
		 (map (compose diff-time->string path->diff-time) (cdr args))))
  0)