Jalali (Shamsi) date and datetime (based on python datetime's module)

Overview

PersianTools

PyPI Travis (.org) AppVeyor Coveralls PyPI - Python Version PyPI - License

  • 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(1369, 7, 1)
JalaliDate(1369, 7, 1, Yekshanbeh)

>>> JalaliDate(datetime.date(1990, 9, 23))      # Gregorian to Jalali
JalaliDate(1369, 7, 1, Yekshanbeh)

>>> JalaliDate.to_jalali(2013, 9, 16)           # Gregorian to Jalali
JalaliDate(1392, 6, 25, Doshanbeh)

>>> JalaliDate(1392, 6, 25).to_gregorian()      # Jalali to Gregorian
datetime.date(2013, 9, 16)

>>> JalaliDate.fromtimestamp(578707200)         # Timestamp to Jalali
JalaliDate(1367, 2, 14, Chaharshanbeh)

Datetime

) >>> JalaliDateTime.now(pytz.utc) JalaliDateTime(1395, 4, 17, 21, 23, 53, 474618, tzinfo= )">
>>> from persiantools.jdatetime import JalaliDateTime
>>> import datetime, pytz

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

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

>>> JalaliDateTime.to_jalali(datetime.datetime(1988, 5, 4, 14, 0, 0, 0))    # Gregorian to Jalali
JalaliDateTime(1367, 2, 14, 14, 0)

>>> JalaliDateTime.fromtimestamp(578723400, pytz.timezone("Asia/Tehran"))   # Timestamp to Jalali
JalaliDateTime(1367, 2, 14, 8, 0, tzinfo=
    
     )

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

     
    

Format

Based on python strftime() behavior

>> 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'">
>>> 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

>> digits.ar_to_fa("٠٩٨٧٦٥٤٣٢١") # or digits.ar_to_fa(u"٠٩٨٧٦٥٤٣٢١") '۰۹۸۷۶۵۴۳۲۱' >>> digits.fa_to_en("۰۹۸۷۶۵۴۳۲۱") '0987654321' >>> digits.fa_to_ar("۰۹۸۷۶۵۴۳۲۱") '٠٩٨٧٦٥٤٣٢١' >>> characters.ar_to_fa("كيك") 'کیک'">
>>> from persiantools import characters, digits

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

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

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

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

>>> characters.ar_to_fa("كيك")
'کیک'

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(1367, 2, 14) == datetime.date(1988, 5, 4)
True

>>> 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

>> pickle.dump(JalaliDate(1367, 2, 14), file) >>> file.close() >>> # de-serializing >>> file = open("save.p", "rb") >>> jalali = pickle.load(file) >>> file.close() >>> jalali JalaliDate(1367, 2, 14, Chaharshanbeh)">
>>> from persiantools.jdatetime import JalaliDate
>>> import pickle

>>> # Serializing
>>> file = open("save.p", "wb")
>>> pickle.dump(JalaliDate(1367, 2, 14), file)
>>> file.close()

