Package to parse dates in Common Lisp?
Asked Answered
E

6

10

I'm writing a simple web scraper in Common Lisp (SBCL) as a learning exercise, & would like to sort by date. To do this, I'll need to parse dates in the format "MM/DD/YYYY" into universal time.

I could simply tokenise the string & pass the bits into encode-universal-time, but I figure that there must be a built-in function (or popular third-party package) for date parsing. I'd greatly appreciate someone recommending one :-)

Estipulate answered 2/5, 2011 at 7:12 Comment(1)
Amusing tangent: one library I found had a website with the comment "fixme: Does not parse yyyymmddTHHMMSS Z god damn it all. Needs an overhaul" so I figured I'd skip that one ;-)Estipulate
I
8

This answer is very late but the local-time library is featureful and widely used. It is based on the article The long painful history of time.

It supports :

  1. Time and date arithmetic
  2. ISO 8601 timestring formatted output and parsing
  3. Reader macros to embed timestrings directly in code
  4. Timezone handling (will read unix tzfile format)
  5. Conversion between universal and unix time epochs
  6. Julian date calculation
Inspirit answered 2/6, 2015 at 10:17 Comment(4)
Yes, but it does not supports parsing string representation of dates.Portecochere
@Portecochere I have used the package cl-date-time-parser for doing that, it can take a variety of common representations and translate them into a universal-time-in-seconds integer representation.Her
@Portecochere It depends on the string representation. For example, although (local-time:parse-timestring "2009/06/02") is problematic, local-time parsing can work with other string formats -- e.g., (local-time:parse-timestring "2009-06-02").Refugee
"2009/06/02" is not problematic! parse-timestring fails by default. But it accepts the :date-separator argument: (local-time:parse-timestring "2019/11/13" :date-separator #\/) => @2019-11-13T02:00:00.000000+02:00 ! It is not documented, I had to read Slime's suggestions.Immoderation
G
3

See the net-telent-date and simple-date-time libraries for Common Lisp. The former has a parse-time function you can use (see parse-time.lisp). Both are included in the QuickLisp library collection.

Goggin answered 2/5, 2011 at 7:42 Comment(0)
A
3

You could try net-telent-date, which has PARSE-TIME which I think will do what you want.

It's now 2022, and net-telent-date is on github and is also deprecated. Better to find something else.

Arctic answered 2/5, 2011 at 7:51 Comment(2)
The Common Lisp Directory link now goes to a porn site. This should be removed.Penetrating
Updated the links and added a comment.Arctic
I
2

I use local-time and cl-date-time-parser:

edit: and chronicity for parsing natural language dates and times.

(local-time:parse-timestring "2019-11-13T18:09:06.313650+01:00")  ;; OK
(local-time:parse-timestring "2019-11-13") ;;OK

This fails with local-time by default:

(local-time:parse-timestring "2019/11/13")

but it works with Chronicity:

(chronicity:parse "2019/11/13")
@2019-11-13T00:00:00.000000+01:00

and we can set the date separator of local-time to "/":

(local-time:parse-timestring "2019/11/13" :date-separator #\/) ;; OK

There is also the time and datetime separators.

Now a format like ""Wed Nov 13 18:13:15 2019" will fail. We'll use the cl-date-time-parser library:

(cl-date-time-parser:parse-date-time "Wed Nov 13 18:13:15 2019")
;; 3782657595
;; 0

It returns the universal time which, in turn, we can ingest with the local-time library:

(local-time:universal-to-timestamp *)
;; @2019-11-13T19:13:15.000000+01:00
Immoderation answered 13/11, 2019 at 19:12 Comment(0)
P
1

Many implementations have a UNIX interface and, in same cases, this includes the strptime function.

Piecemeal answered 11/11, 2012 at 14:9 Comment(0)
A
1

Antik handles dates and times and includes date/time parsers. The result is a "timepoint" which by default is UTC (CL's "universal-time" is something different, but it can be converted to that).

Appomattox answered 12/11, 2012 at 6:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.