Repository: destag/at-date

Issue:
ISO 8601 compatibility 
I think that it would be nice to support dates and times in ISO 8601 format. 

Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/destag/at-date/pull/17?src=pr&el=h1) Report
> Merging [#17](https://codecov.io/gh/destag/at-date/pull/17?src=pr&el=desc) into [master](https://codecov.io/gh/destag/at-date/commit/3957d0b750ef1205adb0ba2fc3dccf34c3f0ce62?src=pr&el=desc) will **increase** coverage by `0.03%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/destag/at-date/pull/17/graphs/tree.svg?width=650&token=zT5lrR9jF6&height=150&src=pr)](https://codecov.io/gh/destag/at-date/pull/17?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master      #17      +/-   ##
==========================================
+ Coverage   99.21%   99.25%   +0.03%     
==========================================
  Files           3        3              
  Lines         128      134       +6     
==========================================
+ Hits          127      133       +6     
  Misses          1        1
```


| [Impacted Files](https://codecov.io/gh/destag/at-date/pull/17?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [atdate/atdate\_format.py](https://codecov.io/gh/destag/at-date/pull/17/diff?src=pr&el=tree#diff-YXRkYXRlL2F0ZGF0ZV9mb3JtYXQucHk=) | `100% <ø> (ø)` | :arrow_up: |
| [atdate/api.py](https://codecov.io/gh/destag/at-date/pull/17/diff?src=pr&el=tree#diff-YXRkYXRlL2FwaS5weQ==) | `99.21% <100%> (+0.03%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/destag/at-date/pull/17?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/destag/at-date/pull/17?src=pr&el=footer). Last update [3957d0b...4c68f85](https://codecov.io/gh/destag/at-date/pull/17?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

andrzej3393: I fixed it :slightly_smiling_face: Also, added some docs, but I'm not sure if they're enough.

Patch:
diff --git a/atdate/api.py b/atdate/api.py
index b42b2ac..7f1af8e 100644
--- a/atdate/api.py
+++ b/atdate/api.py
@@ -15,7 +15,9 @@ class AtDateParser:
         tree = self.parser.parse(string_to_parse.lower())
         new_tree = transformer.transform(tree)
 
-        next_time_run = new_tree if isinstance(new_tree, datetime) else new_tree.children[-1]
+        next_time_run = new_tree
+        while not isinstance(next_time_run, datetime):
+            next_time_run = next_time_run.children[-1]
 
         if next_time_run < transformer.now:
             raise ValueError
@@ -65,7 +67,7 @@ class AtDateTransformer(Transformer):
         self.datetime_params['second'] = 0
         return datetime(**self.datetime_params)
 
-    def _hr24clock_hour_minute(self, matches):
+    def _iso_time(self, matches):
         hour = int(matches[0])
         minute = int(matches[1])
         next_day = self._check_if_next_day(hour, minute)
@@ -152,6 +154,13 @@ class AtDateTransformer(Transformer):
         self.datetime_params['year'] = year
         return datetime(**self.datetime_params)
 
+    def _iso_date(self, matches):
+        year, month, day = map(int, matches)
+        self.datetime_params['day'] = day
+        self.datetime_params['month'] = month
+        self.datetime_params['year'] = year
+        return datetime(**self.datetime_params)
+
     def _next(self, matches):
         inc_period = matches[0] if matches[0].endswith('s') else matches[0] + 's'
         dt = datetime(**self.datetime_params)
diff --git a/atdate/atdate_format.py b/atdate/atdate_format.py
index 42e910b..f163137 100644
--- a/atdate/atdate_format.py
+++ b/atdate/atdate_format.py
@@ -3,25 +3,30 @@ format_string = r'''
          | time date
          | time increment
          | time date increment
+         | date time
+         | isodate "t" isotime
          | date
          | date increment
          | nowspec
          | nowspec increment
          | increment
-time: HR24CLOCK_HR_MIN                            -> _hr24clock_hr_min
-    | HR24CLOCK_HOUR ":" MINUTE                   -> _hr24clock_hour_minute
-    | WALLCLOCK_HR_MIN AM_PM                      -> _wallclock_hr_min_am_pm
-    | WALLCLOCK_HOUR ":" MINUTE AM_PM             -> _wallclock_hour_minute_am_pm
-    | "noon"                                      -> _noon
-    | "midnight"                                  -> _midnight
-date: MONTH_NAME DAY_NUMBER                       -> _month_name_day_number
-    | MONTH_NUMBER "/" DAY_NUMBER                 -> _month_number_day_number
-    | MONTH_NUMBER "/" DAY_NUMBER "/" YEAR_NUMBER -> _month_number_day_number_year_number
-    | DAY_NUMBER "." MONTH_NUMBER                 -> _day_number_month_number
-    | DAY_NUMBER "." MONTH_NUMBER "." YEAR_NUMBER -> _day_number_month_number_year_number
-increment: "next" INC_PERIOD                      -> _next
-         | "+" INT INC_PERIOD                     -> _inc_number
-nowspec: "now"                                    -> _now
+time: HR24CLOCK_HR_MIN                               -> _hr24clock_hr_min
+    | WALLCLOCK_HR_MIN AM_PM                         -> _wallclock_hr_min_am_pm
+    | WALLCLOCK_HOUR ":" MINUTE AM_PM                -> _wallclock_hour_minute_am_pm
+    | "noon"                                         -> _noon
+    | "midnight"                                     -> _midnight
+    | isotime
+date: MONTH_NAME DAY_NUMBER                          -> _month_name_day_number
+    | MONTH_NUMBER "/" DAY_NUMBER                    -> _month_number_day_number
+    | MONTH_NUMBER "/" DAY_NUMBER "/" YEAR_NUMBER    -> _month_number_day_number_year_number
+    | DAY_NUMBER "." MONTH_NUMBER                    -> _day_number_month_number
+    | DAY_NUMBER "." MONTH_NUMBER "." YEAR_NUMBER    -> _day_number_month_number_year_number
+    | isodate
+isodate: YEAR_NUMBER "-" MONTH_NUMBER "-" DAY_NUMBER -> _iso_date
+isotime: HR24CLOCK_HOUR ":" MINUTE                   -> _iso_time
+increment: "next" INC_PERIOD                         -> _next
+         | "+" INT INC_PERIOD                        -> _inc_number
+nowspec: "now"                                       -> _now
 INC_PERIOD: "minutes" | "minute"
           | "hours" | "hour"
           | "days" | "day"
diff --git a/docs/README.md b/docs/README.md
index b33fdbd..ab82ea5 100644
--- a/docs/README.md
+++ b/docs/README.md
@@ -98,15 +98,18 @@ tokens|example
 [time] [increment]|17:32 next day
 [time] [date] [increment]|17:32 11/22/2033 next day
 [date]|11/22/2033
+[date] [time]|11/22/2033 17:32
 [date] [increment]|11/22/2033 next month
 [now]|now
 [now] [increment]|now next day
 [increment]|next month
+[isodatetime]|2033-11-22T17:32
 
 [time]: #time
 [date]: #date
 [increment]: #increment
 [now]: #now
+[isodatetime]: #isodatetime
 
 ### At date tokens
 
@@ -135,6 +138,7 @@ format|example
 \[1-12\] / \[1-31\] / \[0-9999\]|10/27/2006
 \[1-12\] . \[1-31\]|10.27
 \[1-12\] . \[1-31\] . \[0-9999\]|10.27.2006
+\[0-9999\] - \[1-12\] - \[1-31\]|2006-10-27
 
 #### increment
 
@@ -145,6 +149,14 @@ format|example
 next \[[period](#period)\]|next month
 \+ \[0-9999\] \[[period](#period)\]|\+ 12 minutes
 
+#### isodatetime
+
+Format for ISO 8601 date time.
+
+format|example
+---|---
+\[0-9999\] - \[1-12\] - \[1-31\] T \[0-23\] : \[0-59\]|2033-11-22T17:32
+
 #### now
 
 Format for this token is literally `now`.


Test patch:
diff --git a/test/test_atdate.py b/test/test_atdate.py
index fdbf0ce..8c23f7c 100644
--- a/test/test_atdate.py
+++ b/test/test_atdate.py
@@ -220,3 +220,31 @@ def test_plus_one_day_without_now():
     test_string = '+1days'
     result = atdate.parse(test_string)
     assert result == datetime(2000, 7, 3, 3, 4, 5, 0)
+
+
+@freeze_time('2000-07-02 03:04:05')
+def test_isodate():
+    test_string = '2011-09-22'
+    result = atdate.parse(test_string)
+    assert result == datetime(2011, 9, 22, 3, 4, 5, 0)
+
+
+@freeze_time('2000-07-02 03:04:05')
+def test_time_date():
+    test_string = "12:24 01.02.2011"
+    result = atdate.parse(test_string)
+    assert result == datetime(2011, 2, 1, 12, 24, 0, 0)
+
+
+@freeze_time('2000-07-02 03:04:05')
+def test_isodatetime():
+    test_string = '2011-09-22T11:44'
+    result = atdate.parse(test_string)
+    assert result == datetime(2011, 9, 22, 11, 44, 0, 0)
+
+
+@freeze_time('2000-07-02 03:04:05')
+def test_isodatetime_without_t():
+    test_string = '2011-09-22 11:44'
+    result = atdate.parse(test_string)
+    assert result == datetime(2011, 9, 22, 11, 44, 0, 0)