diff --git a/pysnow/query_builder.py b/pysnow/query_builder.py index 3efee30..188de60 100644 --- a/pysnow/query_builder.py +++ b/pysnow/query_builder.py @@ -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""" @@ -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" @@ -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" @@ -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" @@ -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" @@ -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 diff --git a/tests/test_query_builder.py b/tests/test_query_builder.py index f22e0cf..1eb46fe 100644 --- a/tests/test_query_builder.py +++ b/tests/test_query_builder.py @@ -207,7 +207,7 @@ 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 = ( @@ -215,7 +215,7 @@ def test_query_cond_greater_than(self): .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 @@ -228,7 +228,7 @@ 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 = ( @@ -236,7 +236,7 @@ def test_query_cond_greater_than_or_equal(self): .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 @@ -249,7 +249,7 @@ 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