RSS Feed

Lisp Project of the Day


You can support this project by donating at:

Donate using PatreonDonate using Liberapay

Or see the list of project sponsors.


This is a library for time manipulation. May be you heard about "local-time-duration" system? "Periods" is a "local-time-duration" on steroids.

With "periods" you can generate sequences of dates by defining special rules.

In it's earlier version, this library was tightly integrated with "Series" but this commit broke that functionality.

Actually, the pull intended to separate Period's integration with Series, but it has broken it because nobody cares.

I think this integration with Series is cool. So, I've fixed it.

Let's see how does it work!

This example shows how to create an infinite sequence of dates with 14 days interval between them.

I specified a starting date and interval's duration:

POFTHEDAY> (local-time:enable-read-macros)

POFTHEDAY> (periods-series:scan-times @2007-11-18
                                      (periods:duration :days 14))

The library contains other functions for relative date definition.

I was able to reproduce a more complex example from the documentation.

It generates an infinite sequence of dates, where each date is the second Friday of month.

POFTHEDAY> (series:mapping ((time (periods-series:scan-times
                                   (periods::previous-time @2020-04-02
                                                          (periods::relative-time :day 1))
                                   (periods:duration :months 1))))
             (periods::next-time (periods::next-time
                                  (periods::relative-time :day-of-week 5)
                                  :accept-anchor t)
                                 (periods::relative-time :day-of-week 5)))

Now let's go step by step to understand what happens under the hood.

This form gives as a date pointing to the beginning of the current month:

POFTHEDAY> (periods::previous-time @2020-04-02
                                   (periods::relative-time :day 1))

Next, we are defining a generator which iterates by months starting from the current one:

POFTHEDAY> (periods-series:scan-times
            (periods::previous-time @2020-04-02
                                    (periods::relative-time :day 1))
            (periods:duration :months 1))

As you might notice, a sequence starts from May, skipping the second Friday of April. Probably it is a bug inside the "scan-times" because it generates a sequence, skipping the initial month:

POFTHEDAY> (series:subseries
             (periods:duration :months 1))
            0 5)

But maybe this is not a bug in the library, but a bug in the example.

Anyway, now we have an infinite sequence of dates where each date, is the beginning of the month. We need to shift these dates to the second Friday.

Function "periods::next-time" combined with "periods::relative-time" does what we need. It moves given time forward according to the given relative time definition.

For example, today is 2 April, Thursday. But we want to move the date to the next Friday:

POFTHEDAY> (periods::next-time @2020-04-02
                               (periods::relative-time :day-of-week 5))

But if the current day is already Friday, then this call will give us the next Friday. Beginning of the month can be a Friday. In this case, calling "next-time" twice will give us the third Friday instead of the second.

To prevent this but, in the code I've shown, the first call to the "next-time" have a special flag ":accept-anchor". It says literally: "Hey, if date already is Friday, then don't move!"

That is how does this complex example work.

"Periods" documentation has one more interesting example, where you are iterating by 15 days intervals, but skipping the weekends:

POFTHEDAY> (series:mapping ((time (periods-series:scan-times
                                   (periods:duration :days 15))))
             (if (periods:falls-on-weekend-p time)
                 (periods::next-time time
                                      :day-of-week 1))

Note, that 04 April + 15 days will be 19 April, but it is a Sunday. Because of this, a date in our sequence was moved to the next Monday - 20 April.

Working with time is hard.

I think this cool library needs some love - a few fixes in package definition and better documentation.

Worldwide pandemic is the best period to contribute to Opensource! Go and make a pull request to the

Brought to you by 40Ants under Creative Commons License