Python datetime.isoformat() Examples

The following are 10 code examples for showing how to use datetime.isoformat(). 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 datetime , or try the search function .

Example 1
Project: airflow   Author: apache   File: external_task_sensor.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self,
                 external_dag_id,
                 external_task_id,
                 execution_date: Optional[Union[str, datetime.datetime]] = "{{ execution_date.isoformat() }}",
                 recursion_depth: int = 10,
                 *args,
                 **kwargs):
        super().__init__(*args, **kwargs)
        self.external_dag_id = external_dag_id
        self.external_task_id = external_task_id
        if isinstance(execution_date, datetime.datetime):
            self.execution_date = execution_date.isoformat()
        elif isinstance(execution_date, str):
            self.execution_date = execution_date
        else:
            raise TypeError('Expected str or datetime.datetime type for execution_date. Got {}'
                            .format(type(execution_date)))
        if recursion_depth <= 0:
            raise ValueError("recursion_depth should be a positive integer")
        self.recursion_depth = recursion_depth 
Example 2
Project: Cirq   Author: quantumlib   File: calibration.py    License: Apache License 2.0 6 votes vote down vote up
def timestamp_str(self,
                      tz: Optional[datetime.tzinfo] = None,
                      timespec: str = 'auto') -> str:
        """Return a string for the calibration timestamp.

        Args:
            tz: The timezone for the string. If None, the method uses the
                platform's local date and time.
            timespec: See datetime.isoformat for valid values.

        Returns:
            The string in ISO 8601 format YYYY-MM-DDTHH:MM:SS.ffffff.
        """
        dt = datetime.datetime.fromtimestamp(self.timestamp / 1000, tz)
        dt += datetime.timedelta(microseconds=self.timestamp % 1000000)
        return dt.isoformat(sep=' ', timespec=timespec) 
Example 3
def add_microseconds(timestamp, microseconds):
    """
    Add given microseconds to a timestamp.

    Input and output are timestamps as ISO format strings.  Microseconds can be negative.
    """
    # First try to parse the timestamp string and do simple math, to avoid
    # the high cost of using strptime to parse in most cases.
    timestamp_base, _period, microsec_base = timestamp.partition('.')
    if not microsec_base:
        microsec_base = '0'
        timestamp = ensure_microseconds(timestamp)
    microsec_int = int(microsec_base) + microseconds
    if microsec_int >= 0 and microsec_int < 1000000:
        return "{}.{}".format(timestamp_base, str(microsec_int).zfill(6))

    # If there's a carry, then just use the datetime library.
    parsed_timestamp = datetime.datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%f')
    newtimestamp = (parsed_timestamp + datetime.timedelta(microseconds=microseconds)).isoformat()
    return ensure_microseconds(newtimestamp) 
Example 4
Project: micromasters   Author: mitodl   File: utils_test.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_datetime(self):
        """
        Test that a model with a datetime and date field is handled correctly
        """
        financial_aid = FinancialAidFactory.create(justification=None)
        assert serialize_model_object(financial_aid) == {
            'country_of_income': financial_aid.country_of_income,
            'country_of_residence': financial_aid.country_of_residence,
            'created_on': format_as_iso8601(financial_aid.created_on),
            'date_documents_sent': financial_aid.date_documents_sent.isoformat(),
            'date_exchange_rate': format_as_iso8601(financial_aid.date_exchange_rate),
            'id': financial_aid.id,
            'income_usd': financial_aid.income_usd,
            'justification': None,
            'original_currency': financial_aid.original_currency,
            'original_income': financial_aid.original_income,
            'status': financial_aid.status,
            'tier_program': financial_aid.tier_program.id,
            'updated_on': format_as_iso8601(financial_aid.updated_on),
            'user': financial_aid.user.id,
        } 
Example 5
def ensure_microseconds(timestamp):
    """
    Make sure timestamp strings contain microseconds, even if zero.

    This is needed after datetime.isoformat() calls, which truncate zero microseconds.
    """
    if '.' not in timestamp:
        return '{datetime}.000000'.format(datetime=timestamp)
    else:
        return timestamp 
