Repository: dlr-eoc/ukis-pysat

Issue:
`get_ts_from_sentinel_filename()` does not return a defined date format
**Describe the bug**
`get_ts_from_sentinel_filename()` does not return a defined date format, but just a substring of the filename. It would be more useful to return something which is readable for common parsers. I would even expect this to happen and therefore label this as _bug_.

**Expected behavior**
Return a timestamp in ISO 8601 `YYYY-MM-DD`, e.g. with [`isoformat()`](https://docs.python.org/3/library/datetime.html#datetime.date.isoformat)


Discussion:
codecov-commenter: # [Codecov](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/43?src=pr&el=h1) Report
> Merging [#43](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/43?src=pr&el=desc) into [master](https://codecov.io/gh/dlr-eoc/ukis-pysat/commit/9da685ab9d4644ddb0b7a7fc0c864fd96f642d98&el=desc) will **increase** coverage by `0.01%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/43/graphs/tree.svg?width=650&height=150&src=pr&token=6LYPGRGMXV)](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/43?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master      #43      +/-   ##
==========================================
+ Coverage   93.46%   93.48%   +0.01%     
==========================================
  Files           8        8              
  Lines         673      675       +2     
==========================================
+ Hits          629      631       +2     
  Misses         44       44              
```

| Flag | Coverage Δ | |
|---|---|---|
| #unittests | `93.48% <100.00%> (+0.01%)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/43?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [tests/test\_file.py](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/43/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9maWxlLnB5) | `97.91% <100.00%> (+0.04%)` | :arrow_up: |
| [ukis\_pysat/file.py](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/43/diff?src=pr&el=tree#diff-dWtpc19weXNhdC9maWxlLnB5) | `94.33% <100.00%> (+0.05%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/43?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/dlr-eoc/ukis-pysat/pull/43?src=pr&el=footer). Last update [9da685a...0daa85c](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/43?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 0fb8f90..5dc2efe 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -13,10 +13,14 @@ Changed
 ~~~~~~~
 something was changed
 
-[master]  (2020-XX-XX)
+[master]  (2020-06-16)
 ----------------------
 
-[master]  (2020-05-06)
+Fixed
+*****
+- ``file.get_ts_from_sentinel_filename()``: Return datetime.datetime objects instead of timestamp strings #42
+
+[0.4.0]  (2020-06-05)
 ----------------------
 
 Added
diff --git a/ukis_pysat/file.py b/ukis_pysat/file.py
index 1c40e3e..046c57e 100644
--- a/ukis_pysat/file.py
+++ b/ukis_pysat/file.py
@@ -7,6 +7,7 @@ import shutil
 import tempfile
 import xml.etree.ElementTree as ET
 import zipfile
+from datetime import datetime, timezone
 
 
 def env_get(key):
@@ -110,35 +111,35 @@ def get_polarization_from_s1_filename(filename, dual=False):
 
 def get_ts_from_sentinel_filename(filename, start_date=True):
     """Get timestamp from the filename of a Sentinel scene, according to naming conventions.
-    Currently works for S1, S2 (generated after the 6th of December, 2016) & S3.
+    Currently works for S1, S2 & S3.
 
     :param  filename: top-level SENTINEL product folder or file name
     :param start_date: boolean (default: True), False is Stop Date, optional
-    :return: str
+    :return: datetime.datetime object with timezone information
 
     >>> get_ts_from_sentinel_filename("S1M_BB_TTTR_LFPP_20200113T074619_YYYYMMDDTHHMMSS_OOOOOO_DDDDDD_CCCC.SAFE.zip")
-    '20200113T074619'
+    datetime.datetime(2020, 1, 13, 7, 46, 19, tzinfo=datetime.timezone.utc)
     >>> get_ts_from_sentinel_filename("S1M_BB_TTTR_LFPP_YYYYMMDDTHHMMSS_20200113T002219_OOOOOO_DDDDDD_CCCC.SAFE.zip", False)
-    '20200113T002219'
-    >>> get_ts_from_sentinel_filename("S3M_OL_L_TTT____yyyymmddThhmmss_YYYYMMDDTHHMMSS_YYYYMMDDTHHMMSS_i_GGG_c.SEN3")
-    'yyyymmddThhmmss'
-    >>> get_ts_from_sentinel_filename("S3M_OL_L_TTTTTT_yyyymmddThhmmss_YYYYMMDDTHHMMSS_YYYYMMDDTHHMMSS_i_GGG_c.SEN3", False)
-    'YYYYMMDDTHHMMSS'
-    >>> get_ts_from_sentinel_filename("S2AM_MSIXXX_YYYYMMDDHHMMSS_Nxxyy_ROOO_Txxxxx_<Product Discriminator>.SAFE")
-    'YYYYMMDDHHMMSS'
+    datetime.datetime(2020, 1, 13, 0, 22, 19, tzinfo=datetime.timezone.utc)
+    >>> get_ts_from_sentinel_filename("S3M_OL_L_TTT____20200113T074619_YYYYMMDDTHHMMSS_YYYYMMDDTHHMMSS_i_GGG_c.SEN3")
+    datetime.datetime(2020, 1, 13, 7, 46, 19, tzinfo=datetime.timezone.utc)
+    >>> get_ts_from_sentinel_filename("S3M_OL_L_TTTTTT_yyyymmddThhmmss_20200113T074619_YYYYMMDDTHHMMSS_i_GGG_c.SEN3", False)
+    datetime.datetime(2020, 1, 13, 7, 46, 19, tzinfo=datetime.timezone.utc)
+    >>> get_ts_from_sentinel_filename("S2AM_MSIXXX_20200113T074619_Nxxyy_ROOO_Txxxxx_<Product Discriminator>.SAFE")
+    datetime.datetime(2020, 1, 13, 7, 46, 19, tzinfo=datetime.timezone.utc)
     """
     if filename.startswith("S2"):
-        return filename.split("_")[2]
+        return datetime.strptime(filename.split("_")[2], "%Y%m%dT%H%M%S").replace(tzinfo=timezone.utc)
     elif filename.startswith("S1"):
         if start_date:
-            return filename.split("_")[4]
+            return datetime.strptime(filename.split("_")[4], "%Y%m%dT%H%M%S").replace(tzinfo=timezone.utc)
         else:
-            return filename.split("_")[5]
+            return datetime.strptime(filename.split("_")[5], "%Y%m%dT%H%M%S").replace(tzinfo=timezone.utc)
     else:
         if start_date:
-            return filename[16:31]
+            return datetime.strptime(filename[16:31], "%Y%m%dT%H%M%S").replace(tzinfo=timezone.utc)
         else:
-            return filename[32:47]
+            return datetime.strptime(filename[32:47], "%Y%m%dT%H%M%S").replace(tzinfo=timezone.utc)
 
 
 def get_footprint_from_manifest(xml_path):


Test patch:
diff --git a/tests/test_file.py b/tests/test_file.py
index 9060aae..0aa2520 100644
--- a/tests/test_file.py
+++ b/tests/test_file.py
@@ -1,6 +1,8 @@
 import os
 import shutil
 import unittest
+from datetime import datetime, timezone
+
 import ukis_pysat.file as psf
 
 path_testfiles = os.path.join(os.path.dirname(__file__), "testfiles")
@@ -60,33 +62,33 @@ class FileTest(unittest.TestCase):
     def test_get_ts_from_sentinel_filename(self):
         self.assertEqual(
             psf.get_ts_from_sentinel_filename(
-                "S3M_OL_L_TTT____yyyymmddThhmmss_YYYYMMDDTHHMMSS_YYYYMMDDTHHMMSS_i_GGG_c.SEN3"
+                "S3M_OL_L_TTT____20200113T002219_YYYYMMDDTHHMMSS_YYYYMMDDTHHMMSS_i_GGG_c.SEN3"
             ),
-            "yyyymmddThhmmss",
+            datetime(2020, 1, 13, 0, 22, 19, tzinfo=timezone.utc),
         )
         self.assertEqual(
             psf.get_ts_from_sentinel_filename(
                 "S1M_BB_TTTR_LFPP_YYYYMMDDTHHMMSS_20200113T002219_OOOOOO_DDDDDD_CCCC.SAFE.zip", False,
             ),
-            "20200113T002219",
+            datetime(2020, 1, 13, 0, 22, 19, tzinfo=timezone.utc),
         )
         self.assertEqual(
             psf.get_ts_from_sentinel_filename(
                 "S1M_BB_TTTR_LFPP_20200113T074619_YYYYMMDDTHHMMSS_OOOOOO_DDDDDD_CCCC.SAFE.zip"
             ),
-            "20200113T074619",
+            datetime(2020, 1, 13, 7, 46, 19, tzinfo=timezone.utc),
         )
         self.assertEqual(
             psf.get_ts_from_sentinel_filename(
-                "S2AM_MSIXXX_YYYYMMDDHHMMSS_Nxxyy_ROOO_Txxxxx_<Product Discriminator>.SAFE"
+                "S2AM_MSIXXX_20200113T002219_Nxxyy_ROOO_Txxxxx_<Product Discriminator>.SAFE"
             ),
-            "YYYYMMDDHHMMSS",
+            datetime(2020, 1, 13, 0, 22, 19, tzinfo=timezone.utc),
         )
         self.assertEqual(
             psf.get_ts_from_sentinel_filename(
-                "S3M_OL_L_TTTTTT_yyyymmddThhmmss_YYYYMMDDTHHMMSS_YYYYMMDDTHHMMSS_i_GGG_c.SEN3", False
+                "S3M_OL_L_TTTTTT_yyyymmddThhmmss_20200113T002219_YYYYMMDDTHHMMSS_i_GGG_c.SEN3", False
             ),
-            "YYYYMMDDTHHMMSS",
+            datetime(2020, 1, 13, 0, 22, 19, tzinfo=timezone.utc),
         )
 
     def test_get_footprint_from_manifest(self):