>>> # de-serializing
>>> file = open("save.p", "rb")
>>> jalali = pickle.load(file)
>>> file.close()
>>> jalali
JalaliDate(1367, 2, 14, Chaharshanbeh)
Comments
  • Accept unicode strings as input

    Accept unicode strings as input

    The replace method in utils.py starts with checking if the input is of type string. This is not ideal because in python 2.x the strings are often of type unicode and that renders some of the functionalities of persiantools unusbale.

    For instance, this won't work in persiantools:

    digits.fa_to_en(u'۱۲۳۴')
    

    This can be address by allowing the unicode data-type here

    opened by Huji 1
  • Add some other Persian tools

    Add some other Persian tools

    Add some tools like: is_valid_national_id, generate_random_national_id, clean_mobile_number, is_valid_mobile_number and some digit convertor Refactor some codes and make them more pythonic

    enhancement 
    opened by torkashvand 1
  • Why the result is not the same in Python and Sublime Text 3?

    Why the result is not the same in Python and Sublime Text 3?

    When testing your module, the result in Sublime Text 3 and Python IDLE is the same and none of them show the Day name (for instance: Jomeh), but when using the Python or CMD directly to work with the same code, it works fine! Test:

    from persiantools.jdatetime import JalaliDate from datetime import datetime print(datetime.today()) # Output: 2018-08-24 12:33:20.700418 print(JalaliDate.today()) # Output: 1397-06-02 print(JalaliDate(1369, 7, 1)) # Output: 1369-07-01

    1 2

    opened by 0xdolan 1
  • installation error

    installation error

    Collecting persiantools Downloading persiantools-1.2.0.tar.gz Complete output from command python setup.py egg_info: Traceback (most recent call last): File "", line 1, in File "/tmp/pip-build-7vMtxF/persiantools/setup.py", line 14, in long_description=readme(), File "/tmp/pip-build-7vMtxF/persiantools/setup.py", line 7, in readme with open('README.rst', encoding='utf8') as f: TypeError: 'encoding' is an invalid keyword argument for this function

    ----------------------------------------
    

    Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-7vMtxF/persiantools/

    bug 
    opened by mhkarimi 1
  • Getting more done in GitHub with ZenHub

    Getting more done in GitHub with ZenHub

    Hola! @mhajiloo has created a ZenHub account for the mhajiloo organization. ZenHub is the only project management tool integrated natively in GitHub – created specifically for fast-moving, software-driven teams.


    How do I use ZenHub?

    To get set up with ZenHub, all you have to do is download the browser extension and log in with your GitHub account. Once you do, you’ll get access to ZenHub’s complete feature-set immediately.

    What can ZenHub do?

    ZenHub adds a series of enhancements directly inside the GitHub UI:

    • Real-time, customizable task boards for GitHub issues;
    • Multi-Repository burndown charts, estimates, and velocity tracking based on GitHub Milestones;
    • Personal to-do lists and task prioritization;
    • Time-saving shortcuts – like a quick repo switcher, a “Move issue” button, and much more.

    Add ZenHub to GitHub

    Still curious? See more ZenHub features or read user reviews. This issue was written by your friendly ZenHub bot, posted by request from @mhajiloo.

    ZenHub Board

    opened by mhajiloo 0
  • Extract the distance between two dates

    Extract the distance between two dates

    How do I take the time interval between two dates based on the date ?? For example, the first date: 1400/02/26 Second Date: 1400/03/26 And I want to extract a list of days between these two dates

    opened by Bestsenator 0
  • a new function with a better name

    a new function with a better name

    I think you can write a new function like convert_numbers and accept from and to from the user. So? Then you don't need four functions that basically do the same thing.

    def convert_numbers(from, to, string):
        return output
    
    opened by GreatBahram 0
  • unnecessary utility

    unnecessary utility

    It seems you don't need utils at all. You can use some kind of comprehension to make the code even much more readable and independent.

    return ''.join(
        dic.get(char, char) # maybe dic is not a good name though
        for char in string
    )
    

    https://github.com/mhajiloo/persiantools/blob/a88112b5955cb8214e84467357bee61cc01242ba/persiantools/digits.py#L28

    opened by GreatBahram 0
  • No need to use compile here

    No need to use compile here

    People usually use compile object, when they want to use the sample pattern thousand of times, here you don't need it (maybe).

    https://github.com/mhajiloo/persiantools/blob/a88112b5955cb8214e84467357bee61cc01242ba/persiantools/utils.py#L9

    opened by GreatBahram 0
  • make this one better

    make this one better

    https://github.com/mhajiloo/persiantools/blob/a88112b5955cb8214e84467357bee61cc01242ba/persiantools/utils.py#L14-L32

    isintance takes a tuple of classes for the second argument. So you could basicly do something like this:

    if not isinstance(value, (int, float)):
        raise TypeError('value should int or float object')
    return int(value)
    
    opened by GreatBahram 0
Owner
Majid Hajiloo
Majid Hajiloo
Datetimes for Humans™

