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 comparison and arithmetic operators such as +, -, ==, >=

    • Support timezone

  • Convert Arabic and Persian characters/digits to each other


Install Package

$ pip install persiantools

How to use

Date:

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

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

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

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

>>> JalaliDate(datetime.date(1990, 9, 23))
JalaliDate(1369, 7, 1, Yekshanbeh)

>>> JalaliDate.to_jalali(2013, 9, 16)
JalaliDate(1392, 6, 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>)

Format

Based on python strftime() behavior

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

>>> JalaliDate(1367, 2, 14).isoformat()
'1367-02-14'

>>> JalaliDate(1395, 3, 1).strftime("%Y/%m/%d")
'1395/03/01'

>>> JalaliDateTime(1369, 7, 1, 14, 0, 10, 0, pytz.utc).strftime("%c")
'Yekshanbeh 01 Mehr 1369 14:00:10'

>>> JalaliDateTime.now(pytz.utc).strftime("%I:%M:%S.%f %p %z %Z")
'01:49:22.518523 PM +0000 UTC'

Digit/Character converter:

>>> from persiantools import characters, 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, 3, 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, 7, 1, Yekshanbeh)

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

persiantools-1.2.0.tar.gz (12.3 kB view details)

Uploaded Source

File details

Details for the file persiantools-1.2.0.tar.gz.

File metadata

  • Download URL: persiantools-1.2.0.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for persiantools-1.2.0.tar.gz
Algorithm Hash digest
SHA256 718e377bcfcf155433f88a6b3149fe445b293246ccb039f4947cc2304146a139
MD5 aad45a4f2f6ac63006a576950d40f947
BLAKE2b-256 02f7a9a741fcb628f12589ab4218f7fa88e3ba5e14295cf86bf2ca270e665f5c

See more details on using hashes here.

Supported by

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