Logo

Programming-Idioms

  • Perl
  • Lisp

Idiom #61 Get current date

Assign to the variable d the current date/time value, in the most standard type.

(defparameter d
  (multiple-value-bind (seconds minutes hours day month year day-of-the-week daylight-savings-time-p time-zone)
(get-decoded-time)
(declare (ignorable day-of-the-week daylight-savings-time-p time-zone))
(format nil "~D-~2,'0D-~2,'0D ~2,'0D:~2,'0D:~2,'0D~%" year month day hours minutes seconds))
"The current date and time as a string value.")

GET-DECODED-TIME returns 9 values. We capture them all in MULTIPLE-VALUE-BIND. I chose to ignore day-of-the-week , daylight-savings-time-p , and time-zone , but they are still bound.
Note that Lisp's FORMAT specifier string is very different from most other languages!
$d = time;
$d = time;

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($d);

Builtin function time gets the number of seconds since epoch. localtime converts to date and time components and returns them as a list. For an OO interface, see Time::Piece.
use Time::Piece;

$d = localtime;		# local time as a Time::Piece object
say $d->ymd;		# yyyy-mm-dd format
say $d->datetime;	# ISO 8601 format

$g = gmtime;		# GMT as a Time::Piece object

Importing CPAN module Time::Piece replaces perl builtin functions localtime and gmtime with object-oriented equivalents.
with Ada.Calendar;
D : Ada.Calendar.Time := Ada.Calendar.Clock;

New implementation...
< >
programming-idioms.org