pyjalali Package

pyjalali Package

pyjalali

Python bindings for libjalali.

Low level API could be accessed through jstr, jtime and jalali modules. Core libjalali data structures resides in types module.

An implementation of standard python:datetime.date and python:datetime.datetime provided in module datetime using libjalali tools.

datetime Module

pyjalali.datetime

High level API for libjalali.

Note

  • There is no iso_calendar or to_ordinal or fromordinal method here. Implementing them is easy, just forward to datetime.gregorian but those methods are not related to Jalali really.
class pyjalali.datetime.date(year, month, day)
ctime()

Return a string representing the date, date(1392, 8, 2).ctime() == 'Thu Aba 02 00:00:00 1392'

day
classmethod fromtimestamp(ts)

Return the local date corresponding to the POSIX timestamp

isoformat()

Return a string representing the date in ISO 8601 format, YYYY-MM-DD. For example date(1392, 8, 2).isoformat() == 1392-08-02'

isoweekday()

Return the day of the week as an integer, where Shanbeh is 1

jtm

Broken-down jalali time structure for this date

max = pyjalali.datetime.date(9999, 12, 29)
min = pyjalali.datetime.date(1, 1, 1)
month
replace(**kw)

Return new date object where values for supplied keyword keywrod arguments reset: date(1392, 2, 8).replace(month=9) == date(1392, 9, 8)`

resolution = datetime.timedelta(1)
strftime(format)

Return a string representing the date, controlled by an explicit format string. Format codes referring to hours, minutes or seconds will see 0 values.

Warning

libjalali’s jstrftime() defines custom formatting directives which might defined in your platform too but for other intentions. Check list of libjalali’s formatting directives.

timetuple()

Return a time.struct_time from this date. DST flag is -1

classmethod today()

Return current local date

weekday()

Return the day of the week as an integer, where Shanbeh is 0

year
class pyjalali.datetime.datetime(year, month, day, hour=None, minute=None, second=None, microsecond=0, tzinfo=None)
astimezone(tz)

Return a datetime object with new tzinfo attribute tz, adjusting the date and time data so the result is the same UTC time as self, but in tz‘s local time.

>>> from pytz import timezone
>>> d1 = datetime.now(timezone('Asia/Tehran'))
>>> d2 = d1.astimezone(timezone('Asia/Dubai'))
>>> d1 == d2, d1 - d2
(True, datetime.timedelta(0))
classmethod combine(date, time)

Make datetime from supplied date and time.

Parameters:
ctime()

Return a string representing the date and time.

>>> datetime(1392, 9, 1, 12, 32, 14, 992).ctime()
'Jom Aza 01 12:32:14 1392'
date()

Return date object with same year, month and day.

day
dst()

If tzinfo is None, returns None, else returns self.tzinfo.dst(self), and raises an exception if the latter doesn’t return None, or a timedelta object.

classmethod fromtimestamp(ts, tz=None)

Return the local date and time corresponding to the POSIX timestamp, such as is returned by time.time(). If optional argument tz is None or not specified, the timestamp is converted to the platform’s local date and time, and the returned datetime object is naive.

gregorian

Return Gregorian python:datetime.datetime object corresponding to this datetime. Result will cached for future uses.

hour
isoformat(sep='T')

Return a string representing the date and time in ISO 8601 format, YYYY-MM-DDTHH:MM:SS.mmmmmm or, if :attr`:.microsecond is 0, YYYY-MM-DDTHH:MM:SS. The optional argument sep (default ‘T’) is a separator, placed between the date and time portions of the result.

>>> datetime(1392, 9, 1, 12, 32, 14, 992).isoformat(' ')
'1392-09-01 12:32:14.992'
isoweekday()

Return the day of the week as an integer, where Shanbeh is 1

jtm

Broken-down jalali time structure for this date

max = pyjalali.datetime.datetime(9999, 12, 29, 23, 59, 59, 0)
min = pyjalali.datetime.datetime(1, 1, 1, 0, 0, 0, 0)
minute
month
classmethod now(tz=None)

Return the current local date and time. If a timezone provided, date and time will adjusted to that timezone.

replace(**kw)

Return a datetime with the same attributes, except for those attributes given new values by whichever keyword arguments are specified.

resolution = datetime.timedelta(0, 1)
second
strftime(format)

