Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use GlideSystem for datetime comparison operations #138

Merged
merged 1 commit into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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