You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -234,25 +234,53 @@ the somewhat limited functionalities as defined by the standard.
234
234
235
235
In particular, it can
236
236
237
-
- print timestamp in various standard or custom formats (e.g. RFC1123 or RFC3339)
237
+
- print timestamps in various standard or custom formats (e.g. RFC1123 or RFC3339)
238
238
- parse timestrings,
239
239
- perform time arithmetic,
240
-
- convert Unix times, timestamps, and universal times to and fro.
240
+
- convert Unix times, timestamps, and universal times to and from.
241
241
242
-
For example, here is a function that returns Unix times as a human readable string:
242
+
We present below what we find the most useful functions. See its [manual](https://common-lisp.net/project/local-time/manual.html) for the full details.
Create a timestamp with `encode-timestamp`, giving it its number of nanoseconds, seconds, minutes, days, months and years:
253
+
254
+
~~~lisp
255
+
(local-time:encode-timestamp 0 0 0 0 1 1 1984)
256
+
@1984-01-01T00:00:00.000000+01:00
257
+
~~~
258
+
259
+
The complete signature is:
260
+
261
+
**encode-timestamp** nsec sec minute hour day month year &key timezone offset into
262
+
263
+
The offset is the number of seconds offset from UTC of the locale. If offset is not specified, the offset will be guessed from the timezone. If a timestamp is passed as the into argument, its value will be set and that timestamp will be returned. Otherwise, a new timestamp is created.
264
+
265
+
Create a timestamp from a universal time with `universal-to-timestamp`:
See the [manual](https://common-lisp.net/project/local-time/manual.html) for
253
-
the full details.
274
+
You can also parse a human-readable time string:
254
275
255
-
### Get today's date
276
+
~~~lisp
277
+
(local-time:parse-timestring "1984-01-01")
278
+
@1984-01-01T01:00:00.000000+01:00
279
+
~~~
280
+
281
+
But see the section on parsing timestrings for more.
282
+
283
+
### Get today's date (now, today)
256
284
257
285
Use `now` or `today`:
258
286
@@ -266,8 +294,141 @@ Use `now` or `today`:
266
294
267
295
"today" is the midnight of the current day in the UTC zone.
268
296
297
+
To compute "yesterday" and "tomorrow", see below.
298
+
299
+
### Add or substract times (timestamp+, timestamp-)
269
300
270
-
### Formatting time strings
301
+
Use `timestamp+` and `timestamp-`. Each takes 3 arguments: a date, a number and a unit (and optionally a timezone and an offset):
302
+
303
+
~~~lisp
304
+
(local-time:now)
305
+
@2021-06-25T07:19:39.836973+02:00
306
+
307
+
(local-time:timestamp+ (local-time:now) 1 :day)
308
+
@2021-06-26T07:16:58.086226+02:00
309
+
310
+
(local-time:timestamp- (local-time:now) 1 :day)
311
+
@2021-06-24T07:17:02.861763+02:00
312
+
~~~
313
+
314
+
The available units are `:sec :minute :hour :day :year`.
315
+
316
+
This operation is also possible with `adjust-timestamp`, which can do a bit more as we'll see right in the next section (it can do many operations at once).
Here's `yesterday` and `tomorrow` defined from `today`:
327
+
328
+
~~~lisp
329
+
(defun yesterday ()
330
+
"Returns a timestamp representing the day before today."
331
+
(timestamp- (today) 1 :day))
332
+
333
+
(defun tomorrow ()
334
+
"Returns a timestamp representing the day after today."
335
+
(timestamp+ (today) 1 :day))
336
+
~~~
337
+
338
+
### Modify timestamps with any offset (adjust-timestamp)
339
+
340
+
`adjust-timestamp`'s first argument is the timestamp we operate on, and then it accepts a full `&body changes` where a "change" is in the form `(offset :part value)`:
Get all the values at once with `decode-timestamp`.
412
+
413
+
Bind a variable to a value of your choice with this convenient macro:
414
+
415
+
~~~lisp
416
+
(local-time:with-decoded-timestamp (:hour h)
417
+
(now)
418
+
(print h))
419
+
420
+
8
421
+
8
422
+
~~~
423
+
424
+
You can of course bind each time unit (`:sec :minute :day`) to its variable, in any order.
425
+
426
+
See also `(days-in-month <month> <year>)`.
427
+
428
+
429
+
### Formatting time strings (format, format-timestring, +iso-8601-format+)
430
+
431
+
local-time's date representation starts with `@`. We can `format` them as usual, with the aesthetic directive for instance, to get a usual date representation.
271
432
272
433
~~~lisp
273
434
(local-time:now)
@@ -279,7 +440,7 @@ Use `now` or `today`:
279
440
"2019-11-13T18:08:23.312664+01:00"
280
441
~~~
281
442
282
-
`format-timestring` takes a stream argument like `format`:
443
+
We can use `format-timestring`, which can be used like `format` (thus it takes a stream as first argument):
Here `nil` returns a new string. `t` would print to `*standard-output*`.
290
451
291
-
It also accepts a `:format` argument. Its default value is
292
-
`+iso-8601-format+`, with the output shown above. The
293
-
`+rfc3339-format+` format defaults to it.
452
+
But `format-timestring` also accepts a `:format` argument. We can use predefined date formats as well as give our own in s-expression friendly way (see next section).
453
+
454
+
Its default value is
455
+
`+iso-8601-format+`, with the output shown above. The `+rfc3339-format+` format defaults to it.
294
456
295
457
With `+rfc-1123-format+`:
296
458
@@ -314,7 +476,21 @@ With `+iso-week-date-format+`:
314
476
~~~
315
477
316
478
317
-
### Defining format strings
479
+
Putting all this together, here is a function that returns Unix times as a human readable string:
480
+
481
+
~~~lisp
482
+
(defun unix-time-to-human-string (unix-time)
483
+
(local-time:format-timestring
484
+
nil
485
+
(local-time:unix-to-timestamp unix-time)
486
+
:format local-time:+asctime-format+))
487
+
488
+
(unix-time-to-human-string (get-universal-time))
489
+
"Mon Jun 25 06:46:49 2091"
490
+
~~~
491
+
492
+
493
+
### Defining format strings (format-timestring (:year "-" :month "-" :day))
318
494
319
495
We can pass a custom `:format` argument to `format-timestring`.
320
496
@@ -326,7 +502,7 @@ The syntax consists of a list made of symbols with special meanings
326
502
"2019-11-13"
327
503
~~~
328
504
329
-
The list of symbols is available in the documentation: https://common-lisp.net/project/local-time/manual.html#Parsing-and-Formatting
505
+
The list of symbols is available in the documentation: [https://common-lisp.net/project/local-time/manual.html#Parsing-and-Formatting](https://common-lisp.net/project/local-time/manual.html#Parsing-and-Formatting)
330
506
331
507
There are `:year :month :day :weekday :hour :min :sec :msec`, long and
332
508
short notations ("Monday", "Mo."), gmt offset, timezone markers and
@@ -342,7 +518,7 @@ The `+rfc-1123-format+` itself is defined like this:
342
518
"See the RFC 1123 for the details about the possible values of the timezone field.")
343
519
~~~
344
520
345
-
We see the form `(:day 2)`: the 2 is for padding, to ensure that the
521
+
We see the form `(:day 2)`: the 2 is for **padding**, to ensure that the
346
522
day is printed with two digits (not only `1`, but `01`). There could be
347
523
an optional third argument, the character with which to fill the
348
524
padding (by default, `#\0`).
@@ -411,3 +587,7 @@ local-time library:
411
587
(local-time:universal-to-timestamp *)
412
588
;; @2019-11-13T19:13:15.000000+01:00
413
589
~~~
590
+
591
+
### Misc
592
+
593
+
To find out if it's Alice anniversary, use `timestamp-whole-year-difference time-a time-b`.
0 commit comments