Python pendulum.DateTime() Examples

The following are 30 code examples of pendulum.DateTime(). 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 pendulum , or try the search function .
Example #1
Source File: settings.py    From airflow with Apache License 2.0 6 votes vote down vote up
def configure_adapters():
    """ Register Adapters and DB Converters """
    from pendulum import DateTime as Pendulum
    try:
        from sqlite3 import register_adapter
        register_adapter(Pendulum, lambda val: val.isoformat(' '))
    except ImportError:
        pass
    try:
        import MySQLdb.converters
        MySQLdb.converters.conversions[Pendulum] = MySQLdb.converters.DateTime2literal
    except ImportError:
        pass
    try:
        import pymysql.converters
        pymysql.converters.conversions[Pendulum] = pymysql.converters.escape_datetime
    except ImportError:
        pass 
Example #2
Source File: dep_context.py    From airflow with Apache License 2.0 6 votes vote down vote up
def ensure_finished_tasks(self, dag, execution_date: pendulum.DateTime, session: Session):
        """
        This method makes sure finished_tasks is populated if it's currently None.
        This is for the strange feature of running tasks without dag_run.

        :param dag: The DAG for which to find finished tasks
        :type dag: airflow.models.DAG
        :param execution_date: The execution_date to look for
        :param session: Database session to use
        :return: A list of all the finished tasks of this DAG and execution_date
        :rtype: list[airflow.models.TaskInstance]
        """
        if self.finished_tasks is None:
            self.finished_tasks = dag.get_task_instances(
                start_date=execution_date,
                end_date=execution_date,
                state=State.finished() + [State.UPSTREAM_FAILED],
                session=session,
            )
        return self.finished_tasks 
Example #3
Source File: test_construct.py    From pendulum with MIT License 6 votes vote down vote up
def test_timedelta_behavior():
    dt1 = pendulum.DateTime(2000, 11, 20, 1)
    dt2 = pendulum.DateTime(2000, 11, 25, 2)
    dt3 = pendulum.DateTime(2016, 11, 5, 3)

    p1 = pendulum.period(dt1, dt3)
    p2 = pendulum.period(dt2, dt3)
    it1 = p1.as_timedelta()
    it2 = p2.as_timedelta()

    assert it1.total_seconds() == p1.total_seconds()
    assert it2.total_seconds() == p2.total_seconds()
    assert it1.days == p1.days
    assert it2.days == p2.days
    assert it1.seconds == p1.seconds
    assert it2.seconds == p2.seconds
    assert it1.microseconds == p1.microseconds
    assert it2.microseconds == p2.microseconds 
Example #4
Source File: test_construct.py    From pendulum with MIT License 6 votes vote down vote up
def test_accuracy():
    dt1 = pendulum.DateTime(2000, 11, 20)
    dt2 = pendulum.DateTime(2000, 11, 25)
    dt3 = pendulum.DateTime(2016, 11, 5)
    p1 = pendulum.period(dt1, dt3)
    p2 = pendulum.period(dt2, dt3)

    assert p1.years == 15
    assert p1.in_years() == 15
    assert p1.months == 11
    assert p1.in_months() == 191
    assert p1.days == 5829
    assert p1.remaining_days == 2
    assert p1.in_days() == 5829

    assert p2.years == 15
    assert p2.in_years() == 15
    assert p2.months == 11
    assert p2.in_months() == 191
    assert p2.days == 5824
    assert p2.remaining_days == 4
    assert p2.in_days() == 5824 
Example #5
Source File: test_construct.py    From pendulum with MIT License 5 votes vote down vote up
def test_with_datetimes():
    dt1 = datetime(2000, 1, 1)
    dt2 = datetime(2000, 1, 31)
    p = pendulum.period(dt1, dt2)

    assert isinstance(p.start, pendulum.DateTime)
    assert isinstance(p.end, pendulum.DateTime)
    assert_datetime(p.start, 2000, 1, 1)
    assert_datetime(p.end, 2000, 1, 31) 
Example #6
Source File: test_start_end_of.py    From pendulum with MIT License 5 votes vote down vote up
def test_end_of_month_is_fluid():
    d = pendulum.now()
    assert isinstance(d.end_of("month"), pendulum.DateTime) 
Example #7
Source File: test_start_end_of.py    From pendulum with MIT License 5 votes vote down vote up
def test_end_of_year_is_fluid():
    d = pendulum.now()
    assert isinstance(d.end_of("year"), pendulum.DateTime) 
Example #8
Source File: test_start_end_of.py    From pendulum with MIT License 5 votes vote down vote up
def test_start_of_decade_is_fluid():
    d = pendulum.now()
    assert isinstance(d.start_of("decade"), pendulum.DateTime) 
Example #9
Source File: test_start_end_of.py    From pendulum with MIT License 5 votes vote down vote up
def test_end_of_decade_is_fluid():
    d = pendulum.now()
    assert isinstance(d.end_of("decade"), pendulum.DateTime) 
Example #10
Source File: test_start_end_of.py    From pendulum with MIT License 5 votes vote down vote up
def test_end_of_century_is_fluid():
    d = pendulum.now()
    assert isinstance(d.end_of("century"), pendulum.DateTime) 
