Python datetime.isoformat() Examples

The following are 10 code examples of datetime.isoformat(). 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 also want to check out all available functions/classes of the module datetime , or try the search function .
Example #1
Source File:    From airflow with Apache License 2.0 6 votes vote down vote up
def __init__(self,
                 execution_date: Optional[Union[str, datetime.datetime]] = "{{ execution_date.isoformat() }}",
                 recursion_depth: int = 10,
        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
            raise TypeError('Expected str or datetime.datetime type for execution_date. Got {}'
        if recursion_depth <= 0:
            raise ValueError("recursion_depth should be a positive integer")
        self.recursion_depth = recursion_depth 
Example #2
Source File:    From Cirq with 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.

            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.

            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
Source File:    From edx-analytics-pipeline with GNU Affero General Public License v3.0 6 votes vote down vote up
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
Source File:    From micromasters with 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),
            'income_usd': financial_aid.income_usd,
            'justification': None,
            'original_currency': financial_aid.original_currency,
            'original_income': financial_aid.original_income,
            'status': financial_aid.status,
            'updated_on': format_as_iso8601(financial_aid.updated_on),
Example #5
Source File:    From edx-analytics-pipeline with GNU Affero General Public License v3.0 5 votes vote down vote up
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)
        return timestamp 
Example #6
Source File:    From edx-analytics-pipeline with GNU Affero General Public License v3.0 5 votes vote down vote up
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
Source File:    From pairstrade-fyp-2019 with 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)'%s, %s' % (dt.isoformat(), txt)) 
Example #8
Source File:    From pipeline with 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
Source File:    From micromasters with 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
Source File:    From airflow with 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)
            dttm = context['execution_date']

        dttm_filter = dttm if isinstance(dttm, list) else [dttm]
        serialized_dttm_filter = ','.join(
            [datetime.isoformat() for datetime in dttm_filter])
            '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

            if not dag_to_wait:
                raise AirflowException('The external DAG '
                                       '{} does not exist.'.format(self.external_dag_id))
                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.'
            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)

        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))
                raise AirflowException('The external DAG {} failed.'

        return count_allowed == len(dttm_filter)