Return a string representing the date and time, controlled by an explicit format string.

classmethod strptime(date_str, format)

Return a datetime corresponding to date_str, parsed according to format.

time()

Return python:datetime.time object with same hour, minute, second and microsecond. tzinfo is None.

timetuple()

Return a time.struct_time from this date. The tm_isdst flag of the result is set according to the dst() method: tzinfo is None or dst() returns None, tm_isdst is set to -1; else if dst() returns a non-zero value, tm_isdst is set to 1; else tm_isdst is set to 0.

timetz()

Return python:datetime.time object with same hour, minute, second, microsecond, and tzinfo attributes.

classmethod today()

Return the current local datetime, with tzinfo None.

tzinfo = None
tzname()

If tzinfo is None, returns None, else returns self.tzinfo.tzname(self), raises an exception if the latter doesn’t return None or a string object.

classmethod utcfromtimestamp(ts)

Return the UTC datetime corresponding to the POSIX timestamp, with tzinfo None.

classmethod utcnow()

Return the current UTC date and time, with tzinfo None. This is like now(), but returns the current UTC date and time, as a naive datetime object.

utcoffset()

Timezone offset. If tzinfo is None, returns None, else returns self.tzinfo.utcoffset(self), and raises an exception if the latter doesn’t return None, or a datetime.timedelta object.

>>> from pytz import timezone,AmbiguousTimeError,NonExistentTimeError
>>> timezone('Asia/Tehran').utcoffset(datetime(1390, 1, 1, 10, 2))
datetime.timedelta(0, 12600)
>>> timezone('Asia/Tehran').utcoffset(datetime(1390, 6, 30, 22, 30))
datetime.timedelta(0, 16200)
>>> ambiguous = datetime(1392, 6, 30, 23, 30)
>>> try:
...     timezone('Asia/Tehran').utcoffset(ambiguous)
... except AmbiguousTimeError:
...     print "caught"
caught
>>> non_existent = datetime(1390, 1, 2, 0, 45)
>>> try:
...     timezone('Asia/Tehran').utcoffset(non_existent)
... except NonExistentTimeError:
...     print "caught"
caught
utctimetuple()
weekday()

Return the day of the week as an integer, where Shanbeh is 0.

year
pyjalali.datetime.j2g(date_or_datetime)

Alias for gregorian_from_jalali().

pyjalali.datetime.g2j(date_or_datetime)

Alias for jalali_from_gregorian().

pyjalali.datetime.now(timezone=None)

See datetime.now().

pyjalali.datetime.utcnow()

See datetime.utcnow().

pyjalali.datetime.jalali_from_gregorian(date_or_datetime)

Make Jalali datetime from Gregorian python:datetime.datetime or make Jalali date from Gregorian python:datetime.date.

>>> from datetime import datetime as _dtm, date as _dt
>>> jalali_from_gregorian(_dtm(2013, 11, 23, 23, 46, 0, 703498))
pyjalali.datetime.datetime(1392, 9, 2, 23, 46, 0, 703498)
>>> jalali_from_gregorian(_dtm(2013, 4, 13, 21, 10, 2, 292))
pyjalali.datetime.datetime(1392, 1, 24, 21, 10, 2, 292)
>>> jalali_from_gregorian(_dtm(2013, 3, 22, 0, 12))
pyjalali.datetime.datetime(1392, 1, 2, 0, 12, 0, 0)
>>> jalali_from_gregorian(_dt(2013, 3, 21))
pyjalali.datetime.date(1392, 1, 1)
pyjalali.datetime.gregorian_from_jalali(date_or_datetime)

Make Gregorian python:datetime.datetime from Jalali datetime or make Gregorian python:datetime.date from Jalali date.

>>> gregorian_from_jalali(datetime(1392, 9, 2, 23, 10, 2))
datetime.datetime(2013, 11, 23, 23, 10, 2)
>>> gregorian_from_jalali(datetime(1392, 6, 30, 22, 30))
datetime.datetime(2013, 9, 21, 22, 30)
>>> gregorian_from_jalali(date(1392, 6, 30))
datetime.date(2013, 9, 21)
pyjalali.datetime.datetime_from_ts(ts, local, tz=None)

Return datetime from provided timestamp ts.