Example #11
Source File: test_start_end_of.py    From pendulum with MIT License 5 votes vote down vote up
def test_average_is_fluid():
    d = pendulum.now().average()
    assert isinstance(d, pendulum.DateTime) 
Example #12
Source File: test_construct.py    From pendulum with MIT License 5 votes vote down vote up
def test_today():
    today = pendulum.today()
    assert isinstance(today, DateTime) 
Example #13
Source File: test_construct.py    From pendulum with MIT License 5 votes vote down vote up
def test_tomorrow():
    now = pendulum.now().start_of("day")
    tomorrow = pendulum.tomorrow()
    assert isinstance(tomorrow, DateTime)
    assert now.diff(tomorrow).in_days() == 1 
Example #14
Source File: test_construct.py    From pendulum with MIT License 5 votes vote down vote up
def test_yesterday():
    now = pendulum.now().start_of("day")
    yesterday = pendulum.yesterday()

    assert isinstance(yesterday, DateTime)
    assert now.diff(yesterday, False).in_days() == -1 
Example #15
Source File: test_getters.py    From pendulum with MIT License 5 votes vote down vote up
def test_is_past():
    with pendulum.test(DateTime(2000, 1, 1)):
        d = pendulum.now()
        assert not d.is_past()
        d = d.subtract(days=1)
        assert d.is_past() 
Example #16
Source File: test_from_format.py    From pendulum with MIT License 5 votes vote down vote up
def test_from_format_returns_datetime():
    d = pendulum.from_format("1975-05-21 22:32:11", "YYYY-MM-DD HH:mm:ss")
    assert_datetime(d, 1975, 5, 21, 22, 32, 11)
    assert isinstance(d, pendulum.DateTime)
    assert "UTC" == d.timezone_name 
Example #17
Source File: test_from_format.py    From pendulum with MIT License 5 votes vote down vote up
def test_strptime():
    d = pendulum.DateTime.strptime("1975-05-21 22:32:11", "%Y-%m-%d %H:%M:%S")
    assert_datetime(d, 1975, 5, 21, 22, 32, 11)
    assert isinstance(d, pendulum.DateTime)
    assert "UTC" == d.timezone_name 
Example #18
Source File: test_range.py    From pendulum with MIT License 5 votes vote down vote up
def test_iter():
    dt1 = pendulum.datetime(2000, 1, 1, 12, 45, 37)
    dt2 = pendulum.datetime(2000, 1, 31, 12, 45, 37)

    p = Period(dt1, dt2)
    i = 0
    for dt in p:
        assert isinstance(dt, pendulum.DateTime)
        i += 1

    assert i == 31 
Example #19
Source File: test_start_end_of.py    From pendulum with MIT License 5 votes vote down vote up
def test_end_of_day():
    d = pendulum.now()
    new = d.end_of("day")
    assert isinstance(new, pendulum.DateTime)
    assert_datetime(new, d.year, d.month, d.day, 23, 59, 59, 999999) 
Example #20
Source File: test_construct.py    From pendulum with MIT License 5 votes vote down vote up
def test_inverted():
    dt1 = pendulum.DateTime(2000, 1, 1)
    dt2 = pendulum.DateTime(2000, 1, 31)
    p = pendulum.period(dt2, dt1)

    assert_datetime(p.start, 2000, 1, 31)
    assert_datetime(p.end, 2000, 1, 1) 
Example #21
Source File: test_construct.py    From pendulum with MIT License 5 votes vote down vote up
def test_inverted_and_absolute():
    dt1 = pendulum.DateTime(2000, 1, 1)
    dt2 = pendulum.DateTime(2000, 1, 31)
    p = pendulum.period(dt2, dt1, True)

    assert_datetime(p.start, 2000, 1, 1)
    assert_datetime(p.end, 2000, 1, 31) 
Example #22
Source File: test_hashing.py    From pendulum with MIT License 5 votes vote down vote up
def test_periods_with_same_duration_and_different_dates():
    day1 = pendulum.DateTime(2018, 1, 1)
    day2 = pendulum.DateTime(2018, 1, 2)
    day3 = pendulum.DateTime(2018, 1, 2)

    period1 = day2 - day1
    period2 = day3 - day2

    assert period1 != period2
    assert len({period1, period2}) == 2 
Example #23
Source File: test_arithmetic.py    From pendulum with MIT License 5 votes vote down vote up
def test_multiply():
    dt1 = pendulum.DateTime(2016, 8, 7, 12, 34, 56)
    dt2 = dt1.add(days=6, seconds=34)
    it = pendulum.period(dt1, dt2)
    mul = it * 2
    assert isinstance(mul, pendulum.Duration)
    assert_duration(mul, 0, 0, 1, 5, 0, 1, 8)

    dt1 = pendulum.DateTime(2016, 8, 7, 12, 34, 56)
    dt2 = dt1.add(days=6, seconds=34)
    it = pendulum.period(dt1, dt2)
    mul = it * 2
    assert isinstance(mul, pendulum.Duration)
    assert_duration(mul, 0, 0, 1, 5, 0, 1, 8) 