Example 6
def mysql_datetime_to_isoformat(mysql_datetime):
    """
    Convert mysql datetime strings to isoformat standard.

    Mysql outputs strings of the form '2012-07-25 12:26:22.0'.
    Log files use isoformat strings for sorting.
    """
    date_parts = [int(d) for d in re.split(r'[:\-\. ]', mysql_datetime)]
    if len(date_parts) > 6:
        tenths = date_parts[6]
        date_parts[6] = tenths * 100000
    timestamp = datetime.datetime(*date_parts).isoformat()
    return ensure_microseconds(timestamp) 
Example 7
Project: pairstrade-fyp-2019   Author: wywongbd   File: ptstrategy.py    License: MIT License 5 votes vote down vote up
def log(txt, dt=None, data=None):
        dt = dt or data.datetime[0]
        dt = bt.num2date(dt)
        _logger.info('%s, %s' % (dt.isoformat(), txt)) 
Example 8
Project: pipeline   Author: ooni   File: cli.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def isodatetime(s):
    # Reverse of `datetime.isoformat()` besides microseconds & timezones
    return datetime.datetime.strptime(s, "%Y-%m-%dT%H:%M:%S") 
Example 9
Project: micromasters   Author: mitodl   File: utils_test.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def format_as_iso8601(time):
    """Helper function to format datetime with the Z at the end"""
    # Can't use datetime.isoformat() because format is slightly different from this
    iso_format = '%Y-%m-%dT%H:%M:%S'
    formatted_time = time.strftime(iso_format)
    if time.microsecond:
        miniseconds_format = '.%f'
        formatted_time += time.strftime(miniseconds_format)[:4]
    return formatted_time + "Z" 
Example 10
Project: airflow   Author: apache   File: external_task_sensor.py    License: Apache License 2.0 4 votes vote down vote up
def poke(self, context, session=None):
        if self.execution_delta:
            dttm = context['execution_date'] - self.execution_delta
        elif self.execution_date_fn:
            dttm = self._handle_execution_date_fn(context=context)
        else:
            dttm = context['execution_date']

        dttm_filter = dttm if isinstance(dttm, list) else [dttm]
        serialized_dttm_filter = ','.join(
            [datetime.isoformat() for datetime in dttm_filter])

        self.log.info(
            'Poking for %s.%s on %s ... ',
            self.external_dag_id, self.external_task_id, serialized_dttm_filter
        )

        DM = DagModel
        # we only do the check for 1st time, no need for subsequent poke
        if self.check_existence and not self.has_checked_existence:
            dag_to_wait = session.query(DM).filter(
                DM.dag_id == self.external_dag_id
            ).first()

            if not dag_to_wait:
                raise AirflowException('The external DAG '
                                       '{} does not exist.'.format(self.external_dag_id))
            else:
                if not os.path.exists(dag_to_wait.fileloc):
                    raise AirflowException('The external DAG '
                                           '{} was deleted.'.format(self.external_dag_id))

            if self.external_task_id:
                refreshed_dag_info = DagBag(dag_to_wait.fileloc).get_dag(self.external_dag_id)
                if not refreshed_dag_info.has_task(self.external_task_id):
                    raise AirflowException('The external task'
                                           '{} in DAG {} does not exist.'
                                           .format(self.external_task_id,
                                                   self.external_dag_id))
            self.has_checked_existence = True

        count_allowed = self.get_count(dttm_filter, session, self.allowed_states)

        count_failed = -1
        if len(self.failed_states) > 0:
            count_failed = self.get_count(dttm_filter, session, self.failed_states)

        session.commit()
        if count_failed == len(dttm_filter):
            if self.external_task_id:
                raise AirflowException('The external task {} in DAG {} failed.'
                                       .format(self.external_task_id, self.external_dag_id))
            else:
                raise AirflowException('The external DAG {} failed.'
                                       .format(self.external_dag_id))

        return count_allowed == len(dttm_filter)