Parameters:
  • ts (int) – timestamp
  • local (bool) – if True, return local date else return date in UTC
  • tz (datetime.tzinfo) – if provided, make timezone aware datetime discarding effect of local parameter

jalali Module

pyjalali.jalali

libjalali custom functions.

pyjalali.jalali.jalali_create_date_from_days(jtm, silent=False)

Alter provided types.struct_jtm object’s fields tm_mon and tm_mday based on its tm_yday field. In case of failure raise ValueError exception if silent is not True.

pyjalali.jalali.jalali_create_date_from_days(jtm, silent=False)

Alter provided types.struct_jtm object’s fields tm_mon and tm_mday based on its tm_yday field. In case of failure raise ValueError exception if silent is not True.

pyjalali.jalali.jalali_create_secs_from_time(ab_jtm)

Return number of seconds elapsed since UTC Epoch based on supplied types.struct_ab_jtm.

pyjalali.jalali.jalali_create_time_from_secs(timestamp)

Return types.struct_ab_jtm from given timestamp.

pyjalali.jalali.jalali_get_date(days)

Calculates Jalali date based on given number of days since UTC Epoch and return result as types.struct_jtm.

pyjalali.jalali.jalali_get_diff(jtm, silent=False)

Return number of days passed since UTC Epoch based on given types.struct_jtm. In case of failure raise ValueError exception if silent is not True.

pyjalali.jalali.jalali_get_jyear_info(jyinfo)

Fill given types.struct_jyinfo object’s fields with year information based on given year by types.struct_jyinfo.y.

pyjalali.jalali.jalali_year_month_days(year, month)

Return number of days in provided month of year. Month number starts at zero

pyjalali.jalali.jalali_is_jleap(year)

Return True if given year is leap year else False.

pyjalali.jalali.jalali_update(jtm)

Updates given types.struct_jtm object’s fields based on its tm_year, tm_mon and tm_mday.

jstr Module

pyjalali.jstr

String formatting and deformatting

pyjalali.jstr.jstrftime(format, jtm)

Return string representation of given time according to format.

Parameters:
pyjalali.jstr.jstrptime(format, date_str)

Return types.struct_jtm from date_str according to format.

Parameters:
  • format (string) – format of string representation
  • date_str (string) – string representation

jtime Module

pyjalali.time

Time functions.

Functions jasctime, jctime, jgmtime and jlocaltime are forwarded to reentrant backends.

pyjalali.jtime.jasctime(jtm, retain_nl=False)

Return string representation of given time.

Parameters:
pyjalali.jtime.jctime(timestamp, retain_nl=False)

Return string representation of time from timestamp.

Parameters:
  • timestamp (int) –
  • retain_nl (bool) – keep trailing newline character
pyjalali.jtime.jgmtime(timestamp)

Return types.struct_jtm from timestamp expressed in UTC.

pyjalali.jtime.jlocaltime(timestamp)

Make types.struct_jtm from timestamp according to local zone and dst settings.

pyjalali.jtime.jmktime(jtm)

Return timestamp from provided time.

:param pyjalali.types.struct_jtm jtm

types Module

pyjalali.types

Core C types for libjalali binding.

pyjalali.types.jtm_to_struct_time(src_jtm)

Make time.struct_time from broken-down jalali time structure

class pyjalali.types.struct_ab_jtm

Time passed since UTC Epoch

ab_days

Structure/Union member

ab_hour

Structure/Union member

ab_min

Structure/Union member

ab_sec

Structure/Union member

class pyjalali.types.struct_jtm

Broken-down jalali date and time

copy()

New instance of this object

tm_gmtoff

Structure/Union member

tm_hour

Structure/Union member

tm_isdst

Structure/Union member

tm_mday

Structure/Union member

tm_min

Structure/Union member

tm_mon

Structure/Union member

tm_sec

Structure/Union member

tm_wday

Structure/Union member

tm_yday

Structure/Union member

tm_year

Structure/Union member

tm_zone

Structure/Union member

class pyjalali.types.struct_jyinfo

Year specific information

apl

Structure/Union member

lf

Structure/Union member

p

Structure/Union member

pl

Structure/Union member

r

Structure/Union member

rl

Structure/Union member

y

Structure/Union member

pyjalali.types.time_t_p

alias of LP_c_int