Skip to content

Commit

Permalink
Merge pull request #5 from kalaspuff/feature/lrucache
Browse files Browse the repository at this point in the history
Performance improvements + fixes an issue where a timestamp could be returned without microseconds
  • Loading branch information
kalaspuff authored Feb 26, 2021
2 parents 9c2a86a + 2372a05 commit c8f234a
Show file tree
Hide file tree
Showing 9 changed files with 1,023 additions and 31 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,17 @@ Some additional examples of timestamps and to what they whould be converted. Thr
```python
import utcnow

# This represents 20 minutes and 50.52 seconds after the 23rd hour of April 12th, 1985 in UTC.
# This represents 20 minutes and 50.52 seconds after the 23rd hour of April 12th, 1985 UTC.
utcnow.get("1985-04-12T23:20:50.52Z") # "1985-04-12T23:20:50.520000Z"

# This represents 39 minutes and 57 seconds after the 16th hour of December 19th, 1996 with an
# offset of -08:00 from UTC (Pacific Standard Time). Note that this is equivalent to
# This represents 39 minutes and 57 seconds after the 16th hour of December 19th, 1996 with
# an offset of -08:00 from UTC (Pacific Standard Time). Note that this is equivalent to
# 1996-12-20T00:39:57Z in UTC.
utcnow.get("1996-12-19T16:39:57-08:00") # "1996-12-20T00:39:57.000000Z"

# This represents the same instant of time as noon, January 1, 1937, Netherlands time. Standard
# time in the Netherlands was exactly 19 minutes and 32.13 seconds ahead of UTC by law from
# 1909-05-01 through 1937-06-30.
# This represents the same instant of time as noon, January 1, 1937, Netherlands time.
# Standard time in the Netherlands was exactly 19 minutes and 32.13 seconds ahead of UTC by
# law from 1909-05-01 through 1937-06-30.
utcnow.get("1937-01-01T12:00:27.87+00:20") # "1937-01-01T11:40:27.870000Z"

# Examples of other formats of accepted inputs:
Expand Down Expand Up @@ -177,7 +177,7 @@ result = utcnow.get("1984-08-01 13:38")
```

```python
# RFC 3339 timestamps as input – dates and datetimes – UTC will be assumed if tz is left out
# RFC 3339 timestamp as input – dates and datetimes – UTC assumed if tz is left out

from utcnow import utcnow
result = utcnow.get("2077-10-27")
Expand All @@ -199,7 +199,7 @@ result = utcnow.get(dt)
```

```python
# It's also possible to transform datetime values with timezone offsets into timestamp strings
# It's also possible to convert datetime values with tz offsets to timestamp strings

import datetime
from utcnow import utcnow
Expand All @@ -214,15 +214,15 @@ result = utcnow.get(dt)
```

```python
# Or vice versa, transforming a timestamp string into a datetime object (with tzinfo set to UTC)
# Vice versa, transforming a timestamp string to a datetime object (with tzinfo set to UTC)

from utcnow import utcnow
result = utcnow.as_datetime("1984-08-01T13:38:00.123450Z")
# datetime.datetime(1984, 8, 1, 13, 38, 0, 123450, tzinfo=datetime.timezone.utc)
```

```python
# Example of using a value from "arrow" – a popular date-time Python lib with a large featureset
# Example using a value from "arrow" – a popular date-time Python lib with large featureset

import arrow
from utcnow import utcnow
Expand All @@ -235,7 +235,7 @@ str(value)
result = utcnow.get(value)
# "2021-04-30T05:58:30.047110Z"

# the same output as via utcnow can be returned in the following ways, including direct via arrow:
# the same output as via utcnow can be returned in following ways, also directly arrow:
# 1. utcnow.get(value)
# 2. value.to("UTC").strftime("%Y-%m-%dT%H:%M:%S.%fZ")
```
Expand All @@ -247,7 +247,7 @@ import utcnow
utcnow.utcnow()
# "2021-02-18T08:24:48.382262Z"

# same thing can be accomplished using datetime and all of these calls returns the same str value:
# Similar can be accomplished with datetime these lines returns the same string value:
# 1. utcnow.utcnow()
# 2. str(utcnow)
# 3. str(utcnow.utcnow)
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest


@pytest.fixture(autouse=True)
def clear_lru_cache() -> None:
from utcnow import _is_numeric, _timestamp_to_datetime, _transform_value

_is_numeric.cache_clear()
_transform_value.cache_clear()
_timestamp_to_datetime.cache_clear()
127 changes: 127 additions & 0 deletions tests/test_dates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import datetime

import pytest


