Converting UNIX time to datetime object in Jinja templates - python

I want to convert my timestamp to datetime in jinja2..
here's my sample code:
import time
date = time.time()
self.tv['date'] = date
sample html:
<p>{{ date }}</p>
I want to convert it to datetime using jinja2 in python..
thanks..

Make a custom filter like
#app.template_filter('ctime')
def timectime(s):
return time.ctime(s) # datetime.datetime.fromtimestamp(s)
And use your template filter
{{ date | ctime }}

You convert it before passing it to a template, eg:
>>> import time
>>> date = time.time()
>>> from datetime import datetime
>>> datetime.fromtimestamp(date)
datetime.datetime(2013, 3, 1, 2, 57, 29, 472572)
And optionally use formatting:
>>> format(datetime.fromtimestamp(date), '%Y%m%d')
'20130301'

Use this in the template:
{{ time | from_timestamp('%Y.%m.%d. %H:%M:%S UTC') }}

Use the following,
Data
{
"timestamp": "1424197820"
}
Template
{{ timestamp|timestamp_to_time|datetimeformat('%a, %B %d') }}
Rendered
Tue, February 17
Source

Related

The context processor that will pass the variable to the template year

I'm trying to write a context processor core.context_processors.year.year that will pass a variable year to the template, the value of which will be the current year as a number: © {{ year }} Copyright
from datetime import datetime, date
def year(request):
return {
request, datetime.now().date()
}
You need to return a dictionary, not a set. That dictionary should contain as key the name of the variable, and as value the value you want to associate with that, so here this will look like:
from django.utils.timezone import now
def year(request):
return {
'year': now().year
}
You can use the Built-in "now" templatetag https://docs.djangoproject.com/en/3.2/ref/templates/builtins/#now
© {% now "Y" %} Copyright
I figured out how to do it, initially I did it wrong. Thanks to everyone who responded.
from datetime import datetime
def year(request):
dt = datetime.now().year
return {
'year': dt
}

AIRFLOW: use .replace() or relativedelta() in jinja template for {{ds}}

My goal is to return 1st day of previous month based on airflow macro variable {{ds}} and use it e.g. in HiveOperator.
E.g. For ds = 2020-05-09 I expect to return: 2020-04-01
The solution I found and tried were:
SET hivevar:LAST_MONTH='{{ (ds.replace(day=1) - macros.timedelta(days=1)).replace(day=1) }}';
SET hivevar:LAST_MONTH='{{ ds + macros.dateutil.relativedelta.relativedelta(months=-1, day=1) }}'
But both resulted in errors:
Error rendering template: replace() takes no keyword arguments
Error rendering template: must be str, not relativedelta
and when rendered didn't show any dates.
What am I doing wrong?
You can use:
{{ (execution_date + macros.dateutil.relativedelta.relativedelta(months=-1, day=1)).strftime("%Y-%m-%d") }}
Example:
from airflow.operators.bash_operator import BashOperator
default_args = {
'owner': 'airflow',
'start_date': datetime(2020, 4, 1),
}
with DAG(dag_id='stackoverflow',
default_args=default_args,
schedule_interval=None,
catchup=False
) as dag:
run_this = BashOperator(
task_id='example',
bash_command='echo ds is {{ ds }} modified ds is {{ (execution_date + macros.dateutil.relativedelta.relativedelta(months=-1, day=1)).strftime("%Y-%m-%d") }}',
)
According to airflow doc {{ execution_date }} is deprecated. So, you can keep using {{ ds }}, but you should convert it into datetime. Of course {{ ds }} depends on you cron time.
If you cron time is monthly, you could use only replace() function:
"{{ macros.datetime.strptime(ds, '%Y-%m-%d').replace(day=1) }}"
If you cron time is daily, use replace() and then relativedelta():
"{{ (macros.datetime.strptime(ds, '%Y-%m-%d').replace(day=1) - macros.dateutil.relativedelta.relativedelta(months=1)).strftime('%Y-%m-%d') }}

AttributeError: type object 'datetime.datetime' has no attribute 'datetime' even after using import datetime

