mirror of
https://github.com/mikf/gallery-dl.git
synced 2024-11-25 04:02:32 +01:00
implement 'text.parse_timestamp()'
This commit is contained in:
parent
f2cf1c1d73
commit
d670de0344
@ -11,6 +11,7 @@
|
||||
import re
|
||||
import html
|
||||
import os.path
|
||||
import datetime
|
||||
import urllib.parse
|
||||
|
||||
|
||||
@ -214,6 +215,14 @@ def parse_query(qs):
|
||||
return result
|
||||
|
||||
|
||||
def parse_timestamp(ts, default=None):
|
||||
"""Create a datetime object from a unix timestamp"""
|
||||
try:
|
||||
return datetime.datetime.fromtimestamp(int(ts))
|
||||
except (TypeError, ValueError, OverflowError):
|
||||
return default
|
||||
|
||||
|
||||
if os.name == "nt":
|
||||
clean_path = clean_path_windows
|
||||
else:
|
||||
|
@ -8,6 +8,7 @@
|
||||
# published by the Free Software Foundation.
|
||||
|
||||
import unittest
|
||||
import datetime
|
||||
|
||||
from gallery_dl import text
|
||||
|
||||
@ -336,6 +337,19 @@ class TestText(unittest.TestCase):
|
||||
for value in INVALID:
|
||||
self.assertEqual(f(value), {})
|
||||
|
||||
def test_parse_timestamp(self, f=text.parse_timestamp):
|
||||
null = datetime.datetime.fromtimestamp(0)
|
||||
value = datetime.datetime.fromtimestamp(1555816235)
|
||||
|
||||
self.assertEqual(f(0) , null)
|
||||
self.assertEqual(f("0") , null)
|
||||
self.assertEqual(f(1555816235) , value)
|
||||
self.assertEqual(f("1555816235"), value)
|
||||
|
||||
for value in INVALID_ALT:
|
||||
self.assertEqual(f(value), None)
|
||||
self.assertEqual(f(value, "foo"), "foo")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
Loading…
Reference in New Issue
Block a user