Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Lisp Date arithmetic library

28 views
Skip to first unread message

Bruce Lester

unread,
Jun 25, 2002, 1:21:14 PM6/25/02
to
I am looking for Lisp code to calculate and format date values in a variety of formats.  Does anyone have suggestions on where I might look for this?
 
Thanks.

Erik Naggum

unread,
Jun 25, 2002, 5:23:50 PM6/25/02
to
* Bruce Lester

| I am looking for Lisp code to calculate and format date values in a variety
| of formats. Does anyone have suggestions on where I might look for this?

It is actually too simple. First you have to forget tiemzones, because they
do not generally work, not even your local one, since Common Lisp does not
interface to your operating system's timezone database in any standard way
and many implementations are severely confused outside the state where its
authors live. That makes the whole problem a lot simpler than it really is,
so you are relieved of lots of minor problems.

Then you just use decoded-universal-time and bind the values like this:

(multiple-value-bind (ss mm hh d m y dow dst zone) (decode-universal-time X)
...)

Now, the _only_ correct way to print a date is this:

(format <stream> "~4,'0D-~2,'0D-~2,'0D" y m d)

and the _only_ correct way to print a time is this:

(format <stream "~2,'0D:~2,'0D:~2,'0D" hh mm ss)

and if you have to write both in a row, use a space or a T between them.

Drop the "culturally correct" ways to write dates and times and just get with
the program. Just like floating point numbers are _not_ written with a
stupid decimal comma, and times of day are _not_ written in some ancient
12-hours system, nor with hour, minute, second in any other order, you write
year, month, and day of month in that order. No variation, no need to do it
abstractly, just do it right.

http://www.naggum.no/lugm-time.html (or .ps) might still be worth a read.

If you need to compute with days, remember that (floor <universal-time>
86400) always decomposes into a day since 1900-01-01 and the seconds the
start of the current day. There are exactly 86400 seconds in a day, all year
round.
--
Guide to non-spammers: If you want to send me a business proposal, please be
specific and do not put "business proposal" in the Subject header. If it is
urgent, do not use the word "urgent". If you need an immediate answer, give
me a reason, do not shout "for your immediate attention". Thank you.

Raymond Wiker

unread,
Jun 26, 2002, 5:05:08 AM6/26/02
to
Erik Naggum <er...@naggum.net> writes:

> If you need to compute with days, remember that (floor
> <universal-time> 86400) always decomposes into a day since
> 1900-01-01 and the seconds the start of the current day. There
> are exactly 86400 seconds in a day, all year round.

Except for leap seconds, no? (Or is that not an issue with
<universal-time>?)

--
Raymond Wiker Mail: Raymon...@fast.no
Senior Software Engineer Web: http://www.fast.no/
Fast Search & Transfer ASA Phone: +47 23 01 11 60
P.O. Box 1677 Vika Fax: +47 35 54 87 99
NO-0120 Oslo, NORWAY Mob: +47 48 01 11 60

Try FAST Search: http://alltheweb.com/

Erik Naggum

unread,
Jun 26, 2002, 8:35:32 AM6/26/02
to
* Erik Naggum

> If you need to compute with days, remember that (floor <universal-time>
> 86400) always decomposes into a day since 1900-01-01 and the seconds the
> start of the current day.

* Raymond Wiker


| Except for leap seconds, no?

Do you think I would lie about this or withhold essential facts or make a
claim that would be true "most of the time"? "There are exactly 86400
seconds in a day, all year round" was phrased that way in order to avoid
mention of the leap-second mess, but it should _still_ be crystal clear.

| (Or is that not an issue with <universal-time>?)

I think you should look this up and not believe me. This comes up again and
again because some people have heard of leap seconds and get all confused,
and unless they do some work to figure it out, they just keep asking.

Christian Lemburg

unread,
Jun 26, 2002, 4:14:15 AM6/26/02
to
"Bruce Lester" <bruce_...@email.com> writes:

See: Calendrical Calculations, Nachum Dershowitz and Edward
M. Reingold. Software-Practice and Experience 20 (1990), 899-928.

