kamyarmg
kamyarmg

Reputation: 904

Iranian Solar Hijri Calendar in Python

Is there any library in python like calendar module for the Persian solar Hijri calendar I want to print a month like this, for the Iranian calendar (solar Hijri):

>>> from calendar import TextCalendar
>>> print(TextCalendar().formatmonth(2021, 10))

   October 2021
Mo Tu We Th Fr Sa Su
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Upvotes: 1

Views: 532

Answers (1)

TheFaultInOurStars
TheFaultInOurStars

Reputation: 3608

I am not sure whether there is a module that does the job for you, but if you are interested in getting the Persian(Jalali) month days, you can use the function below:

import jdatetime
import pandas as pd
import numpy as np
def get_jalali_calander(jalali_year, jalali_month):
  if jalali_month == 12:
    check_month = 1
    check_year = jalali_year+1
  else:
    check_month = jalali_month+1
    check_year = jalali_year
  days_number = (jdatetime.date(check_year, check_month, 1) - jdatetime.date(jalali_year, jalali_month, 1)).days
  df = {"Mo":[], "Tu":[], "We":[], "Th":[], "Fr":[], "Sa":[], "Su":[]}
  first_weekname = jdatetime.date(check_year, check_month, 1).strftime("%a")[:2]
  for key in df:
    if key != first_weekname:
      df[key].append(None)
    else:
      df[key].append("1")
  for i in range(2, days_number+1):
    day_name = jdatetime.date(jalali_year,jalali_month,i).strftime("%a")
    df[day_name[:2]].append(str(i))
  return pd.DataFrame(df)

If you tend to get Esfand's (the 12th month of the Persian/Jalali year) days in the format of a dataframe, you can call get_jalali_calander(1400, 12). The result would be exactly as what follows:

Mo Tu We Th Fr Sa Su
0 1
1 2 3 4 5 6 7 8
2 9 10 11 12 13 14 15
3 16 17 18 19 20 21 22
4 23 24 25 26 27 28 29

Upvotes: 1

Related Questions