Skip to main content

Jalali datetime binding for python

Project description

jdatetime is Jalali implementation of Python’s datetime module

Status

https://github.com/slashmili/python-jalali/workflows/Tests/badge.svg?branch=main https://ci.appveyor.com/api/projects/status/ge5rk703ydx649a6?svg=true https://img.shields.io/pypi/v/jdatetime.svg https://img.shields.io/pypi/pyversions/jdatetime.svg

Install

pip install jdatetime

Documents

This module exactly follows Python Standard datetime module’s methods http://docs.python.org/release/3.7.1/library/datetime.html

Also these methods are added to jdatetime.date and jdatetime.datetime :

fromgregorian(**kw)
    Convert gregorian to jalali and return jdatetime.date
    jdatetime.date.fromgregorian(day=X,month=X,year=X)
    jdatetime.date.fromgregorian(date=datetime.date)
    jdatetime.datetime.fromgregorian(datetime=datetime.datetime)
togregorian(self)
    Convert current jalali date to gregorian and return datetime.date
isleap(self)
    check if year is leap year
    algortim is based on http://en.wikipedia.org/wiki/Leap_year

Example

>>> import jdatetime
>>> jdatetime.datetime.now()
jdatetime.datetime(1394, 12, 4, 8, 37, 31, 855729)
>>> jdatetime.date.today()
jdatetime.date(1394, 12, 4)

Locale

In order to get the date string in farsi you need to set the locale to jdatetime.FA_LOCALE. The locale could be specified explicitly upon instantiation of date/datetime instances, or by setting a default locale.

Instance locales is named argument only:

import jdatetime
fa_date = jdatetime.date(1397, 4, 23, locale=jdatetime.FA_LOCALE)
fa_datetime = jdatetime.datetime(1397, 4, 23, 11, 40, 30, locale=jdatetime.FA_LOCALE)

date and datetime instances provide the method aslocale() to return a clone of the instance with the same timestamp, in a different locale.

Default Locale

It’s possible to set the default locale, so all new instances created afterwards would use the desired locale, unless explicitly specified otherwise.

>>> import locale
>>> import jdatetime
>> jdatetime.datetime.now().strftime("%a, %d %b %Y %H:%M:%S")
u'Wed, 08 Ord 1395 20:47:32'
>>> locale.setlocale(locale.LC_ALL, jdatetime.FA_LOCALE)
'fa_IR'
>>> jdatetime.datetime.now().strftime("%a, %d %b %Y %H:%M:%S")
u'\u0686\u0647\u0627\u0631\u0634\u0646\u0628\u0647, 08 \u0627\u0631\u062f\u06cc\u0628\u0647\u0634\u062a 1395 20:47:56'

If your requirements demand to support different locales withing the same process, you could set the default locale per thread. New date and datetime instances created in each thread, will use the specified locale by default. This supports both Python threads, and greenlets.

import jdatetime
jdatetime.set_locale(jdatetime.FA_LOCALE)
jdatetime.datetime.now().strftime('%A %B')
# u'\u062f\u0648\u0634\u0646\u0628\u0647 \u062e\u0631\u062f\u0627\u062f'

Development

You can contribute to this project forking it from GitHub and sending pull requests.

First fork the repository and then clone it:

$ git clone git@github.com:<you>/python-jalali.git

Before committing, you can run all the above tests against all supported Python versions with tox. You need to install tox first:

$ pip install tox

And then you can run all tests:

$ tox

If you wish to limit the testing to specific Python version, you can parametrize the tox run:

$ tox -e py39

Release Steps

  • Bump the version in setup.py and jdatetime/__init__.py. We are using Semantic Versioning.

  • Add release notes in CHANGELOG.md

  • Commit and push the changes. Create a PR

  • After the PR is merged, create a release with a tag name like v<version>

  • Github Action creates the package and deploys it to pypi.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jdatetime-5.0.0.tar.gz (21.2 kB view details)

Uploaded Source

Built Distribution

jdatetime-5.0.0-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file jdatetime-5.0.0.tar.gz.

File metadata

  • Download URL: jdatetime-5.0.0.tar.gz
  • Upload date:
  • Size: 21.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for jdatetime-5.0.0.tar.gz
Algorithm Hash digest
SHA256 2cc603d913c0d8e328928454d3d295261cb037e9950227f67c9629ab4710fdf9
MD5 c96a5413a1f3f64f405c1be585d6b538
BLAKE2b-256 1f6ebed5a262bf9a80b4e45507757ae65fb141377eb3800b522eedeed782d865

See more details on using hashes here.

File details

Details for the file jdatetime-5.0.0-py3-none-any.whl.

File metadata

  • Download URL: jdatetime-5.0.0-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for jdatetime-5.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 37e23c710d7e9bde7c37d0cc3ff9e273323889790e902a1ef3ad99148f094fb9
MD5 50810ffc653451d850c9e2f039a3e916
BLAKE2b-256 64e6919b41f6c818dfc47838d0becfd96edac91e4b3d5a76cf73fefce53d88c0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page