Repository: hugovk/pypistats

Issue:
ValueError: month must be in 1..12
```console
$ pypistats python_minor pylast -m 2018-11
| category | percent | downloads |
|----------|--------:|----------:|
|      2.7 |  48.62% |     2,307 |
|      3.6 |  31.63% |     1,501 |
| null     |   7.31% |       347 |
|      3.7 |   6.91% |       328 |
|      3.5 |   4.85% |       230 |
|      3.4 |   0.59% |        28 |
|      3.3 |   0.08% |         4 |
| Total    |         |     4,745 |

$ pypistats python_minor pylast -m 2018-12
Traceback (most recent call last):
  File "/usr/local/bin/pypistats", line 11, in <module>
    load_entry_point('pypistats', 'console_scripts', 'pypistats')()
  File "/Users/hugo/github/pypistats/pypistats/cli.py", line 266, in main
    args.start_date, args.end_date = _month(args.month)
  File "/Users/hugo/github/pypistats/pypistats/cli.py", line 246, in _month
    last = date(year, month + 1, 1) - relativedelta(days=1)
ValueError: month must be in 1..12

$ pypistats python_minor pylast -m 2017-12
Traceback (most recent call last):
  File "/usr/local/bin/pypistats", line 11, in <module>
    load_entry_point('pypistats', 'console_scripts', 'pypistats')()
  File "/Users/hugo/github/pypistats/pypistats/cli.py", line 266, in main
    args.start_date, args.end_date = _month(args.month)
  File "/Users/hugo/github/pypistats/pypistats/cli.py", line 246, in _month
    last = date(year, month + 1, 1) - relativedelta(days=1)
ValueError: month must be in 1..12
```

Discussion:

Patch:
diff --git a/pypistats/cli.py b/pypistats/cli.py
index 092f9b7..8eb62cd 100644
--- a/pypistats/cli.py
+++ b/pypistats/cli.py
@@ -243,7 +243,7 @@ def _month(yyyy_mm):
     """Helper to return start_date and end_date of a month as yyyy-mm-dd"""
     year, month = map(int, yyyy_mm.split("-"))
     first = date(year, month, 1)
-    last = date(year, month + 1, 1) - relativedelta(days=1)
+    last = first + relativedelta(months=1) - relativedelta(days=1)
     return str(first), str(last)
 
 


Test patch:
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 22308f6..f486b7c 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -28,6 +28,17 @@ class TestCli(unittest.TestCase):
         self.assertEqual(first, "2018-07-01")
         self.assertEqual(last, "2018-07-31")
 
+    def test__month_12(self):
+        # Arrange
+        yyyy_mm = "2018-12"
+
+        # Act
+        first, last = cli._month(yyyy_mm)
+
+        # Assert
+        self.assertEqual(first, "2018-12-01")
+        self.assertEqual(last, "2018-12-31")
+
     @freeze_time("2018-09-25")
     def test__last_month(self):
         # Arrange