Python ciso8601.parse_datetime() Examples

The following are 30 code examples for showing how to use ciso8601.parse_datetime(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module ciso8601 , or try the search function .

Example 1
Project: ciso8601   Author: closeio   File: tests.py    License: MIT License 6 votes vote down vote up
def test_invalid_tz_offsets_too_large(self):
        # The Python interpreter crashes if you give the datetime constructor a TZ offset with an absolute value >= 1440
        # TODO: Determine whether these are valid ISO 8601 values and therefore whether ciso8601 should support them.
        self.assertRaisesRegex(
            ValueError,
            # Error message differs whether or not we are using pytz or datetime.timezone
            r"^offset must be a timedelta strictly between" if sys.version_info.major >= 3 else r"\('absolute offset is too large', -5940\)",
            ciso8601.parse_datetime,
            '2018-01-01T00:00:00.00-99',
        )

        self.assertRaisesRegex(
            ValueError,
            r"tzminute must be in 0..59",
            ciso8601.parse_datetime,
            '2018-01-01T00:00:00.00-23:60',
        ) 
Example 2
Project: ciso8601   Author: closeio   File: tests.py    License: MIT License 6 votes vote down vote up
def test_mixed_basic_and_extended_formats(self):
        """
        Both dates and times have "basic" and "extended" formats.
        But when you combine them into a datetime, the date and time components
        must have the same format.
        """
        self.assertRaisesRegex(
            ValueError,
            r"Cannot combine \"extended\" date format with \"basic\" time format",
            ciso8601.parse_datetime,
            '2014-01-02T010203',
        ),

        self.assertRaisesRegex(
            ValueError,
            r"Cannot combine \"basic\" date format with \"extended\" time format",
            ciso8601.parse_datetime,
            '20140102T01:02:03',
        ) 
Example 3
Project: ciso8601   Author: closeio   File: tests.py    License: MIT License 6 votes vote down vote up
def test_valid_rfc3339_timestamps(self):
        """
        Validate that valid RFC 3339 datetimes are parseable by parse_rfc3339
        and produce the same result as parse_datetime.
        """
        for string in [
                '2018-01-02T03:04:05Z',
                '2018-01-02t03:04:05z',
                '2018-01-02 03:04:05z',
                '2018-01-02T03:04:05+00:00',
                '2018-01-02T03:04:05-00:00',
                '2018-01-02T03:04:05.12345Z',
                '2018-01-02T03:04:05+01:23',
                '2018-01-02T03:04:05-12:34',
                '2018-01-02T03:04:05-12:34',
        ]:
            self.assertEqual(ciso8601.parse_datetime(string),
                             ciso8601.parse_rfc3339(string)) 
Example 4
Project: koku   Author: project-koku   File: report_processor_base.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def _should_process_row(self, row, date_column, is_full_month, is_finalized=None):
        """Determine if we want to process this row.

        Args:
            row (dict): The line item entry from the AWS report file
            date_column (str): The name of date column to check
            is_full_month (boolean): If this is the first time we've processed this bill

        Kwargs:
            is_finalized (boolean): If this is a finalized bill

        Returns:
            (bool): Whether this row should be processed

        """
        if is_finalized or is_full_month:
            return True
        row_date = ciso8601.parse_datetime(row[date_column]).date()
        if row_date < self.data_cutoff_date:
            return False
        return True 
Example 5
def convert_date(self, date_string):
        """Converts date from string format to date object, for use by DateField."""
        if date_string:
            try:
                # TODO: for now, return as a string.
                # When actually supporting DateField, then switch back to date.
                # ciso8601.parse_datetime(ts).astimezone(pytz.utc).date().isoformat()
                return self.date_field_for_converting.deserialize_from_string(date_string).isoformat()
            except ValueError:
                self.incr_counter(self.counter_category_name, 'Cannot convert to date', 1)
                # Don't bother to make sure we return a good value
                # within the interval, so we can find the output for
                # debugging.  Should not be necessary, as this is only
                # used for the column value, not the partitioning.
                return u"BAD: {}".format(date_string)
                # return self.lower_bound_date_string
        else:
            self.incr_counter(self.counter_category_name, 'Missing date', 1)
            return date_string 
Example 6
def test_problem_check(self):
        template = self.event_templates['problem_check']
        event = self.create_event_log_line(template=template)
        expected_key = (self.DEFAULT_DATE, self.task.PROJECT_NAME)
        expected_dict = {
            'input_file': '',
            'source': self.task.PROJECT_NAME,
            'event_type': 'problem_check',
            'emitter_type': 'server',
            'timestamp': ciso8601.parse_datetime('2013-12-17T15:38:32.805444+00:00'),
            'received_at': ciso8601.parse_datetime('2013-12-17T15:38:32.805444+00:00'),
            'date': datetime.date(*[int(x) for x in self.DEFAULT_DATE.split('-')]),
            'username': 'test_user',
            'course_id': self.encoded_course_id,
            'org_id': self.encoded_org_id,
            'user_id': '10',
            'raw_event': self.get_raw_event(event),
        }
        expected_value = JsonEventRecord(**expected_dict).to_separated_values()
        self.assert_single_map_output(event, expected_key, expected_value) 
Example 7
def test_android_screen(self):
        template = self.event_templates['android_screen']
        event = self.create_event_log_line(template=template)
        expected_key = (self.DEFAULT_DATE, self.DEFAULT_PROJECT)
        expected_dict = {
            'input_file': '',
            'source': self.DEFAULT_PROJECT,
            'event_type': 'screen',
            'emitter_type': 'server',
            'timestamp': ciso8601.parse_datetime('2013-12-17T15:38:32.700000+00:00'),
            'received_at': ciso8601.parse_datetime('2013-12-17T15:38:32.796000+00:00'),
            'date': datetime.date(*[int(x) for x in self.DEFAULT_DATE.split('-')]),
            'agent_type': 'tablet',
            'agent_device_name': 'Samsung SM-N920A',
            'agent_os': 'Android',
            'agent_browser': 'Android',
            'agent_touch_capable': True,
            'anonymous_id': self.DEFAULT_ANONYMOUS_ID,
            'category': 'screen',
            'label': 'Launch\\0',
            'raw_event': self.get_raw_event(event),
        }
        expected_value = JsonEventRecord(**expected_dict).to_separated_values()
        self.assert_single_map_output(event, expected_key, expected_value) 
Example 8
Project: ciso8601   Author: closeio   File: tests.py    License: MIT License 5 votes vote down vote up
def test_auto_generated_valid_formats(self):
        for (timestamp, expected_datetime) in generate_valid_timestamp_and_datetime():
            try:
                self.assertEqual(ciso8601.parse_datetime(timestamp), expected_datetime)
            except Exception:
                print("Had problems parsing: {timestamp}".format(timestamp=timestamp))
                raise 
Example 9
Project: ciso8601   Author: closeio   File: tests.py    License: MIT License 5 votes vote down vote up
def test_excessive_subsecond_precision(self):
        self.assertEqual(
            ciso8601.parse_datetime('20140203T103527.234567891234'),
            datetime.datetime(2014, 2, 3, 10, 35, 27, 234567)
        ) 
Example 10
Project: ciso8601   Author: closeio   File: tests.py    License: MIT License 5 votes vote down vote up
def test_leap_year(self):
        # There is nothing unusual about leap years in ISO 8601.
        # We just want to make sure that they work in general.
        for leap_year in (1600, 2000, 2016):
            self.assertEqual(
                ciso8601.parse_datetime('{}-02-29'.format(leap_year)),
                datetime.datetime(leap_year, 2, 29, 0, 0, 0, 0)
            ) 
Example 11
Project: ciso8601   Author: closeio   File: tests.py    License: MIT License 5 votes vote down vote up
def test_special_midnight(self):
        self.assertEqual(
            ciso8601.parse_datetime('2014-02-03T24:00:00'),
            datetime.datetime(2014, 2, 4, 0, 0, 0)
        ) 
Example 12
Project: ciso8601   Author: closeio   File: tests.py    License: MIT License 5 votes vote down vote up
def test_parse_auto_generated_invalid_formats(self):
        for timestamp in generate_invalid_timestamp():
            try:
                with self.assertRaises(ValueError, msg="Timestamp '{0}' was supposed to be invalid, but parsing it didn't raise ValueError.".format(timestamp)):
                    ciso8601.parse_datetime(timestamp)
            except Exception as exc:
                print("Timestamp '{0}' was supposed to raise ValueError, but raised {1} instead".format(timestamp, type(exc).__name__))
                raise 
Example 13
Project: ciso8601   Author: closeio   File: tests.py    License: MIT License 5 votes vote down vote up
def test_invalid_calendar_separator(self):
        self.assertRaisesRegex(
            ValueError,
            r"Invalid character while parsing month",
            ciso8601.parse_datetime,
            '2018=01=01',
        )

        self.assertRaisesRegex(
            ValueError,
            r"Invalid character while parsing date separator \('-'\) \('=', Index: 7\)",
            ciso8601.parse_datetime,
            '2018-01=01',
        )

        self.assertRaisesRegex(
            ValueError,
            r"Invalid character while parsing date separator \('-'\) \('0', Index: 7\)",
            ciso8601.parse_datetime,
            '2018-0101',
        )

        self.assertRaisesRegex(
            ValueError,
            r"Invalid character while parsing day \('-', Index: 6\)",
            ciso8601.parse_datetime,
            '201801-01',
        ) 
Example 14
Project: ciso8601   Author: closeio   File: tests.py    License: MIT License 5 votes vote down vote up
def test_invalid_day_for_month(self):
        for non_leap_year in (1700, 1800, 1900, 2014):
            self.assertRaisesRegex(
                ValueError,
                r"day is out of range for month",
                ciso8601.parse_datetime,
                '{}-02-29'.format(non_leap_year)
            )

        self.assertRaisesRegex(
            ValueError,
            r"day is out of range for month",
            ciso8601.parse_datetime,
            '2014-01-32',
        )

        self.assertRaisesRegex(
            ValueError,
            r"day is out of range for month",
            ciso8601.parse_datetime,
            '2014-06-31',
        )

        self.assertRaisesRegex(
            ValueError,
            r"day is out of range for month",
            ciso8601.parse_datetime,
            '2014-06-00',
        ) 
Example 15
Project: ciso8601   Author: closeio   File: tests.py    License: MIT License 5 votes vote down vote up
def test_invalid_yyyymm_format(self):
        self.assertRaisesRegex(
            ValueError,
            r"Unexpected end of string while parsing day. Expected 2 more characters",
            ciso8601.parse_datetime,
            '201406',
        ) 
Example 16
Project: ciso8601   Author: closeio   File: tests.py    License: MIT License 5 votes vote down vote up
def test_invalid_date_and_time_separator(self):
        self.assertRaisesRegex(
            ValueError,
            r"Invalid character while parsing date and time separator \(ie. 'T' or ' '\) \('_', Index: 10\)",
            ciso8601.parse_datetime,
            '2018-01-01_00:00:00',
        ) 
Example 17
Project: ciso8601   Author: closeio   File: tests.py    License: MIT License 5 votes vote down vote up
def test_invalid_hour_24(self):
        # A value of hour = 24 is only valid in the special case of 24:00:00
        self.assertRaisesRegex(
            ValueError,
            r"hour must be in 0..23",
            ciso8601.parse_datetime,
            '2014-02-03T24:35:27',
        ) 
Example 18
Project: ciso8601   Author: closeio   File: tests.py    License: MIT License 5 votes vote down vote up
def test_invalid_tz_minute(self):
        self.assertRaisesRegex(
            ValueError,
            r"tzminute must be in 0..59",
            ciso8601.parse_datetime,
            '2018-01-01T00:00:00.00-00:99',
        ) 
Example 19
Project: ciso8601   Author: closeio   File: tests.py    License: MIT License 5 votes vote down vote up
def test_issue_5(self):
        self.assertRaisesRegex(
            ValueError,
            r"Invalid character while parsing minute \(':', Index: 14\)",
            ciso8601.parse_datetime,
            '2014-02-03T10::27',
        ) 
Example 20
Project: ciso8601   Author: closeio   File: tests.py    License: MIT License 5 votes vote down vote up
def test_issue_8(self):
        self.assertRaisesRegex(
            ValueError,
            r"hour must be in 0..23",
            ciso8601.parse_datetime,
            '2001-01-01T24:01:01',
        )

        self.assertRaisesRegex(
            ValueError,
            r"month must be in 1..12",
            ciso8601.parse_datetime,
            '07722968',
        ) 
Example 21
Project: ciso8601   Author: closeio   File: tests.py    License: MIT License 5 votes vote down vote up
def test_issue_13(self):
        self.assertRaisesRegex(
            ValueError,
            r"month must be in 1..12",
            ciso8601.parse_datetime,
            '2014-13-01',
        ) 
Example 22
Project: ciso8601   Author: closeio   File: tests.py    License: MIT License 5 votes vote down vote up
def test_issue_22(self):
        self.assertRaisesRegex(
            ValueError,
            r"day is out of range for month",
            ciso8601.parse_datetime,
            '2016-11-31T12:34:34.521059',
        ) 
Example 23
Project: ciso8601   Author: closeio   File: tests.py    License: MIT License 5 votes vote down vote up
def test_issue_35(self):
        self.assertRaisesRegex(
            ValueError,
            r"Invalid character while parsing date separator \('-'\) \('1', Index: 7\)",
            ciso8601.parse_datetime,
            '2017-0012-27T13:35:19+0200',
        ) 
Example 24
Project: ciso8601   Author: closeio   File: tests.py    License: MIT License 5 votes vote down vote up
def test_issue_42(self):
        self.assertRaisesRegex(
            ValueError,
            r"day is out of range for month",
            ciso8601.parse_datetime,
            '20140200',
        ) 
Example 25
Project: koku   Author: project-koku   File: ocp_report_processor.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def _create_report(self, row, report_period_id, report_db_accessor):
        """Create a report object.

        Args:
            row (dict): A dictionary representation of a CSV file row
            report_period_id (str): report period object id

        Returns:
            (str): The DB id of the report object

        """
        table_name = OCPUsageReport
        start = ciso8601.parse_datetime(row.get("interval_start").replace(" +0000 UTC", "+0000"))
        end = ciso8601.parse_datetime(row.get("interval_end").replace(" +0000 UTC", "+0000"))

        key = (report_period_id, start)
        if key in self.processed_report.reports:
            return self.processed_report.reports[key]

        if key in self.existing_report_map:
            return self.existing_report_map[key]

        data = {"report_period_id": report_period_id, "interval_start": start, "interval_end": end}
        report_id = report_db_accessor.insert_on_conflict_do_nothing(
            table_name, data, conflict_columns=["report_period_id", "interval_start"]
        )

        self.processed_report.reports[key] = report_id

        return report_id 
Example 26
Project: koku   Author: project-koku   File: ocp_report_processor.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def _create_report_period(self, row, cluster_id, report_db_accessor):
        """Create a report period object.

        Args:
            row (dict): A dictionary representation of a CSV file row
            cluster_id (str): cluster ID

        Returns:
            (str): The DB id of the report period object

        """
        table_name = OCPUsageReportPeriod
        start = ciso8601.parse_datetime(row.get("report_period_start").replace(" +0000 UTC", "+0000"))
        end = ciso8601.parse_datetime(row.get("report_period_end").replace(" +0000 UTC", "+0000"))

        key = (cluster_id, start, self._provider_uuid)
        if key in self.processed_report.report_periods:
            return self.processed_report.report_periods[key]

        if key in self.existing_report_periods_map:
            return self.existing_report_periods_map[key]

        data = {
            "cluster_id": cluster_id,
            "report_period_start": start,
            "report_period_end": end,
            "provider_id": self._provider_uuid,
        }

        report_period_id = report_db_accessor.insert_on_conflict_do_nothing(
            table_name, data, conflict_columns=["cluster_id", "report_period_start", "provider_id"]
        )

        self.processed_report.report_periods[key] = report_period_id

        return report_period_id 
Example 27
Project: spectrify   Author: hellonarrativ   File: timestamps.py    License: MIT License 5 votes vote down vote up
def iso8601_to_nanos(date_str):
    """ Returns a nanoseconds since epoch for a given ISO-8601 date string

        Arguments:
        date: ISO-8601 UTC datetime string (e.g. "2016-01-01 12:00:00.000000")

        Return Values:
        int representing # of nanoseconds since "1970-01-01" for date
    """
    dt = ciso8601.parse_datetime(date_str)
    return unix_time_nanos(dt) 
Example 28
Project: spectrify   Author: hellonarrativ   File: timestamps.py    License: MIT License 5 votes vote down vote up
def iso8601_to_days_since_epoch(date_str):
    dt = ciso8601.parse_datetime(date_str)
    return (dt - epoch).days 
Example 29
Project: target-stitch   Author: singer-io   File: __init__.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def overloaded_parse_message(msg):
    """Parse a message string into a Message object."""

    # We are not using Decimals for parsing here.
    # We recognize that exposes data to potentially
    # lossy conversions.  However, this will affect
    # very few data points and we have chosen to
    # leave conversion as is for now.
    obj = simplejson.loads(msg, use_decimal=True)
    msg_type = _required_key(obj, 'type')

    if msg_type == 'RECORD':
        time_extracted = obj.get('time_extracted')
        if time_extracted:
            try:
                time_extracted = ciso8601.parse_datetime(time_extracted)
            except Exception:
                time_extracted = None
        return singer.RecordMessage(stream=_required_key(obj, 'stream'),
                                    record=_required_key(obj, 'record'),
                                    version=obj.get('version'),
                                    time_extracted=time_extracted)

    if msg_type == 'SCHEMA':
        return singer.SchemaMessage(stream=_required_key(obj, 'stream'),
                                    schema=_required_key(obj, 'schema'),
                                    key_properties=_required_key(obj, 'key_properties'),
                                    bookmark_properties=obj.get('bookmark_properties'))

    if msg_type == 'STATE':
        return singer.StateMessage(value=_required_key(obj, 'value'))

    if msg_type == 'ACTIVATE_VERSION':
        return singer.ActivateVersionMessage(stream=_required_key(obj, 'stream'),
                                             version=_required_key(obj, 'version'))
    return None 
Example 30
def normalize_time(self, event_time):
        """
        Convert time string to ISO-8601 format in UTC timezone.

        Returns None if string representation cannot be parsed.
        """
        datetime = ciso8601.parse_datetime(event_time)
        if datetime:
            return datetime.astimezone(pytz.utc).isoformat()
        else:
            return None