root/lang/gauche/oldtype/branches/stable/Kahua/oldtype/oldtype/pasttime.scm @ 30558

Revision 21138, 2.5 kB (checked in by kiyoka, 5 years ago)

sync from trunk

Line 
1;;;
2;;; oldtype/pasttime - how long has it been passed since ...?
3;;;
4;;;  (This source code devived from wiliki.pasttime...)
5;;;
6;;;
7;;;  Copyright (c) 2000-2003 Shiro Kawai, All rights reserved.
8;;;
9;;;  Permission is hereby granted, free of charge, to any person
10;;;  obtaining a copy of this software and associated documentation
11;;;  files (the "Software"), to deal in the Software without restriction,
12;;;  including without limitation the rights to use, copy, modify,
13;;;  merge, publish, distribute, sublicense, and/or sell copies of
14;;;  the Software, and to permit persons to whom the Software is
15;;;  furnished to do so, subject to the following conditions:
16;;;
17;;;  The above copyright notice and this permission notice shall be
18;;;  included in all copies or substantial portions of the Software.
19;;;
20;;;  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21;;;  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22;;;  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23;;;  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24;;;  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
25;;;  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
26;;;  OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27;;;  IN THE SOFTWARE.
28;;;
29;;;  $Id: pasttime.scm,v 1.3 2003-08-30 12:28:00 shirok Exp $
30;;;
31
32(define-module oldtype.pasttime
33  (export how-long-since))
34(select-module oldtype.pasttime)
35
36;; Multilingualizaton of this module is tricky, as the rules of
37;; forming plurals are different from language to language.
38;; See GNU's gettext document for the problem.
39;; For now, I only support English.
40
41(define-constant secs-in-a-year  31557600)
42(define-constant secs-in-a-month 2629800)
43(define-constant secs-in-a-day   86400)
44(define-constant secs-in-an-hour 3600)
45(define-constant secs-in-a-minute 60)
46
47(define (how-long-since time . opts)
48  (define (pl num unit)
49    (format "~a ~a~a" num unit (if (= num 1) "" "s")))
50  (let-optionals* opts ((now (sys-time)))
51    (let ((diff (- now time)))
52      (cond
53       ((>= diff secs-in-a-year)
54        (pl (quotient diff secs-in-a-year) "year"))
55       ((>= diff secs-in-a-month)
56        (pl (quotient diff secs-in-a-month) "month"))
57       ((>= diff secs-in-a-day)
58        (pl (quotient diff secs-in-a-day) "day"))
59       ((>= diff secs-in-an-hour)
60        (pl (quotient diff secs-in-an-hour) "hour"))
61       ((>= diff secs-in-a-minute)
62        (pl (quotient diff secs-in-a-minute) "minute"))
63       (else
64        (pl diff "second")))
65      )))
66
67(provide "oldtype/pasttime")
68
Note: See TracBrowser for help on using the browser.