Skip to content

Commit

Permalink
Fixed arrow.get with a timestamp and a timezone string (#746)
Browse files Browse the repository at this point in the history
* Fixed arrow.get with a timestamp and a timezone string and added comments.

* Removed pragma from util.py

* Updated Travis Python versions
  • Loading branch information
jadchaar authored and systemcatch committed Jan 1, 2020
1 parent 6a70b9d commit b1b37ce
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ matrix:
os: windows
language: shell # 'language: python' is an error on Travis CI Windows
before_install:
- choco install python --version 3.7.4
- choco install python --version 3.7.5
- python -m pip install --upgrade pip
env:
- PATH=/c/Python37:/c/Python37/Scripts:$PATH
- TOXENV=py37
- name: "Python 3.8"
python: "3.8-dev"
python: "3.8"
env: TOXENV=py38
- name: "Linting"
python: "3.7"
Expand Down
24 changes: 9 additions & 15 deletions arrow/arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,12 @@ def fromtimestamp(cls, timestamp, tzinfo=None):
:param timestamp: an ``int`` or ``float`` timestamp, or a ``str`` that converts to either.
:param tzinfo: (optional) a ``tzinfo`` object. Defaults to local time.
Timestamps should always be UTC. If you have a non-UTC timestamp::
>>> arrow.Arrow.utcfromtimestamp(1367900664).replace(tzinfo='US/Pacific')
<Arrow [2013-05-07T04:24:24-07:00]>
"""

if tzinfo is None:
tzinfo = dateutil_tz.tzlocal()
elif util.isstr(tzinfo):
tzinfo = parser.TzinfoParser.parse(tzinfo)

if not util.is_timestamp(timestamp):
raise ValueError(
Expand Down Expand Up @@ -1423,19 +1419,17 @@ def _get_tzinfo(tz_expr):

@classmethod
def _get_datetime(cls, expr):

"""Get datetime object for a specified expression."""
if isinstance(expr, Arrow):
return expr.datetime

if isinstance(expr, datetime):
elif isinstance(expr, datetime):
return expr

try:
expr = float(expr)
return cls.utcfromtimestamp(expr).datetime
except Exception:
elif util.is_timestamp(expr):
timestamp = float(expr)
return cls.utcfromtimestamp(timestamp).datetime
else:
raise ValueError(
"'{}' not recognized as a timestamp or datetime".format(expr)
"'{}' not recognized as a datetime or timestamp.".format(expr)
)

@classmethod
Expand Down
3 changes: 2 additions & 1 deletion arrow/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import datetime


def total_seconds(td): # pragma: no cover
def total_seconds(td):
"""Get total seconds for timedelta."""
return td.total_seconds()


Expand Down
6 changes: 6 additions & 0 deletions tests/arrow_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ def test_fromtimestamp(self):
datetime.fromtimestamp(timestamp, tz.gettz("Europe/Paris")),
)

result = arrow.Arrow.fromtimestamp(timestamp, tzinfo="Europe/Paris")
assertDtEqual(
result._datetime,
datetime.fromtimestamp(timestamp, tz.gettz("Europe/Paris")),
)

with self.assertRaises(ValueError):
arrow.Arrow.fromtimestamp("invalid timestamp")

Expand Down
5 changes: 5 additions & 0 deletions tests/util_tests.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# -*- coding: utf-8 -*-
import time
from datetime import datetime

from chai import Chai

from arrow import util


class UtilTests(Chai):
def test_total_seconds(self):
td = datetime(2019, 1, 1) - datetime(2018, 1, 1)
self.assertEqual(util.total_seconds(td), td.total_seconds())

def test_is_timestamp(self):
timestamp_float = time.time()
timestamp_int = int(timestamp_float)
Expand Down

0 comments on commit b1b37ce

Please sign in to comment.