Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get execution_date as epoch time?

In regular python code I can do:

import time
int(time.time())

This gives me the time as epoch. I want to be able to do this with airflow macro: execution_date

This is what I tried:

"{{  strptime(execution_date.strftime('%Y-%m-%d %H:%M:%S'), '%d.%m.%Y %H:%M:%S') }}"

But this gives:

jinja2.exceptions.UndefinedError: 'strptime' is undefined

I'm running Airflow 1.9 & Python 2.7

like image 668
KoKu Avatar asked Oct 25 '25 02:10

KoKu


1 Answers

Since Airflow 1.10, Airflow uses Pendulum for datetimes, which has attributes timestamp(), int_timestamp and float_timestamp which return the epoch.

So, you could do:

{{ execution_date.int_timestamp }}

Docs: https://pendulum.eustace.io/docs/#attributes-and-properties

Other options are:

{{ execution_date.strftime('%s') }} # Also Python 2
{{ execution_date.timestamp() }}    # Python >=3.3
like image 77
Bas Harenslak Avatar answered Oct 26 '25 18:10

Bas Harenslak