Skip to content

Instantly share code, notes, and snippets.

@FinchPowers
Created October 5, 2018 18:46
Show Gist options
  • Save FinchPowers/11355c842b7f0f1daa0a8cf3f76634de to your computer and use it in GitHub Desktop.
Save FinchPowers/11355c842b7f0f1daa0a8cf3f76634de to your computer and use it in GitHub Desktop.
Neo4j time tests
I started with the following node creaton loop
for i in range(2):
tx.run(f"MERGE (:Node {{date: datetime('2018-06-01T{i:02}:00:00Z')}})")
tx.run(f"MERGE (:Node {{date: datetime('2018-06-01T{i:02}:00:00-01:00')}})")
Here is a TZ reference table
| UTC | -01:00 |
|-----|--------|
| 0 | 23 |
| 1 | 0 |
| 2 | 1 |
| 3 | 2 |
|-----|--------|
Partial strings don't match
n.date = '2018'
[]
n.date > '2018'
[]
n.date < '2018'
[]
Full strings don't match either
n.date < '2019-01-01T00:00:00'
[]
n.date < '2019-01-01T00:00:00Z'
[]
TZ unaware datetimes seem to be casted to UTC
When using a < > operator, it takes both TZ
n.date < datetime('2019-01-01T00:00:00')
[[neotime.DateTime(2018, 6, 1, 0, 0, 0.0, tzinfo=<UTC>)], [neotime.DateTime(2018, 6, 1, 0, 0, 0.0, tzinfo=pytz.FixedOffset(-60))], [neotime.DateTime(2018, 6, 1, 1, 0, 0.0, tzinfo=<UTC>)], [neotime.DateTime(2018, 6, 1, 1, 0, 0.0, tzinfo=pytz.FixedOffset(-60))]]
n.date < datetime('2019-01-01T00:00:00Z')
[[neotime.DateTime(2018, 6, 1, 0, 0, 0.0, tzinfo=<UTC>)], [neotime.DateTime(2018, 6, 1, 0, 0, 0.0, tzinfo=pytz.FixedOffset(-60))], [neotime.DateTime(2018, 6, 1, 1, 0, 0.0, tzinfo=<UTC>)], [neotime.DateTime(2018, 6, 1, 1, 0, 0.0, tzinfo=pytz.FixedOffset(-60))]]
When using an = operator, it matches on datetime and TZ
n.date = datetime('2018-06-01T00:00:00')
[[neotime.DateTime(2018, 6, 1, 0, 0, 0.0, tzinfo=<UTC>)]]
n.date = datetime('2018-06-01T01:00:00')
[[neotime.DateTime(2018, 6, 1, 1, 0, 0.0, tzinfo=<UTC>)]]
n.date = datetime('2018-06-01T02:00:00')
[]
n.date = datetime('2018-06-01T00:00:00Z')
[[neotime.DateTime(2018, 6, 1, 0, 0, 0.0, tzinfo=<UTC>)]]
n.date = datetime('2018-06-01T01:00:00Z')
[[neotime.DateTime(2018, 6, 1, 1, 0, 0.0, tzinfo=<UTC>)]]
n.date = datetime('2018-06-01T02:00:00Z')
[]
So TZ equivalence doesn't work in equality
n.date = datetime('2018-05-31T23:00:00-01:00')
[]
n.date = datetime('2018-06-01T00:00:00Z')
[[neotime.DateTime(2018, 6, 1, 0, 0, 0.0, tzinfo=<UTC>)]]
If we use >= 1 second before, we have both TZ
n.date >= datetime('2018-06-01T00:59:59Z')
[[neotime.DateTime(2018, 6, 1, 0, 0, 0.0, tzinfo=pytz.FixedOffset(-60))], [neotime.DateTime(2018, 6, 1, 1, 0, 0.0, tzinfo=<UTC>)], [neotime.DateTime(2018, 6, 1, 1, 0, 0.0, tzinfo=pytz.FixedOffset(-60))]]
If we use >= right on time, we lose one valid -01:00 entry
n.date >= datetime('2018-06-01T01:00:00Z')
[[neotime.DateTime(2018, 6, 1, 1, 0, 0.0, tzinfo=<UTC>)], [neotime.DateTime(2018, 6, 1, 1, 0, 0.0, tzinfo=pytz.FixedOffset(-60))]]
If we use a combination of >= and <= with the 1 second off trick, we get both
n.date >= datetime('2018-06-01T00:59:59Z') AND n.date <= datetime('2018-06-01T01:00:00Z')
[[neotime.DateTime(2018, 6, 1, 0, 0, 0.0, tzinfo=pytz.FixedOffset(-60))], [neotime.DateTime(2018, 6, 1, 1, 0, 0.0, tzinfo=<UTC>)]]
On the other hand
RETURN datetime('2018-06-01T01:00:00Z').epochMillis = datetime('2018-06-01T00:00:00-01:00').epochMillis
[[True]]
So maybe that when we query and care about a time point/range but don't care about the TZ, we ought to use epochMillis.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment