Make hy-repr support DateTime objects

This commit is contained in:
Kodi Arfer 2017-11-03 10:51:41 -07:00
parent 3dbe05302e
commit 38f461890d
2 changed files with 50 additions and 6 deletions

View File

@ -5,7 +5,8 @@
(import (import
[math [isnan]] [math [isnan]]
re re
[hy._compat [PY3 str-type bytes-type long-type]] datetime
[hy._compat [PY3 PY36 str-type bytes-type long-type]]
[hy.models [HyObject HyExpression HySymbol HyKeyword HyInteger HyFloat HyComplex HyList HyDict HySet HyString HyBytes]]) [hy.models [HyObject HyExpression HySymbol HyKeyword HyInteger HyFloat HyComplex HyList HyDict HySet HyString HyBytes]])
(try (try
@ -102,14 +103,30 @@
(hy-repr-register fraction (fn [x] (hy-repr-register fraction (fn [x]
(.format "{}/{}" (hy-repr x.numerator) (hy-repr x.denominator)))) (.format "{}/{}" (hy-repr x.numerator) (hy-repr x.denominator))))
(setv matchobject-type (type (re.match "" ""))) (setv -matchobject-type (type (re.match "" "")))
(hy-repr-register matchobject-type (fn [x] (hy-repr-register -matchobject-type (fn [x]
(.format "<{}.{} object; :span {} :match {}>" (.format "<{}.{} object; :span {} :match {}>"
matchobject-type.__module__ -matchobject-type.__module__
matchobject-type.__name__ -matchobject-type.__name__
(hy-repr (.span x)) (hy-repr (.span x))
(hy-repr (.group x 0))))) (hy-repr (.group x 0)))))
(hy-repr-register datetime.datetime (fn [x]
(.format "(datetime.datetime {}{})"
(.strftime x "%Y %-m %-d %-H %-M %-S")
(-repr-time-innards x))))
(hy-repr-register datetime.date (fn [x]
(.strftime x "(datetime.date %Y %-m %-d)")))
(hy-repr-register datetime.time (fn [x]
(.format "(datetime.time {}{})"
(.strftime x "%-H %-M %-S")
(-repr-time-innards x))))
(defn -repr-time-innards [x]
(.rstrip (+ " " (.join " " (filter identity [
(if x.microsecond (str-type x.microsecond))
(if (not (none? x.tzinfo)) (+ ":tzinfo " (hy-repr x.tzinfo)))
(if (and PY36 (!= x.fold 0)) (+ ":fold " (hy-repr x.fold)))])))))
(for [[types fmt] (partition [ (for [[types fmt] (partition [
list "[...]" list "[...]"
[set HySet] "#{...}" [set HySet] "#{...}"

View File

@ -3,7 +3,7 @@
;; license. See the LICENSE. ;; license. See the LICENSE.
(import (import
[hy._compat [PY3]] [hy._compat [PY3 PY36]]
[math [isnan]] [math [isnan]]
[hy.contrib.hy-repr [hy-repr hy-repr-register]]) [hy.contrib.hy-repr [hy-repr hy-repr-register]])
@ -92,6 +92,33 @@
(assert (= (hy-repr (.values {1 2})) "(dict-values [2])")) (assert (= (hy-repr (.values {1 2})) "(dict-values [2])"))
(assert (= (hy-repr (.items {1 2})) "(dict-items [(, 1 2)])")))) (assert (= (hy-repr (.items {1 2})) "(dict-items [(, 1 2)])"))))
(defn test-datetime []
(import [datetime :as D])
(assert (= (hy-repr (D.datetime 2009 1 15 15 27 5 0))
"(datetime.datetime 2009 1 15 15 27 5)"))
(assert (= (hy-repr (D.datetime 2009 1 15 15 27 5 123))
"(datetime.datetime 2009 1 15 15 27 5 123)"))
(when PY3
(assert (= (hy-repr (D.datetime 2009 1 15 15 27 5 123 :tzinfo D.timezone.utc))
"(datetime.datetime 2009 1 15 15 27 5 123 :tzinfo datetime.timezone.utc)")))
(when PY36
(assert (= (hy-repr (D.datetime 2009 1 15 15 27 5 :fold 1))
"(datetime.datetime 2009 1 15 15 27 5 :fold 1)"))
(assert (= (hy-repr (D.datetime 2009 1 15 15 27 5 :fold 1 :tzinfo D.timezone.utc))
"(datetime.datetime 2009 1 15 15 27 5 :tzinfo datetime.timezone.utc :fold 1)")))
(assert (= (hy-repr (D.date 2015 11 3))
"(datetime.date 2015 11 3)"))
(assert (= (hy-repr (D.time 1 2 3))
"(datetime.time 1 2 3)"))
(assert (= (hy-repr (D.time 1 2 3 4567))
"(datetime.time 1 2 3 4567)"))
(when PY36
(assert (= (hy-repr (D.time 1 2 3 4567 :fold 1 :tzinfo D.timezone.utc))
"(datetime.time 1 2 3 4567 :tzinfo datetime.timezone.utc :fold 1)"))))
(defn test-hy-model-constructors [] (defn test-hy-model-constructors []
(import hy) (import hy)
(assert (= (hy-repr (hy.HyInteger 7)) "'7")) (assert (= (hy-repr (hy.HyInteger 7)) "'7"))