Skip to main content

Jalali date and datetime with other tools

Project description

pypi-ver PyPI-license travic-build Coverage Status python-ver
  • Jalali (Shamsi) date and datetime (based on python datetime’s module)
    • Convert Jalali to Gregorian date/datetime and vice versa
    • Support comparition and arithmetic operators such as +, -, ==, >=
    • Support timezone
  • Convert Arabic and Persian characters/digits to each other

Install Package

$ pip install persiantools==1.0b2

How to use

Date:

>>> from persiantools.jdatetime import JalaliDate
>>> import datetime

>>> JalaliDate.today()
JalaliDate(1395, 04, 18, Jomeh)

>>> JalaliDate.today().to_gregorian()
datetime.date(2016, 7, 8)

>>> JalaliDate(1369, 7, 1)
JalaliDate(1369, 07, 01, Yekshanbeh)

>>> JalaliDate(datetime.date(1990, 9, 23))
JalaliDate(1369, 07, 01, Yekshanbeh)

>>> JalaliDate.to_jalali(2013, 9, 16)
JalaliDate(1392, 06, 25, Doshanbeh)

Datetime:

>>> from persiantools.jdatetime import JalaliDateTime
>>> import datetime, pytz

>>> JalaliDateTime.now()
JalaliDateTime(1395, 4, 18, 1, 43, 24, 720505)

>>> JalaliDateTime.now().to_gregorian()
datetime.datetime(2016, 7, 8, 1, 43, 24, 720505)

>>> JalaliDateTime.now(pytz.timezone("Asia/Tehran"))
JalaliDateTime(1395, 4, 18, 1, 53, 30, 407770, tzinfo=<DstTzInfo 'Asia/Tehran' IRDT+4:30:00 DST>)

>>> JalaliDateTime.now(pytz.utc)
JalaliDateTime(1395, 4, 17, 21, 23, 53, 474618, tzinfo=<UTC>)

Digit/Character converter:

>>> from persiantools import characters
>>> from persiantools import digits

>>> digits.en_to_fa("0987654321")
'۰۹۸۷۶۵۴۳۲۱'

>>> digits.ar_to_fa("٠٩٨٧٦٥٤٣٢١")
'۰۹۸۷۶۵۴۳۲۱'

>>> digits.fa_to_en("۰۹۸۷۶۵۴۳۲۱")
'0987654321'

>>> digits.fa_to_ar("۰۹۸۷۶۵۴۳۲۱")
'٠٩٨٧٦٥٤٣٢١'

>>> characters.ar_to_fa("راك")
'راک'

>>> characters.fa_to_ar("ای چرخ فلک خرابی از کینه تست")
'اي چرخ فلك خرابي از كينه تست'

Operators

>>> from persiantools.jdatetime import JalaliDate, JalaliDateTime
>>> import datetime

>>> JalaliDate(1367, 2, 14) == JalaliDate(datetime.date(1988, 5, 4))
True

>>> JalaliDateTime(1367, 2, 14, 4, 30) >= JalaliDateTime(1369, 7, 1, 1, 0)
False

>>> JalaliDate(1395, 2, 14) + datetime.timedelta(days=38)
JalaliDate(1395, 03, 21, Jomeh)

>>> JalaliDateTime(1395, 12, 30) - JalaliDateTime(1395, 1, 1)
datetime.timedelta(365)

Serializing and de-serializing

>>> from persiantools.jdatetime import JalaliDate
>>> import pickle

>>> # Serializing
>>> file = open("save.p", "wb")
>>> pickle.dump(JalaliDate(1369, 7, 1), file)
>>> file.close()

>>> # de-serializing
>>> file = open("save.p", "rb")
>>> jalali = pickle.load(file)
>>> file.close()
>>> jalali
JalaliDate(1369, 07, 01, Yekshanbeh)

Project details


Download files

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

Files for persiantools, version 1.0b2
Filename, size File type Python version Upload date Hashes
Filename, size persiantools-1.0b2.tar.gz (10.1 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page