Maya: Datetimes for Humans™ Datetimes are very frustrating to work with in Python, especially when dealing with different locales on different systems

Timo Furrer 3.4k Dec 28, 2022
A datetime parser in Python by Ari24-cb24 and NekoFantic

datetimeparser A datetime parser in Python by Ari24-cb24 and NekoFantic V 1.0 Erinnerung für den Parser Auf falsche Eingaben überprüfen Liste an Event

AriDevelopment 13 Dec 30, 2022
Useful extensions to the standard Python datetime features

dateutil - powerful extensions to datetime The dateutil module provides powerful extensions to the standard datetime module, available in Python. Inst

null 2k Dec 29, 2022
A simple in-process python scheduler library, designed to be integrated seamlessly with the `datetime` standard library.

scheduler A simple in-process python scheduler library, designed to be integrated seamlessly with the datetime standard library. Due to the support of

null 30 Dec 30, 2022
Make Python datetime formatting human readable

Make Python datetime formatting human readable

James Timmins 0 Oct 3, 2021
Croniter provides iteration for the datetime object with a cron like format

Introduction Contents Introduction Travis badge Usage About DST About second repeats Testing if a date matches a crontab Gaps between date matches Ite

kiorky 152 Dec 30, 2022
Parse human-readable date/time strings

parsedatetime Parse human-readable date/time strings. Python 2.6 or greater is required for parsedatetime version 1.0 or greater. While we still test

Mike Taylor 651 Dec 23, 2022
ISO 8601 date/time parser

ISO 8601 date/time parser This module implements ISO 8601 date, time and duration parsing. The implementation follows ISO8601:2004 standard, and imple

null 118 Dec 20, 2022
A Python module that tries to figure out what your local timezone is

tzlocal This Python module returns a tzinfo object with the local timezone information under Unix and Windows. It requires either Python 3.9+ or the b

Lennart Regebro 159 Dec 16, 2022
TimeTagger is a web-based time-tracking solution that can be run locally or on a server

TimeTagger is a web-based time-tracking solution that can be run locally or on a server. In the latter case, you'll want to add authentication, and also be aware of the license restrictions.

Almar Klein 626 Jan 6, 2023
A Python 3 library for parsing human-written times and dates

Chronyk A small Python 3 library containing some handy tools for handling time, especially when it comes to interfacing with those pesky humans. Featu

Felix Wiegand 339 Dec 19, 2022
pytz Python historical timezone library and database

pytz Brings the IANA tz database into Python. This library allows accurate and cross platform timezone calculations. pytz contains generated code, and

Stub 236 Jan 3, 2023
Generate and work with holidays in Python

python-holidays A fast, efficient Python library for generating country, province and state specific sets of holidays on the fly. It aims to make dete

Maurizio Montel 881 Dec 29, 2022
darts is a Python library for easy manipulation and forecasting of time series.

A python library for easy manipulation and forecasting of time series.

Unit8 5.2k Jan 1, 2023
A Python library for dealing with dates

moment A Python library for dealing with dates/times. Inspired by Moment.js and Kenneth Reitz's Requests library. Ideas were also taken from the Times

Zach Williams 709 Dec 9, 2022
Friendly Python Dates

When.py: Friendly Dates and Times Production: Development: User-friendly functions to help perform common date and time actions. Usage To get the syst

Andy Dirnberger 191 Oct 14, 2022
Better dates & times for Python

Arrow: Better dates & times for Python Arrow is a Python library that offers a sensible and human-friendly approach to creating, manipulating, formatt

Arrow 8.2k Jan 5, 2023
python parser for human readable dates

Python parser for human readable dates Key Features • How To Use • Installation • Common use cases • You may also like... • License Key Features Suppo

Scrapinghub 2.2k Jan 8, 2023
A simple digital clock made with the help of python

Digital-Clock ⏰ Description ?? ✔️ A simple digital clock made with the help of python. The code is easy to understand and implement. With this reposit

Mohit 0 Dec 10, 2021