You can get the paper and the corresponding code in Common Lisp, C++,
and GNU Emacs Lisp from:

http://emr.cs.iit.edu/~reingold/calendars.shtml

--
Christian Lemburg, <lem...@aixonix.de>, http://www.clemburg.com/
Whenever you find that you are on the side of the majority, it is time
to reform.
-- Mark Twain

Sam Steingold

unread,
Jun 26, 2002, 9:20:05 AM6/26/02
to
> * In message <eE1S8.2$d%6.2...@news7.onvoy.net>
> * On the subject of "Lisp Date arithmetic library"
> * Sent on Tue, 25 Jun 2002 17:21:14 GMT

CLOCC/CLLIB/date.lisp
<http://clocc.sourceforge.net/dist/cllib.html>
<http://www.podval.org/~sds/data/cllib.html>

--
Sam Steingold (http://www.podval.org/~sds) running RedHat7.2 GNU/Linux
<http://www.camera.org> <http://www.iris.org.il> <http://www.memri.org/>
<http://www.mideasttruth.com/> <http://www.palestine-central.com/links.html>
If I had known that it was harmless, I would have killed it myself.

Julian Stecklina

unread,
Jun 26, 2002, 10:16:41 AM6/26/02
to
Erik Naggum <er...@naggum.net> writes:

[...]

> (format <stream> "~4,'0D-~2,'0D-~2,'0D" y m d)
>
> and the _only_ correct way to print a time is this:
>
> (format <stream "~2,'0D:~2,'0D:~2,'0D" hh mm ss)

Why are these the _only_ correct ways of displaying date and time?
This Euro-format date and "military" time is quite common in
Europe. (I do not like 2002-6-26... I keep using 26.6.2002 just as I
speak) But in the USA it is common to use a.m./p.m. for times (ok,
it's really cumbersome, but they like it *g*) and "their" date format. Who
cares?


Regards,
Julian

--
Meine Hompage: http://julian.re6.de

Ich suche eine PCMCIA v1.x type I/II/III Netzwerkkarte.
Ich biete als Tauschobjekt eine v2 100MBit Karte in OVP.

ozan s yigit

unread,
Jun 26, 2002, 10:19:21 AM6/26/02
to
Raymond Wiker:

> Erik Naggum <er...@naggum.net> writes:
>
> > If you need to compute with days, remember that (floor
> > <universal-time> 86400) always decomposes into a day since
> > 1900-01-01 and the seconds the start of the current day. There
> > are exactly 86400 seconds in a day, all year round.
>
> Except for leap seconds, no? (Or is that not an issue with
> <universal-time>?)

no, length of the day is always 86400 seconds. the leap second insertion
is a synchronization device to keep the difference between the solar time
and atomic time small. earth's rotation is slowing down, so a day is about
2.5ms longer than it was in 1820. that is about 1s per year, thus the
(more or less regular) insertion of a leap second.

oz
---
the dots can only be numbered after the fact - bruce schneier

Ian Wild

unread,
Jun 26, 2002, 10:34:35 AM6/26/02
to
Julian Stecklina wrote:
>
> Erik Naggum <er...@naggum.net> writes:
>
> [...]
>
> > (format <stream> "~4,'0D-~2,'0D-~2,'0D" y m d)
> >
> > and the _only_ correct way to print a time is this:
> >
> > (format <stream "~2,'0D:~2,'0D:~2,'0D" hh mm ss)
>
> Why are these the _only_ correct ways of displaying date and time?

'cos they're blessed by ISO and they work.

> Who cares?

Anyone who's ever tried to sort, eg, a list of
automatically generated file names that
include the date and time...

Erik Naggum

unread,
Jun 26, 2002, 4:55:20 PM6/26/02
to
* Julian Stecklina

| Why are these the _only_ correct ways of displaying date and time?

ISO 8601.

| This Euro-format date and "military" time is quite common in Europe.

Right, pretend it is "Euro-format". *sigh*  It was used in Japan before
Europe adopted it.

| (I do not like 2002-6-26... I keep using 26.6.2002 just as I speak)

YECCH! Yeah, I hate that, too. However, 2002-06-26 is correct.

I reply "fem på halv" if somebody asks me the time of day and my watch says
18:24:15. Writing times in this spoken manner is retarded, because it lacks
all the context of the verbal communication. (Note that I did not say which
hour it was, and rounded off to the nearest 5-minute interval.) Writing is
all about retaining _more_ context than voice communication normally does.
This is why it is hard to write as you speak and speak as you write, and why
nobody should _ever_ try to do it, either.

| But in the USA it is common to use a.m./p.m. for times (ok, it's really
| cumbersome, but they like it *g*) and "their" date format. Who cares?

Provincialism at its worst. We have international standards to avoid the
problem of trying to figure out more context than we need to use a value.
Like, I have to know which goddamn country some English writer is from in
order to figure out which day of the year he is talking about. I mean, did
anything interesting happen on November 9? I cannot recall _anything_ from
November 9. Why are you Americans so upset over November 9? I don't get it.

However, some people appear to be generally context-free, and are completely
unaware of the total lack of value in writing down things just like they say
them to people who regularly have to ask for or already know major parts of
the answer.

But who cares? I tell you who cares. There are lots of people around the
world who care about international standards in all sorts of areas, and they
have actually made serious headway into many areas previously left to random
local decisions. And, in time, standards will save mankind from its local
follies. But first, Microsoft must be destroyed.

Thomas A. Russ

unread,
Jun 26, 2002, 4:43:24 PM6/26/02
to

If you are looking for string to date conversions, there is parse-time
code in the CMUCL distribution.

--
Thomas A. Russ, USC/Information Sciences Institute t...@isi.edu

Julian Stecklina

unread,
Jun 27, 2002, 10:11:04 AM6/27/02
to
Erik Naggum <er...@naggum.net> writes:

> * Julian Stecklina
> | Why are these the _only_ correct ways of displaying date and time?
>
> ISO 8601.

I recall DIN 44300 which told us poor Germans to name this thing
turning mnemonics into machine code "Assemblierer" instead of the
established "Assembler".

> | This Euro-format date and "military" time is quite common in Europe.
>
> Right, pretend it is "Euro-format". *sigh*  It was used in Japan before
> Europe adopted it.

OK. Learnt something new.

> | (I do not like 2002-6-26... I keep using 26.6.2002 just as I speak)
>
> YECCH! Yeah, I hate that, too. However, 2002-06-26 is correct.

2002-6-26 and 26.6.2002 are as easily recognized by a computer
programme as by human eye.

> I reply "fem på halv" if somebody asks me the time of day and my watch says
> 18:24:15. Writing times in this spoken manner is retarded, because it lacks
> all the context of the verbal communication. (Note that I did not say which
> hour it was, and rounded off to the nearest 5-minute interval.) Writing is
> all about retaining _more_ context than voice communication normally does.
> This is why it is hard to write as you speak and speak as you write, and why
> nobody should _ever_ try to do it, either.

I think dates are not subject to "information loss" either spoken or
written. (At least in the named examples)

> | But in the USA it is common to use a.m./p.m. for times (ok, it's really
> | cumbersome, but they like it *g*) and "their" date format. Who cares?
>
> Provincialism at its worst. We have international standards to avoid the
> problem of trying to figure out more context than we need to use a value.
> Like, I have to know which goddamn country some English writer is from in
> order to figure out which day of the year he is talking about. I mean, did
> anything interesting happen on November 9? I cannot recall _anything_ from
> November 9. Why are you Americans so upset over November 9? I don't get it.

What about countries that do not share the Gregorian calendar?

http://www.jewfaq.org/calendar.htm

I doubt it is in much use, but nevertheless it is there.

> But who cares? I tell you who cares. There are lots of people around the
> world who care about international standards in all sorts of areas, and they
> have actually made serious headway into many areas previously left to random
> local decisions. And, in time, standards will save mankind from its local
> follies. But first, Microsoft must be destroyed.

It is said that IBM is the only company violating their own standards. :)

Erik Naggum

unread,
Jun 27, 2002, 11:04:41 AM6/27/02
to
* Julian Stecklina

| I recall DIN 44300 which told us poor Germans to name this thing turning
| mnemonics into machine code "Assemblierer" instead of the established
| "Assembler".

Well, I have also experienced personal tragedies, but try to keep those away
from completely irrelevant topics and not to let my impression of society or
any particular subgroup thereof be formed by my personal experiences. If you
have some grievances with a particular DIN standard, you have to be pretty
fucked up to take that out on standards in general. Maybe most people get
fucked up after traumatizing experiences, what do I know, but try to get a
grip on your emotional response and try not fuck up other people, too.

* Erik Naggum


> YECCH! Yeah, I hate that, too. However, 2002-06-26 is correct.

| 2002-6-26 and 26.6.2002 are as easily recognized by a computer programme as
| by human eye.

Are you blind or something? 2002-06-26 contains a leading zero so the number
of characters in the format is constant, the position of the start of each
element is predictable, etc. Some people seem to be really upset with the
use of leading zeros numbers. Why is this? Another personal tragedy inter-
fering with your normal reasoning?

You have tell the computer which format you use in order for it to get it
right. The various forms of "parse-by-hunch" that are used for dates get
some dates written by some sloppy humans or computers on their behalf wrong.

The fact is, that some people use a month-day order and some people use a
day-month order. This would be perfectly OK if the month-day people could at
least have the mental wherewithal to do year-month-day and the day-month
people could use day-month-year, and they both wrote their years with four
digits, but the month-day people are screwed up and use month-day-year, with
only two-digit years, so for the next 30 years, many dates will be ambiguous.
Sort of like because you say "ten to five" for 16:50, you would write it
50:16 or maybe -10:17 and maybe -10:17:23 to be precise. How come people do
not do this? (Well, they are so nutty that they think 12 is 0, too, so just
how much can you trust such people, anyway? They even produce household
appliances that, when they are used in Europe with 24-hour clocks, they show
24:10 instead 00:10. Since many journalists spend most of their time after
midnight staring into microwave ovens, they also report TV programs to begin
at 24:30.)

The only reason to have fun with this topic is that it is so tragic that if
we took it seriously, we would have to reinvent time as we know it. Oh,
wait, some Swiss clock-maker did just that, and thought, brilliantly, we
could use 1000 units per day instead of 86400, while other people were busy
trying to divide the second into billions if not trillions of tiny little
pieces so each interesting thing could have its own little time unit, so
clearly, nothing much happens in the life of people who have space for 1000
events per day. Still, it was pretty clever. But 86.4 seconds per beat is
like, sloooow, man, it's like you stab someone and it won't bleed until the
next beat, man. Not to mention the ultra-high precision that you would need
in order to synchronize these things. With an off-beat beat like that, you
would need high-precision clocks that could synchronize at any beat instead
of having to wait for every fifth beat to line up with a precise second. And
they cleverly made their 000 beat some stupid non-Greenwich meridian just to
piss people off. They got the idea of a world-wide uniform beat right,
though. It is time for such a thing, except it needs to be at a million
beats per day, conveniently grouped in hundreds, so sloppy people can still
use only two digits to refer to their synchronized events and precision
people can use all six digits. This has worked so well to distinguish sloppy
from precision people in the past, we should not just randomly let go of such
a simple way to gauge people. Besides, 100 large units per day would line up
pretty nicely with the good old "quarter hour". This could work! Yeah! (I
need to contact Matt Groening and have him make a new Futurama episode. Ever
noticed how a thousand years from now, they _still_ use AM/PM? *sigh* There
goes my desire to be cryogenically preserved.)

| It is said that IBM is the only company violating their own standards. :)

