From 7ba2105a2b861667a732e3fae2ea512528b409fc Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Thu, 26 Jul 2018 13:49:04 -0700 Subject: [PATCH] Fix date and time hy-reprs on Windows --- NEWS.rst | 2 ++ hy/contrib/hy_repr.hy | 10 +++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/NEWS.rst b/NEWS.rst index a636f48..5a226f8 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -17,6 +17,8 @@ Bug Fixes ------------------------------ * Fixed bugs in the handling of unpacking forms in method calls and attribute access. +* Fixed crashes on Windows when calling `hy-repr` on date and time + objects. 0.15.0 ============================== diff --git a/hy/contrib/hy_repr.hy b/hy/contrib/hy_repr.hy index aca8b2a..5d238ba 100644 --- a/hy/contrib/hy_repr.hy +++ b/hy/contrib/hy_repr.hy @@ -121,19 +121,23 @@ (hy-repr-register datetime.datetime (fn [x] (.format "(datetime.datetime {}{})" - (.strftime x "%Y %-m %-d %-H %-M %-S") + (-strftime-0 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)"))) + (-strftime-0 x "(datetime.date %Y %m %d)"))) (hy-repr-register datetime.time (fn [x] (.format "(datetime.time {}{})" - (.strftime x "%-H %-M %-S") + (-strftime-0 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)))]))))) +(defn -strftime-0 [x fmt] + ; Remove leading 0s in `strftime`. This is a substitute for the `-` + ; flag for when Python isn't built with glibc. + (re.sub r"(\A| )0([0-9])" r"\1\2" (.strftime x fmt))) (hy-repr-register collections.Counter (fn [x] (.format "(Counter {})"