I had the following codes in my flask project that modifies the timestamp. I haven't modified anything, but the codes now throw the above mentioned error.
I have referred all the similar questions on StackOverflow but all suggests that it's due to using from datetime import datetime instead of import datetime. But in my following code, I am using import datetime. Yet, still I get AttributeError: type object 'datetime.datetime' has no attribute 'datetime'.
My code:
view.py
import datetime
#app.template_filter('datetimeformat')
def datetimeformat(value, format):
d_obj = datetime.datetime.strptime(value, '%Y-%m-%d')
return d_obj.strftime(format)
#app.template_filter('timestampformat')
def timestampformat(value, format):
d_obj = datetime.datetime.strptime(value, '%Y-%m-%d %H:%M:%S') # This is where the error occurs.
current_date = datetime.date.today()
timestamp_date = datetime.datetime.strptime(value, '%Y-%m-%d %H:%M:%S').date()
if timestamp_date == current_date:
timestamp_time = datetime.datetime.strptime(value, '%Y-%m-%d %H:%M:%S').time()
delta = datetime.timedelta(hours=5, minutes=30)
timestamp_local = ((datetime.datetime.combine(datetime.date(1,1,1),timestamp_time) + delta).time())
return "Today at {}".format(timestamp_local.strftime('%I:%M %p'))
else:
return d_obj.strftime(format)
template.html (registration['timestamp'] = 2019-09-13 13:29:47)
...
<td>{{ registration['timestamp'] }}</td>
{% if 'Today' in registration['timestamp']|timestampformat('%B %d, %Y %A \n %I:%M %p') %}
<td><span class="timestamp today">{{ registration['timestamp']|timestampformat('%B %d, %Y, %A, %I:%M %p') }}</span></td>
{% else %}
<td><span class="timestamp">{{ registration['timestamp']|timestampformat('%B %d, %Y, %A, %I:%M %p') }}</span></td>
{% endif %}
...
I am really confused what went wrong.
From the python documentation, modules are only loaded once per session for efficiency reasons. A simple restart of your interpreter (or in this case, server) should solve the problem.

How do I convert seconds to hh:mm:ss in django template

So I have a dictionary called match. This has details of a match and one of the keys are game_time. If the game time is <= 10, the match is in draft. Else, I want to report the game time but the value its stored in is just in seconds.
{% if match.game_time <= 10 %}
drafting
{% else %}
{{match.game_time|date: "Z"}}
{% endif %}
Just gives me an error
Could not parse the remainder: ': "Z"' from 'match.game_time|date: "Z"'
any help appreciated
Use this formate for time
{{your_date_field|time:"h:i a"}}
from datetime import datetime, timedelta
def millisec_to_time(millisec):
d = datetime(1, 1, 1)+millisec
if d.day - 1 == 0:
return "{0}:{1}:{2}".format(d.hour, d.minute, d.second)
else:
return "{0}:{1}:{2}:{3}".format(d.day-1, d.hour, d.minute, d.second)
Try this to convert sec/msec to actual time.
If you install mathfilters, add it to your install_apps, and load it into your template then you can do this.
{{ booking.duration|div:3600|floatformat:"2" }}

Why do my dates and times differ?

I have in my jinja2 template code for localization.
{% set currency = "SEK" %}
{% set format = "sv_SE" %}
{% set timezoneinfo = 'Europe/Stockholm' %}
{% set locale = "se" %}
But it's not working for hours and minutes if I use it like this with a filter using values from a google search API result to filter.
{{scored_document.fields.10.value|datetimeformat_list(hour=scored_document.fields.17.value|int ,minute =scored_document.fields.18.value|int, timezoneinfo=timezoneinfo, locale=locale)}}
filter
def datetimeformat_list(date, hour, minute, locale='en', timezoneinfo='Asia/Calcutta'):
tzinfo = timezone(timezoneinfo)
input = datetime(date.year, date.month, date.day, int(hour), int(minute), tzinfo=tzinfo)
time_str = format_time(input, 'H:mm', tzinfo=tzinfo, locale=locale)
return "{0}".format(time_str)
The code gives a different time than if I just do
{{ ad.modified|datetimeformat_viewad(locale='se', timezoneinfo='Europe/Stockholm') }}
with this filter
def datetimeformat_viewad(to_format, locale='en', timezoneinfo='Asia/Calcutta'):
tzinfo = timezone(timezoneinfo)
month = MONTHS[to_format.month - 1]
date_str = '{0} {1}'.format(to_format.day, _(month))
time_str = format_time(to_format, 'H:mm', tzinfo=tzinfo, locale=locale)
return "{0} {1}".format(date_str, time_str)
Why are the outputs not the same time?
It is incorrect to pass an arbitrary pytz timezone to datetime constructor directly; you should use pytz_tzinfo.localize() method instead as it is said at the very beginning of pytz docs.
There could be other issues in your code.

Categories