|
2 | 2 |
|
3 | 3 | from __future__ import absolute_import, division, print_function, unicode_literals
|
4 | 4 |
|
5 |
| -import datetime |
6 | 5 | import re
|
7 | 6 | import warnings
|
| 7 | +from datetime import datetime, timedelta |
8 | 8 |
|
9 | 9 | from django.conf import settings
|
10 | 10 | from django.core.exceptions import ImproperlyConfigured
|
@@ -586,13 +586,22 @@ def _process_results(self, raw_results, highlight=False,
|
586 | 586 | 'queries': {},
|
587 | 587 | }
|
588 | 588 |
|
| 589 | + # ES can return negative timestamps for pre-1970 data. Handle it. |
| 590 | + def from_timestamp(tm): |
| 591 | + if tm >= 0: |
| 592 | + return datetime.utcfromtimestamp(tm) |
| 593 | + else: |
| 594 | + return datetime(1970, 1, 1) + timedelta(seconds=tm) |
| 595 | + |
589 | 596 | for facet_fieldname, facet_info in raw_results['facets'].items():
|
590 | 597 | if facet_info.get('_type', 'terms') == 'terms':
|
591 | 598 | facets['fields'][facet_fieldname] = [(individual['term'], individual['count']) for individual in facet_info['terms']]
|
592 | 599 | elif facet_info.get('_type', 'terms') == 'date_histogram':
|
593 | 600 | # Elasticsearch provides UTC timestamps with an extra three
|
594 | 601 | # decimals of precision, which datetime barfs on.
|
595 |
| - facets['dates'][facet_fieldname] = [(datetime.datetime.utcfromtimestamp(individual['time'] / 1000), individual['count']) for individual in facet_info['entries']] |
| 602 | + facets['dates'][facet_fieldname] = [(from_timestamp(individual['time'] / 1000), |
| 603 | + individual['count']) |
| 604 | + for individual in facet_info['entries']] |
596 | 605 | elif facet_info.get('_type', 'terms') == 'query':
|
597 | 606 | facets['queries'][facet_fieldname] = facet_info['count']
|
598 | 607 |
|
@@ -706,10 +715,12 @@ def _to_python(self, value):
|
706 | 715 | for dk, dv in date_values.items():
|
707 | 716 | date_values[dk] = int(dv)
|
708 | 717 |
|
709 |
| - return datetime.datetime( |
710 |
| - date_values['year'], date_values['month'], |
711 |
| - date_values['day'], date_values['hour'], |
712 |
| - date_values['minute'], date_values['second']) |
| 718 | + return datetime(date_values['year'], |
| 719 | + date_values['month'], |
| 720 | + date_values['day'], |
| 721 | + date_values['hour'], |
| 722 | + date_values['minute'], |
| 723 | + date_values['second']) |
713 | 724 |
|
714 | 725 | try:
|
715 | 726 | # This is slightly gross but it's hard to tell otherwise what the
|
|
0 commit comments