Mito how to save multiple Date/Time Types

Viewed 107

I have some files that using different date types:

some only have year "2020", others have month "2020-12", and still, some have date:"2020-12-21"

So I want to use interval. Like this:

(define-table test-date ()
  ((date :col-type :interval
         :initarg :date
         :accessor date)))
(create-dao 'test-date
            :date "2020 years")
(date (find-dao 'test-date))=>((:MONTHS 24240) (:DAYS 0) (:SECONDS 0) (:USECONDS 0))

Q1. Is this is how interval supposed to use?

Q2. How to get year?

1 Answers

Your use of interval is smelly. Look, you store a year and you are getting a duration in months. interval is used to store durations, and is particularly used in date arithmetic: date + INTERVAL expr unit. So I don't think you want to use it here to store a date format.

You could:

  • either store a date and transform an incomplete date to a full one: "2020" would become "2020-00-00" if that is acceptable for your logic,
  • either store a string and use a validator function of your own (you could use Mito's inflation/deflation mechanism to retrieve your data in the right data format),
  • either use more date fields (year, month, day…) that are nullable.

Recipes for dates and time manipulation: https://lispcookbook.github.io/cl-cookbook/dates_and_times.html

Related