@pytest.mark.parametrize(
"value, expect_error",
[
("1970-01-01", False),
("2020-01-01", False),
("2020-01-29", False),
("2020-01-30", False),
("2020-01-31", False),
("2020-01-32", True),
("2020-01-40", True),
("2020-01-50", True),
("2020-00-00", True),
("2020-01-00", True),
("2020-00-01", True),
("2020-12-01", False),
("2020-12-31", False),
("2020-12-32", True),
("2020-13-01", True),
("2020-02-01", False),
("2020-02-28", False),
("2020-02-29", False),
("2020-02-30", True),
("2020-02-31", True),
("2021-02-28", False),
("2021-02-29", True),
],
)
def test_dates(value: str, expect_error: bool) -> None:
import utcnow

try:
assert isinstance(utcnow.as_string(value), str)
assert isinstance(utcnow.as_datetime(value), datetime.datetime)
if expect_error:
assert False
except Exception:
if not expect_error:
raise
if not expect_error:
# unreachable
assert False

assert True

try:
assert isinstance(utcnow.as_string(f"{value}T00:00:00.000000Z"), str)
assert isinstance(utcnow.as_datetime(f"{value}T00:00:00.000000Z"), datetime.datetime)
if expect_error:
assert False
except Exception:
if not expect_error:
raise
if not expect_error:
# unreachable
assert False

assert True

try:
assert isinstance(utcnow.as_string(value), str)
assert isinstance(utcnow.as_datetime(value), datetime.datetime)

assert isinstance(utcnow.as_string(f"{value}T00:00:00.000000Z"), str)
assert isinstance(utcnow.as_datetime(f"{value}T00:00:00.000000Z"), datetime.datetime)

assert utcnow.as_string(value) == utcnow.as_string(f"{value}T00:00:00.000000Z")

assert isinstance(utcnow.as_string(f"{value} 00:00:00.000000Z"), str)
assert isinstance(utcnow.as_datetime(f"{value} 00:00:00.000000Z"), datetime.datetime)

assert isinstance(utcnow.as_string(f"{value}T00:00:00.000000"), str)
assert isinstance(utcnow.as_datetime(f"{value}T00:00:00.000000"), datetime.datetime)

assert isinstance(utcnow.as_string(f"{value} 00:00:00.000000"), str)
assert isinstance(utcnow.as_datetime(f"{value} 00:00:00.000000"), datetime.datetime)

assert isinstance(utcnow.as_string(f"{value}T00:00:00.000000+00:00"), str)
assert isinstance(utcnow.as_datetime(f"{value}T00:00:00.000000+00:00"), datetime.datetime)

assert isinstance(utcnow.as_string(f"{value} 00:00:00.000000+00:00"), str)
assert isinstance(utcnow.as_datetime(f"{value} 00:00:00.000000+00:00"), datetime.datetime)

assert isinstance(utcnow.as_string(f"{value}T00:00:00.000000-00:00"), str)
assert isinstance(utcnow.as_datetime(f"{value}T00:00:00.000000-00:00"), datetime.datetime)

assert isinstance(utcnow.as_string(f"{value}T00:00:00.000000 UTC"), str)
assert isinstance(utcnow.as_datetime(f"{value}T00:00:00.000000 UTC"), datetime.datetime)

assert isinstance(utcnow.as_string(f"{value}T00:00:00Z"), str)
assert isinstance(utcnow.as_datetime(f"{value}T00:00:00Z"), datetime.datetime)

assert isinstance(utcnow.as_string(f"{value} 00:00:00Z"), str)
assert isinstance(utcnow.as_datetime(f"{value} 00:00:00Z"), datetime.datetime)

assert isinstance(utcnow.as_string(f"{value}T00:00:00"), str)
assert isinstance(utcnow.as_datetime(f"{value}T00:00:00"), datetime.datetime)

assert isinstance(utcnow.as_string(f"{value} 00:00:00"), str)
assert isinstance(utcnow.as_datetime(f"{value} 00:00:00"), datetime.datetime)

assert isinstance(utcnow.as_string(f"{value}T00:00:00+00:00"), str)
assert isinstance(utcnow.as_datetime(f"{value}T00:00:00+00:00"), datetime.datetime)

assert isinstance(utcnow.as_string(f"{value} 00:00:00+00:00"), str)
assert isinstance(utcnow.as_datetime(f"{value} 00:00:00+00:00"), datetime.datetime)

assert isinstance(utcnow.as_string(f"{value}T00:00:00-00:00"), str)
assert isinstance(utcnow.as_datetime(f"{value}T00:00:00-00:00"), datetime.datetime)

assert isinstance(utcnow.as_string(f"{value}T00:00:00 UTC"), str)
assert isinstance(utcnow.as_datetime(f"{value}T00:00:00 UTC"), datetime.datetime)

if expect_error:
assert False
except Exception:
if not expect_error:
raise
if not expect_error:
# unreachable
assert False

assert True
Loading

0 comments on commit c8f234a

Please sign in to comment.