You just scored much lower.

Joe Marshall

unread,
Jun 27, 2002, 11:58:28 AM6/27/02
to

"Erik Naggum" <er...@naggum.net> wrote in message news:32341790...@naggum.net...

>
> The only reason to have fun with this topic is that it is so tragic that if
> we took it seriously, we would have to reinvent time as we know it. Oh,
> wait, some Swiss clock-maker did just that, and thought, brilliantly, we
> could use 1000 units per day instead of 86400, while other people were busy
> trying to divide the second into billions if not trillions of tiny little
> pieces so each interesting thing could have its own little time unit, so
> clearly, nothing much happens in the life of people who have space for 1000
> events per day.

"I've got them," said the Starbuck. "Two centons out!"

"Frak! We don't have two centons!" yelled Apollo.

A few microns beyond the burning spot, Lt. Adama pulled back on the stick...


Julian Stecklina

unread,
Jun 27, 2002, 12:43:25 PM6/27/02
to
Erik Naggum <er...@naggum.net> writes:

> | 2002-6-26 and 26.6.2002 are as easily recognized by a computer programme as
> | by human eye.
>
> Are you blind or something? 2002-06-26 contains a leading zero so the number
> of characters in the format is constant, the position of the start of each
> element is predictable, etc. Some people seem to be really upset with the
> use of leading zeros numbers. Why is this? Another personal tragedy inter-
> fering with your normal reasoning?

