Skip to content

Commit

Permalink
Merge pull request #35 from skx/33-time-date
Browse files Browse the repository at this point in the history
Update date/time functions.
  • Loading branch information
skx authored Oct 15, 2022
2 parents 52ac625 + dc2a159 commit 5271d29
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
28 changes: 18 additions & 10 deletions stdlib/stdlib.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -71,39 +71,47 @@
;; as a list (DD MM YYYY). We also ahve a builtin function (time)
;; to return the time as a list (HH MM SS).
;;
;; create some helper functions for retrieving the various parts of
;; the date/time.
(set! year (fn* ()
;; Here we create some helper functions for retrieving the various
;; parts of the date/time, as well as some aliases for ease of typing.
(set! date:year (fn* ()
"Return the current year, as an integer."
(nth (date) 3)))
(set! year date:year)

(set! month (fn* ()
(set! date:month (fn* ()
"Return the number of the current month, as an integer."
(nth (date) 2)))
(set! month date:month)

(set! day (fn* ()
(set! date:day (fn* ()
"Return the day of the current month, as an integer."
(nth (date) 1)))
(set! day date:day)

(set! weekday (fn* ()
(set! date:weekday (fn* ()
"Return a string containing the current day of the week."
(nth (date) 0)))
(set! weekday date:weekday)

(set! hour (fn* ()
(set! time:hour (fn* ()
"Return the current hour, as an integer."
(nth (time) 0)))
(set! hour time:hour)

(set! minute (fn* ()
(set! time:minute (fn* ()
"Return the current minute, as an integer."
(nth (time) 1)))
(set! minute time:minute)

(set! second (fn* ()
(set! time:second (fn* ()
"Return the current seconds, as an integer."
(nth (time) 2)))
(set! second time:second)

(set! hms (fn* ()
(set! time:hms (fn* ()
"Return the current time, formatted as 'HH:MM:SS', as a string."
(sprintf "%s:%s:%s" (hour) (minute) (second))))
(set! hms time:hms)


;;
Expand Down
8 changes: 4 additions & 4 deletions time.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
;; made available by helper-functions defined in our standard-library.
;;

(print "The year is %s" (year))
(print "The date is %s/%s/%s" (day) (month) (year))
(print "The time is %s (%s seconds past the epoch)" (hms) (now))
(print "Today is a %s" (weekday))
(print "The year is %s" (date:year))
(print "The date is %s/%s/%s" (date:day) (date:month) (date:year))
(print "The time is %s (%s seconds past the epoch)" (time:hms) (now))
(print "Today is a %s" (date:weekday))

(print "Date as a list %s" (date))
(print "Time as a list %s" (time))

0 comments on commit 5271d29

Please sign in to comment.