1 (in-package #:trivial-backtrace
)
3 (defparameter *date-time-format
* "%Y-%m-%d-%H:%M")
5 ;; modified from metatilities-base
6 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
7 (defmacro generate-time-part-function
(part-name position
)
11 (symbol-name 'time
) "-" (symbol-name part-name
))
13 `(eval-when (:compile-toplevel
:load-toplevel
:execute
)
14 (export ',function-name
)
16 (&optional
(universal-time (get-universal-time))
18 ,(format nil
"Returns the ~(~A~) part of the given time." part-name
)
20 (apply #'decode-universal-time
21 universal-time time-zone
))))))
23 (generate-time-part-function second
0)
24 (generate-time-part-function minute
1)
25 (generate-time-part-function hour
2)
26 (generate-time-part-function date
3)
27 (generate-time-part-function month
4)
28 (generate-time-part-function year
5)
29 (generate-time-part-function day-of-week
6)
30 (generate-time-part-function daylight-savings-time-p
7))
32 (defun date-time-string (&key
(date/time
(get-universal-time))
33 (format *date-time-format
*))
34 (format-date format date
/time nil
))
36 (defun format-date (format date
&optional stream time-zone
)
37 "Formats universal dates using the same format specifiers as NSDateFormatter. The format is:
40 %d - Day of the month as a decimal number [01-31]
41 %e - Same as %d but does not print the leading 0 for days 1 through 9
42 [unlike strftime[], does not print a leading space]
43 %H - Hour based on a 24-hour clock as a decimal number [00-23]
44 %I - Hour based on a 12-hour clock as a decimal number [01-12]
45 %m - Month as a decimal number [01-12]
46 %M - Minute as a decimal number [00-59]
47 %S - Second as a decimal number [00-59]
48 %w - Weekday as a decimal number [0-6], where Sunday is 0
49 %y - Year without century [00-99]
50 %Y - Year with century [such as 1990]
52 (declare (ignore time-zone
))
53 (let ((format-length (length format
)))
56 (loop for index
= 0 then
(1+ index
)
57 while
(< index format-length
) collect
58 (let ((char (aref format index
)))
61 (setf char
(aref format
(incf index
)))
63 ;; %% - A '%' character
64 ((char= char
#\%
) #\%
)
66 ;; %d - Day of the month as a decimal number [01-31]
67 ((char= char
#\d
) (format nil
"~2,'0D" (time-date date
)))
69 ;; %e - Same as %d but does not print the leading 0 for
70 ;; days 1 through 9. Unlike strftime, does not print a
72 ((char= char
#\e
) (format nil
"~D" (time-date date
)))
74 ;; %H - Hour based on a 24-hour clock as a decimal number [00-23]
75 ((char= char
#\H
) (format nil
"~2,'0D" (time-hour date
)))
77 ;; %I - Hour based on a 12-hour clock as a decimal number [01-12]
78 ((char= char
#\I
) (format nil
"~2,'0D"
79 (1+ (mod (time-hour date
) 12))))
81 ;; %m - Month as a decimal number [01-12]
82 ((char= char
#\m
) (format nil
"~2,'0D" (time-month date
)))
84 ;; %M - Minute as a decimal number [00-59]
85 ((char= char
#\M
) (format nil
"~2,'0D" (time-minute date
)))
87 ;; %S - Second as a decimal number [00-59]
88 ((char= char
#\S
) (format nil
"~2,'0D" (time-second date
)))
90 ;; %w - Weekday as a decimal number [0-6], where Sunday is 0
91 ((char= char
#\w
) (format nil
"~D" (time-day-of-week date
)))
93 ;; %y - Year without century [00-99]
95 (let ((year-string (format nil
"~,2A" (time-year date
))))
96 (subseq year-string
(- (length year-string
) 2))))
98 ;; %Y - Year with century [such as 1990]
99 ((char= char
#\Y
) (format nil
"~D" (time-year date
)))
102 (error "Ouch - unknown formatter '%~c" char
))))