Coder Social home page Coder Social logo

jinja2-time's Introduction

Jinja2 Time

PyPI Package PyPI Python Versions PyPI Package License See Build Status on Travis CI

Jinja2 Extension for Dates and Times

Installation

jinja2-time is available for download from PyPI via pip:

$ pip install jinja2-time

It will automatically install jinja2 along with arrow.

Usage

Now Tag

The extension comes with a now tag that provides convenient access to the arrow.now() API from your templates.

You can control the output by specifying a format, that will be passed to Python's strftime():

from jinja2 import Environment

env = Environment(extensions=['jinja2_time.TimeExtension'])

# Timezone 'local', default format -> "2015-12-10"
template = env.from_string("{% now 'local' %}")

# Timezone 'utc', explicit format -> "Thu, 10 Dec 2015 15:49:01"
template = env.from_string("{% now 'utc', '%a, %d %b %Y %H:%M:%S' %}")

# Timezone 'Europe/Berlin', explicit format -> "CET +0100"
template = env.from_string("{% now 'Europe/Berlin', '%Z %z' %}")

# Timezone 'utc', explicit format -> "2015"
template = env.from_string("{% now 'utc', '%Y' %}")

template.render()

Default Datetime Format

TimeExtension extends the environment with a datetime_format attribute.

It is used as a fallback if you omit the format for now.

from jinja2 import Environment

env = Environment(extensions=['jinja2_time.TimeExtension'])

env.datetime_format = '%a, %d %b %Y %H:%M:%S'

# Timezone 'utc', default format -> "Thu, 10 Dec 2015 15:49:01"
template = env.from_string("{% now 'utc' %}")

template.render()

Time Offset

jinja2-time implements a convenient interface to modify now by a relative time offset:

# Examples for now "2015-12-09 23:33:01"

# "Thu, 10 Dec 2015 01:33:31"
"{% now 'utc' + 'hours=2, seconds=30' %}"

# "Wed, 09 Dec 2015 23:22:01"
"{% now 'utc' - 'minutes=11' %}"

# "07 Dec 2015 23:00:00"
"{% now 'utc' - 'days=2, minutes=33, seconds=1', '%d %b %Y %H:%M:%S' %}"

Further documentation on the underlying functionality can be found in the arrow replace docs.

Issues

If you encounter any problems, please file an issue along with a detailed description.

Code of Conduct

Everyone interacting in the jinja2-time project's codebases, issue trackers, chat rooms, and mailing lists is expected to follow the PyPA Code of Conduct.

License

Distributed under the terms of the MIT license, jinja2-time is free and open source software

OSI certified

jinja2-time's People

Contributors

hackebrot avatar luzfcb avatar blackdole avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.