No. (I remember you posting: "we have lots of small things that are
black to some people and white to others" <32251304...@naggum.net>)

I just drop a character that is redundant. I personally try to write
computer programmes which adapt to humans and do not try to force
humans to adapt to the programme in most aspects. Parsing a date that
could, but not has to, contain leading zeros is of course more work for
computers and slightly more complex to implement. The result will
nevertheless be more versatile.

(Actually dates found in data files will usually be either binary
representations or in some format that is constant throughout the
file.)

> The only reason to have fun with this topic is that it is so tragic that if
> we took it seriously, we would have to reinvent time as we know it.

I would most certainly support this, but it seems to be quite
impossible to achieve this.

> | It is said that IBM is the only company violating their own standards. :)
>
> You just scored much lower.

Ok. Remind me of not telling anymore jokes in future. Perhaps I should
learn from others and restrict myself to cynicism...

Erik Naggum

unread,
Jun 27, 2002, 2:57:57 PM6/27/02
to
* Julian Stecklina

| I just drop a character that is redundant.

Oh, christ. That you think it is redundant does not make it redundant, it
only means you have not figured things out, yet. Drop the "I know better
than everyone else because I am ignorant and have a freer mind" attitude.

| Ok. Remind me of not telling anymore jokes in future. Perhaps I should
| learn from others and restrict myself to cynicism...