Example #24
Source File: test_arithmetic.py    From pendulum with MIT License 5 votes vote down vote up
def test_divide():
    dt1 = pendulum.DateTime(2016, 8, 7, 12, 34, 56)
    dt2 = dt1.add(days=2, seconds=34)
    it = pendulum.period(dt1, dt2)
    mul = it / 2
    assert isinstance(mul, pendulum.Duration)
    assert_duration(mul, 0, 0, 0, 1, 0, 0, 17)

    dt1 = pendulum.DateTime(2016, 8, 7, 12, 34, 56)
    dt2 = dt1.add(days=2, seconds=35)
    it = pendulum.period(dt1, dt2)
    mul = it / 2
    assert isinstance(mul, pendulum.Duration)
    assert_duration(mul, 0, 0, 0, 1, 0, 0, 17) 
Example #25
Source File: test_arithmetic.py    From pendulum with MIT License 5 votes vote down vote up
def test_floor_divide():
    dt1 = pendulum.DateTime(2016, 8, 7, 12, 34, 56)
    dt2 = dt1.add(days=2, seconds=34)
    it = pendulum.period(dt1, dt2)
    mul = it // 2
    assert isinstance(mul, pendulum.Duration)
    assert_duration(mul, 0, 0, 0, 1, 0, 0, 17)

    dt1 = pendulum.DateTime(2016, 8, 7, 12, 34, 56)
    dt2 = dt1.add(days=2, seconds=35)
    it = pendulum.period(dt1, dt2)
    mul = it // 3
    assert isinstance(mul, pendulum.Duration)
    assert_duration(mul, 0, 0, 0, 0, 16, 0, 11) 
Example #26
Source File: schema.py    From pytezos with MIT License 5 votes vote down vote up
def encode_literal(value, prim, binary=False):
    if prim in ['int', 'nat']:
        core_type = 'int'
        value = str(value)
    elif prim == 'timestamp':
        core_type = 'string'
        if isinstance(value, int):
            value = pendulum.from_timestamp(value)
        if isinstance(value, pendulum.DateTime):
            value = value.strftime('%Y-%m-%dT%H:%M:%SZ')
    elif prim == 'mutez':
        core_type = 'int'
        if isinstance(value, Decimal):
            value = int(value * 10 ** 6)
        if isinstance(value, int):
            value = str(value)
    elif prim == 'bool':
        core_type = 'prim'
        value = 'True' if value else 'False'
    elif prim == 'bytes':
        core_type = 'bytes'
    else:
        core_type = 'string'
        value = str(value)

    return {core_type: value} 
Example #27
Source File: date.py    From choochoo with GNU General Public License v2.0 5 votes vote down vote up
def local_date_to_time(date, none=False):
    if none and date is None: return None
    date = to_date(date)
    ptime = p.DateTime(year=date.year, month=date.month, day=date.day,
                       tzinfo=p.tz.get_local_timezone()).in_timezone(dt.timezone.utc)
    return dt.datetime(*ptime.timetuple()[:6], tzinfo=dt.timezone.utc) 
Example #28
Source File: date.py    From choochoo with GNU General Public License v2.0 5 votes vote down vote up
def time_to_local_date(time):
    time = to_time(time)
    ptime = p.DateTime(*time.timetuple()[:6], tzinfo=dt.timezone.utc).in_timezone(p.tz.get_local_timezone())
    return dt.date(year=ptime.year, month=ptime.month, day=ptime.day) 
Example #29
Source File: test_parameters.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_should_works_with_datetime_formatter(self):
        decorator = format_parameters({"param_a": format_datetime})
        endpoint = mock.MagicMock()
        decorated_endpoint = decorator(endpoint)

        decorated_endpoint(param_a='2020-01-01T0:0:00+00:00')

        endpoint.assert_called_once_with(param_a=DateTime(2020, 1, 1, 0, tzinfo=Timezone('UTC'))) 
Example #30
Source File: test_taskinstance.py    From airflow with Apache License 2.0 5 votes vote down vote up
def _test_previous_dates_setup(schedule_interval: Union[str, datetime.timedelta, None],
                                   catchup: bool, scenario: List[str]) -> list:
        dag_id = 'test_previous_dates'
        dag = models.DAG(dag_id=dag_id, schedule_interval=schedule_interval, catchup=catchup)
        task = DummyOperator(task_id='task', dag=dag, start_date=DEFAULT_DATE)

        def get_test_ti(session, execution_date: pendulum.DateTime, state: str) -> TI:
            dag.create_dagrun(
                run_type=DagRunType.SCHEDULED,
                state=state,
                execution_date=execution_date,
                start_date=pendulum.now('UTC'),
                session=session
            )
            ti = TI(task=task, execution_date=execution_date)
            ti.set_state(state=State.SUCCESS, session=session)
            return ti

        with create_session() as session:  # type: Session

            date = cast(pendulum.DateTime, pendulum.parse('2019-01-01T00:00:00+00:00'))

            ret = []

            for idx, state in enumerate(scenario):
                new_date = date.add(days=idx)
                ti = get_test_ti(session, new_date, state)
                ret.append(ti)

            return ret