Python pendulum.timezone() Examples
The following are 30
code examples of pendulum.timezone().
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: test_taskinstance.py From airflow with Apache License 2.0 | 6 votes |
def test_post_execute_hook(self): """ Test that post_execute hook is called with the Operator's result. The result ('error') will cause an error to be raised and trapped. """ class TestError(Exception): pass class TestOperator(PythonOperator): def post_execute(self, context, result=None): if result == 'error': raise TestError('expected error.') dag = models.DAG(dag_id='test_post_execute_dag') task = TestOperator( task_id='test_operator', dag=dag, python_callable=lambda: 'error', owner='airflow', start_date=timezone.datetime(2017, 2, 1)) ti = TI(task=task, execution_date=timezone.utcnow()) with self.assertRaises(TestError): ti.run()
Example #2
Source File: test_taskinstance.py From airflow with Apache License 2.0 | 6 votes |
def test_mark_non_runnable_task_as_success(self): """ test that running task with mark_success param update task state as SUCCESS without running task despite it fails dependency checks. """ non_runnable_state = ( set(State.task_states) - RUNNABLE_STATES - set(State.SUCCESS)).pop() dag = models.DAG(dag_id='test_mark_non_runnable_task_as_success') task = DummyOperator( task_id='test_mark_non_runnable_task_as_success_op', dag=dag, pool='test_pool', owner='airflow', start_date=timezone.datetime(2016, 2, 1, 0, 0, 0)) ti = TI( task=task, execution_date=timezone.utcnow(), state=non_runnable_state) # TI.run() will sync from DB before validating deps. with create_session() as session: session.add(ti) session.commit() ti.run(mark_success=True) self.assertEqual(ti.state, State.SUCCESS)
Example #3
Source File: test_taskinstance.py From airflow with Apache License 2.0 | 6 votes |
def test_run_pooling_task_with_mark_success(self): """ test that running task in an existing pool with mark_success param update task state as SUCCESS without running task despite it fails dependency checks. """ dag = models.DAG(dag_id='test_run_pooling_task_with_mark_success') task = DummyOperator( task_id='test_run_pooling_task_with_mark_success_op', dag=dag, pool='test_pool', owner='airflow', start_date=timezone.datetime(2016, 2, 1, 0, 0, 0)) ti = TI( task=task, execution_date=timezone.utcnow()) ti.run(mark_success=True) self.assertEqual(ti.state, State.SUCCESS)
Example #4
Source File: test_taskinstance.py From airflow with Apache License 2.0 | 6 votes |
def test_run_pooling_task_with_skip(self): """ test that running task which returns AirflowSkipOperator will end up in a SKIPPED state. """ def raise_skip_exception(): raise AirflowSkipException dag = models.DAG(dag_id='test_run_pooling_task_with_skip') task = PythonOperator( task_id='test_run_pooling_task_with_skip', dag=dag, python_callable=raise_skip_exception, owner='airflow', start_date=timezone.datetime(2016, 2, 1, 0, 0, 0)) ti = TI( task=task, execution_date=timezone.utcnow()) ti.run() self.assertEqual(State.SKIPPED, ti.state)
Example #5
Source File: test_taskinstance.py From airflow with Apache License 2.0 | 6 votes |
def test_timezone_awareness(self): naive_datetime = DEFAULT_DATE.replace(tzinfo=None) # check ti without dag (just for bw compat) op_no_dag = DummyOperator(task_id='op_no_dag') ti = TI(task=op_no_dag, execution_date=naive_datetime) self.assertEqual(ti.execution_date, DEFAULT_DATE) # check with dag without localized execution_date dag = DAG('dag', start_date=DEFAULT_DATE) op1 = DummyOperator(task_id='op_1') dag.add_task(op1) ti = TI(task=op1, execution_date=naive_datetime) self.assertEqual(ti.execution_date, DEFAULT_DATE) # with dag and localized execution_date tzinfo = pendulum.timezone("Europe/Amsterdam") execution_date = timezone.datetime(2016, 1, 1, 1, 0, 0, tzinfo=tzinfo) utc_date = timezone.convert_to_utc(execution_date) ti = TI(task=op1, execution_date=execution_date) self.assertEqual(ti.execution_date, utc_date)
Example #6
Source File: test_dag.py From airflow with Apache License 2.0 | 6 votes |
def test_sync_to_db_default_view(self, mock_now): dag = DAG( 'dag', start_date=DEFAULT_DATE, default_view="graph", ) with dag: DummyOperator(task_id='task', owner='owner1') SubDagOperator( task_id='subtask', owner='owner2', subdag=DAG( 'dag.subtask', start_date=DEFAULT_DATE, ) ) now = datetime.datetime.utcnow().replace(tzinfo=pendulum.timezone('UTC')) mock_now.return_value = now session = settings.Session() dag.sync_to_db(session=session) orm_dag = session.query(DagModel).filter(DagModel.dag_id == 'dag').one() self.assertIsNotNone(orm_dag.default_view) self.assertEqual(orm_dag.default_view, "graph") session.close()
Example #7
Source File: forms.py From airflow with Apache License 2.0 | 6 votes |
def process_formdata(self, valuelist): if not valuelist: return date_str = ' '.join(valuelist) try: # Check if the datetime string is in the format without timezone, if so convert it to the # default timezone if len(date_str) == 19: parsed_datetime = dt.strptime(date_str, '%Y-%m-%d %H:%M:%S') default_timezone = self._get_default_timezone() self.data = default_timezone.convert(parsed_datetime) else: self.data = pendulum.parse(date_str) except ValueError: self.data = None raise ValueError(self.gettext('Not a valid datetime value'))
Example #8
Source File: test_timezone.py From pendulum with MIT License | 6 votes |
def test_datetime(): tz = timezone("Europe/Paris") dt = tz.datetime(2013, 3, 24, 1, 30) assert dt.year == 2013 assert dt.month == 3 assert dt.day == 24 assert dt.hour == 1 assert dt.minute == 30 assert dt.second == 0 assert dt.microsecond == 0 dt = tz.datetime(2013, 3, 31, 2, 30) assert dt.year == 2013 assert dt.month == 3 assert dt.day == 31 assert dt.hour == 3 assert dt.minute == 30 assert dt.second == 0 assert dt.microsecond == 0
Example #9
Source File: test_taskinstance.py From airflow with Apache License 2.0 | 6 votes |
def test_does_not_retry_on_airflow_fail_exception(self): def fail(): raise AirflowFailException("hopeless") dag = models.DAG(dag_id='test_does_not_retry_on_airflow_fail_exception') task = PythonOperator( task_id='test_raise_airflow_fail_exception', dag=dag, python_callable=fail, owner='airflow', start_date=timezone.datetime(2016, 2, 1, 0, 0, 0), retries=1 ) ti = TI(task=task, execution_date=timezone.utcnow()) try: ti.run() except AirflowFailException: pass # expected self.assertEqual(State.FAILED, ti.state)
Example #10
Source File: test_timezone.py From pendulum with MIT License | 6 votes |
def test_on_last_transition(): tz = pendulum.timezone("Europe/Paris") dt = pendulum.naive(2037, 10, 25, 2, 30) dt = tz.convert(dt, dst_rule=pendulum.POST_TRANSITION) assert dt.year == 2037 assert dt.month == 10 assert dt.day == 25 assert dt.hour == 2 assert dt.minute == 30 assert dt.second == 0 assert dt.microsecond == 0 assert dt.utcoffset().total_seconds() == 3600 dt = pendulum.naive(2037, 10, 25, 2, 30) dt = tz.convert(dt, dst_rule=pendulum.PRE_TRANSITION) assert dt.year == 2037 assert dt.month == 10 assert dt.day == 25 assert dt.hour == 2 assert dt.minute == 30 assert dt.second == 0 assert dt.microsecond == 0 assert dt.utcoffset().total_seconds() == 7200
Example #11
Source File: test_timezone.py From pendulum with MIT License | 5 votes |
def test_utcoffset(): tz = pendulum.timezone("America/Guayaquil") utcoffset = tz.utcoffset(pendulum.now("UTC")) assert utcoffset == timedelta(0, -18000)
Example #12
Source File: test_timezone.py From pendulum with MIT License | 5 votes |
def test_convert_accept_pendulum_instance(): dt = pendulum.datetime(2016, 8, 7, 12, 53, 54) tz = timezone("Europe/Paris") new = tz.convert(dt) assert isinstance(new, pendulum.DateTime) assert_datetime(new, 2016, 8, 7, 14, 53, 54)
Example #13
Source File: test_timezone.py From pendulum with MIT License | 5 votes |
def test_repeated_time_pre_rule(): dt = datetime(2013, 10, 27, 2, 30, 45, 123456, fold=0) tz = timezone("Europe/Paris") dt = tz.convert(dt) assert dt.year == 2013 assert dt.month == 10 assert dt.day == 27 assert dt.hour == 2 assert dt.minute == 30 assert dt.second == 45 assert dt.microsecond == 123456 assert dt.tzinfo.name == "Europe/Paris" assert dt.tzinfo.utcoffset(dt) == timedelta(seconds=7200) assert dt.tzinfo.dst(dt) == timedelta(seconds=3600)
Example #14
Source File: test_timezone.py From pendulum with MIT License | 5 votes |
def test_utcoffset_pre_transition(): tz = pendulum.timezone("America/Chicago") utcoffset = tz.utcoffset(datetime(1883, 11, 18)) assert utcoffset == timedelta(days=-1, seconds=64800)
Example #15
Source File: test_timezones.py From pendulum with MIT License | 5 votes |
def test_timezones_are_loadable(zone): pendulum.timezone(zone)
Example #16
Source File: test_behavior.py From pendulum with MIT License | 5 votes |
def d(): return time(12, 34, 56, 123456, tzinfo=pendulum.timezone("Europe/Paris"))
Example #17
Source File: test_behavior.py From pendulum with MIT License | 5 votes |
def p(): return pendulum.Time(12, 34, 56, 123456, tzinfo=pendulum.timezone("Europe/Paris"))
Example #18
Source File: test_timezone.py From pendulum with MIT License | 5 votes |
def test_astimezone(): d = pendulum.datetime(2015, 1, 15, 18, 15, 34) now = pendulum.datetime(2015, 1, 15, 18, 15, 34) assert d.timezone_name == "UTC" assert_datetime(d, now.year, now.month, now.day, now.hour, now.minute) d = d.astimezone(pendulum.timezone("Europe/Paris")) assert d.timezone_name == "Europe/Paris" assert_datetime(d, now.year, now.month, now.day, now.hour + 1, now.minute)
Example #19
Source File: test_timezone.py From pendulum with MIT License | 5 votes |
def test_repr(): tz = timezone("Europe/Paris") assert "Timezone('Europe/Paris')" == repr(tz)
Example #20
Source File: test_timezone.py From pendulum with MIT License | 5 votes |
def test_repeated_time_explicit_pre_rule(): dt = datetime(2013, 10, 27, 2, 30, 45, 123456) tz = timezone("Europe/Paris") dt = tz.convert(dt, dst_rule=pendulum.PRE_TRANSITION) assert dt.year == 2013 assert dt.month == 10 assert dt.day == 27 assert dt.hour == 2 assert dt.minute == 30 assert dt.second == 45 assert dt.microsecond == 123456 assert dt.tzinfo.name == "Europe/Paris" assert dt.tzinfo.utcoffset(dt) == timedelta(seconds=7200) assert dt.tzinfo.dst(dt) == timedelta(seconds=3600)
Example #21
Source File: dag.py From airflow with Apache License 2.0 | 5 votes |
def timezone(self): return settings.TIMEZONE
Example #22
Source File: test_timezone.py From pendulum with MIT License | 5 votes |
def test_repeated_time_explicit_post_rule(): dt = datetime(2013, 10, 27, 2, 30, 45, 123456) tz = timezone("Europe/Paris") dt = tz.convert(dt, dst_rule=pendulum.POST_TRANSITION) assert dt.year == 2013 assert dt.month == 10 assert dt.day == 27 assert dt.hour == 2 assert dt.minute == 30 assert dt.second == 45 assert dt.microsecond == 123456 assert dt.tzinfo.name == "Europe/Paris" assert dt.tzinfo.utcoffset(dt) == timedelta(seconds=3600) assert dt.tzinfo.dst(dt) == timedelta()
Example #23
Source File: test_timezone.py From pendulum with MIT License | 5 votes |
def test_repeated_time(): dt = datetime(2013, 10, 27, 2, 30, 45, 123456, fold=1) tz = timezone("Europe/Paris") dt = tz.convert(dt) assert dt.year == 2013 assert dt.month == 10 assert dt.day == 27 assert dt.hour == 2 assert dt.minute == 30 assert dt.second == 45 assert dt.microsecond == 123456 assert dt.tzinfo.name == "Europe/Paris" assert dt.tzinfo.utcoffset(dt) == timedelta(seconds=3600) assert dt.tzinfo.dst(dt) == timedelta()
Example #24
Source File: test_timezone.py From pendulum with MIT License | 5 votes |
def test_skipped_time_with_error(): dt = datetime(2013, 3, 31, 2, 30, 45, 123456) tz = timezone("Europe/Paris") with pytest.raises(NonExistingTime): tz.convert(dt, dst_rule=pendulum.TRANSITION_ERROR)
Example #25
Source File: test_timezone.py From pendulum with MIT License | 5 votes |
def test_skipped_time_with_explicit_post_rule(): dt = datetime(2013, 3, 31, 2, 30, 45, 123456) tz = timezone("Europe/Paris") dt = tz.convert(dt, dst_rule=pendulum.POST_TRANSITION) assert dt.year == 2013 assert dt.month == 3 assert dt.day == 31 assert dt.hour == 3 assert dt.minute == 30 assert dt.second == 45 assert dt.microsecond == 123456 assert dt.tzinfo.name == "Europe/Paris" assert dt.tzinfo.utcoffset(dt) == timedelta(seconds=7200) assert dt.tzinfo.dst(dt) == timedelta(seconds=3600)
Example #26
Source File: test_timezone.py From pendulum with MIT License | 5 votes |
def test_skipped_time_with_post_rule(): dt = datetime(2013, 3, 31, 2, 30, 45, 123456, fold=1) tz = timezone("Europe/Paris") dt = tz.convert(dt) assert dt.year == 2013 assert dt.month == 3 assert dt.day == 31 assert dt.hour == 3 assert dt.minute == 30 assert dt.second == 45 assert dt.microsecond == 123456 assert dt.tzinfo.name == "Europe/Paris" assert dt.tzinfo.utcoffset(dt) == timedelta(seconds=7200) assert dt.tzinfo.dst(dt) == timedelta(seconds=3600)
Example #27
Source File: test_timezone.py From pendulum with MIT License | 5 votes |
def test_skipped_time_with_pre_rule(): dt = datetime(2013, 3, 31, 2, 30, 45, 123456, fold=0) tz = timezone("Europe/Paris") dt = tz.convert(dt) assert dt.year == 2013 assert dt.month == 3 assert dt.day == 31 assert dt.hour == 1 assert dt.minute == 30 assert dt.second == 45 assert dt.microsecond == 123456 assert dt.tzinfo.name == "Europe/Paris" assert dt.tzinfo.utcoffset(dt) == timedelta(seconds=3600) assert dt.tzinfo.dst(dt) == timedelta()
Example #28
Source File: test_timezone.py From pendulum with MIT License | 5 votes |
def test_basic_convert(): dt = datetime(2016, 6, 1, 12, 34, 56, 123456, fold=1) tz = timezone("Europe/Paris") dt = tz.convert(dt) assert dt.year == 2016 assert dt.month == 6 assert dt.day == 1 assert dt.hour == 12 assert dt.minute == 34 assert dt.second == 56 assert dt.microsecond == 123456 assert dt.tzinfo.name == "Europe/Paris" assert dt.tzinfo.utcoffset(dt) == timedelta(seconds=7200) assert dt.tzinfo.dst(dt) == timedelta(seconds=3600)
Example #29
Source File: conftest.py From parsec-cloud with GNU Affero General Public License v3.0 | 5 votes |
def mock_timezone_utc(request): # Mock and non-UTC timezones are a really bad mix, so keep things simple with pendulum.tz.LocalTimezone.test(pendulum.timezone("utc")): yield
Example #30
Source File: dag.py From airflow with Apache License 2.0 | 5 votes |
def get_run_dates(self, start_date, end_date=None): """ Returns a list of dates between the interval received as parameter using this dag's schedule interval. Returned dates can be used for execution dates. :param start_date: the start date of the interval :type start_date: datetime :param end_date: the end date of the interval, defaults to timezone.utcnow() :type end_date: datetime :return: a list of dates within the interval following the dag's schedule :rtype: list """ run_dates = [] using_start_date = start_date using_end_date = end_date # dates for dag runs using_start_date = using_start_date or min([t.start_date for t in self.tasks]) using_end_date = using_end_date or timezone.utcnow() # next run date for a subdag isn't relevant (schedule_interval for subdags # is ignored) so we use the dag run's start date in the case of a subdag next_run_date = (self.normalize_schedule(using_start_date) if not self.is_subdag else using_start_date) while next_run_date and next_run_date <= using_end_date: run_dates.append(next_run_date) next_run_date = self.following_schedule(next_run_date) return run_dates