Wrong lesson. Try telling good jokes, instead.

Ian Wild

unread,
Jun 28, 2002, 3:49:17 AM6/28/02
to
Julian Stecklina wrote:
>
> I just drop a character that is redundant. I personally try to write
> computer programmes which adapt to humans and do not try to force
> humans to adapt to the programme in most aspects. Parsing a date that
> could, but not has to, contain leading zeros is of course more work for
> computers and slightly more complex to implement. The result will
> nevertheless be more versatile.

You're trading off a moment's convenience for a lifetime
of ambiguity and maintenance problems.

> (Actually dates found in data files will usually be either binary
> representations or in some format that is constant throughout the
> file.)

I recently had the pleasure of seeing a whole bunch of
"nightly backup" files which had, quite consistently,
included the date of backup in the file name in a "user
friendly" way. The backups for this week, when looked at
by almost any standard tool, would be ordered "28, 24, 29,
30, 27, 25, 26". Of course, you'd probably never have
noticed this, since ALL Fridays came at the top of the
list, and ALL Wednesdays come last. With April as the first
month, and September last. And the 10th comes before the 9th,
obviously. Some, in fact many, of these "nightly backup"
files were missing. Which ones, and was there a pattern?

My, how I did laugh.

--
Emacs tells me it's 10 Messidor aR 210, but won't give
me the stardate!

Joel Ray Holveck

unread,
Jun 28, 2002, 5:55:15 AM6/28/02
to
> I am looking for Lisp code to calculate and format date values in a
> variety of formats. Does anyone have suggestions on where I might
> look for this?

I don't know about formatting, but the local-time library (on cliki)
does a good job of calculations.

joelh

Joel Ray Holveck

unread,
Jun 28, 2002, 6:24:08 AM6/28/02
to

Oops, it's not on Cliki. It's at
http://alpha.onshored.com/lisp-software/

joelh

0 new messages