Skip to content

Commit

Permalink
Use GlideSystem for datetime comparisons (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbw committed Dec 5, 2019
1 parent 6084713 commit acfbc83
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
23 changes: 13 additions & 10 deletions pysnow/query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
)


def datetime_as_utc(date_obj):
if date_obj.tzinfo is not None and date_obj.tzinfo.utcoffset(date_obj) is not None:
dt_str = date_obj.astimezone(pytz.UTC).strftime("%Y-%m-%d %H:%M:%S")
else:
dt_str = date_obj

return 'javascript:gs.dateGenerate("%s")' % dt_str


class QueryBuilder(object):
"""Query builder - for constructing advanced ServiceNow queries"""

Expand Down Expand Up @@ -130,7 +139,7 @@ def greater_than(self, greater_than):
"""

if hasattr(greater_than, "strftime"):
greater_than = datetime_as_utc(greater_than).strftime("%Y-%m-%d %H:%M:%S")
greater_than = datetime_as_utc(greater_than)
elif isinstance(greater_than, six.string_types):
raise QueryTypeError(
"Expected value of type `int` or instance of `datetime`, not %s"
Expand All @@ -148,7 +157,7 @@ def greater_than_or_equal(self, greater_than):
"""

if hasattr(greater_than, "strftime"):
greater_than = datetime_as_utc(greater_than).strftime("%Y-%m-%d %H:%M:%S")
greater_than = datetime_as_utc(greater_than)
elif isinstance(greater_than, six.string_types):
raise QueryTypeError(
"Expected value of type `int` or instance of `datetime`, not %s"
Expand All @@ -166,7 +175,7 @@ def less_than(self, less_than):
"""

if hasattr(less_than, "strftime"):
less_than = datetime_as_utc(less_than).strftime("%Y-%m-%d %H:%M:%S")
less_than = datetime_as_utc(less_than)
elif isinstance(less_than, six.string_types):
raise QueryTypeError(
"Expected value of type `int` or instance of `datetime`, not %s"
Expand All @@ -184,7 +193,7 @@ def less_than_or_equal(self, less_than):
"""

if hasattr(less_than, "strftime"):
less_than = datetime_as_utc(less_than).strftime("%Y-%m-%d %H:%M:%S")
less_than = datetime_as_utc(less_than)
elif isinstance(less_than, six.string_types):
raise QueryTypeError(
"Expected value of type `int` or instance of `datetime`, not %s"
Expand Down Expand Up @@ -310,9 +319,3 @@ def __str__(self):
raise QueryExpressionError("field() expects an expression")

return str().join(self._query)


def datetime_as_utc(date_obj):
if date_obj.tzinfo is not None and date_obj.tzinfo.utcoffset(date_obj) is not None:
return date_obj.astimezone(pytz.UTC)
return date_obj
16 changes: 8 additions & 8 deletions tests/test_query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,15 @@ def test_query_cond_greater_than(self):

# Make sure naive dates are assumed as UTC
q3 = pysnow.QueryBuilder().field("test").greater_than(dt(2016, 2, 1))
self.assertEqual(str(q3), "test>2016-02-01 00:00:00")
self.assertEqual(str(q3), 'test>javascript:gs.dateGenerate("2016-02-01 00:00:00")')

# Make sure tz-aware dates are converted to UTC (UTC+1)
q4 = (
pysnow.QueryBuilder()
.field("test")
.greater_than(dt(2016, 2, 1, 3, tzinfo=pytz.FixedOffset(60)))
)
self.assertEqual(str(q4), "test>2016-02-01 02:00:00")
self.assertEqual(str(q4), 'test>javascript:gs.dateGenerate("2016-02-01 02:00:00")')

def test_query_cond_greater_than_or_equal(self):
# Make sure type checking works
Expand All @@ -228,15 +228,15 @@ def test_query_cond_greater_than_or_equal(self):

# Make sure naive dates are assumed as UTC
q3 = pysnow.QueryBuilder().field("test").greater_than_or_equal(dt(2016, 2, 1))
self.assertEqual(str(q3), "test>=2016-02-01 00:00:00")
self.assertEqual(str(q3), 'test>=javascript:gs.dateGenerate("2016-02-01 00:00:00")')

# Make sure tz-aware dates are converted to UTC (UTC+1)
q4 = (
pysnow.QueryBuilder()
.field("test")
.greater_than_or_equal(dt(2016, 2, 1, 3, tzinfo=pytz.FixedOffset(60)))
)
self.assertEqual(str(q4), "test>=2016-02-01 02:00:00")
self.assertEqual(str(q4), 'test>=javascript:gs.dateGenerate("2016-02-01 02:00:00")')

def test_query_cond_less_than(self):
# Make sure type checking works
Expand All @@ -249,15 +249,15 @@ def test_query_cond_less_than(self):

# Make sure naive dates are assumed as UTC
q3 = pysnow.QueryBuilder().field("test").less_than(dt(2016, 2, 1))
self.assertEqual(str(q3), "test<2016-02-01 00:00:00")
self.assertEqual(str(q3), 'test<javascript:gs.dateGenerate("2016-02-01 00:00:00")')

# Make sure tz-aware dates are converted to UTC (UTC+1)
q3 = (
pysnow.QueryBuilder()
.field("test")
.less_than(dt(2016, 2, 1, 3, tzinfo=pytz.FixedOffset(60)))
)
self.assertEqual(str(q3), "test<2016-02-01 02:00:00")
self.assertEqual(str(q3), 'test<javascript:gs.dateGenerate("2016-02-01 02:00:00")')

def test_query_cond_less_than_or_equal(self):
# Make sure type checking works
Expand All @@ -270,15 +270,15 @@ def test_query_cond_less_than_or_equal(self):

# Make sure naive dates are assumed as UTC
q3 = pysnow.QueryBuilder().field("test").less_than_or_equal(dt(2016, 2, 1))
self.assertEqual(str(q3), "test<=2016-02-01 00:00:00")
self.assertEqual(str(q3), 'test<=javascript:gs.dateGenerate("2016-02-01 00:00:00")')

# Make sure tz-aware dates are converted to UTC (UTC+1)
q3 = (
pysnow.QueryBuilder()
.field("test")
.less_than_or_equal(dt(2016, 2, 1, 3, tzinfo=pytz.FixedOffset(60)))
)
self.assertEqual(str(q3), "test<=2016-02-01 02:00:00")
self.assertEqual(str(q3), 'test<=javascript:gs.dateGenerate("2016-02-01 02:00:00")')

def test_complex_query(self):
start = dt(2016, 2, 1)
Expand Down

0 comments on commit acfbc83

Please sign in to comment.