Skip to content

Add static calendar() method to jdatetime.date for displaying monthly calendar#177

Open
Peyman20122 wants to merge 28 commits into
slashmili:mainfrom
Peyman20122:main
Open

Add static calendar() method to jdatetime.date for displaying monthly calendar#177
Peyman20122 wants to merge 28 commits into
slashmili:mainfrom
Peyman20122:main

Conversation

@Peyman20122

@Peyman20122 Peyman20122 commented May 10, 2026

Copy link
Copy Markdown

Hello dear engineer

Summary

This PR adds a new static method calendar() to the jdatetime.date class. It displays a monthly calendar (week-based table) for any given Shamsi (Jalali) year and month, similar to the Unix cal command or Python's calendar module.

Motivation

Currently, jdatetime provides date conversion and formatting but lacks a built‑in way to visualize a whole month as a weekly grid. This feature is useful for many Persian applications (CLI tools, scheduling widgets, etc.) and reduces boilerplate code for users.

Changes

  • Added @staticmethod calendar(year=None, month=None, locale=None) to jdatetime.date.
  • If year and month are omitted, it shows the current month.
  • The method respects the thread‑local locale (Persian/English) for month names and weekday headers.
  • Returns a multi‑line string (not printed directly) for flexibility.
  • Handles leap years correctly (Esfand can have 30 days).
  • Input validation (year range, month range) similar to the constructor.

Example usage

import jdatetime

# Current month calendar
print(jdatetime.date.calendar())

# Specific month (English)
print(jdatetime.date.calendar(1405, 2, locale='en_US'))

# Persian calendar
jdatetime.set_locale('fa_IR')
print(jdatetime.date.calendar(1405, 2))

Output example (English)

   Ordibehesht 1405
Sa Su Mo Tu We Th Fr
    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

Additional notes

· The weekday headers assume Saturday = 0 (week starts on Saturday), consistent with jdatetime.date.weekday().
· The method does not depend on external libraries (e.g., numpy, hijridate).
· I have tested the method with various inputs and locales.

Peyman20122 and others added 4 commits May 10, 2026 16:37
feat: add static calender method to jdatetime.date class
The problem with the code was that I used unittest.main() but did not import unittest.
@Peyman20122

Copy link
Copy Markdown
Author

Hello, good day, dear engineer
The problem with the code was that I used unittest.main() but did not import unittest.

@slashmili

Copy link
Copy Markdown
Owner

Hi @Peyman20122

Thanks for your work. Before looking into the PR, I want to mention that this module strives to provide interfaces similar to those provided by the standard library datetime provides. I only found a module named Calendar

My suggestion is that you work on jcalendar module in a separate repo and I'll be happy to support you on that!

@Peyman20122

Copy link
Copy Markdown
Author

Hello dear engineer @slashmili

Thank you for your support

I told you the truth, maybe the user wants the calendar to be inside the jdatetime library, that's why I made the pull request

I also wrote a repository for the calendar, the link to which you can see below, it has both the Jalali calendar and the calendar with the moon in Scorpio
Thank you for your opinion about it

https://github.com/Peyman20122/Jalali-Calendar-library

@slashmili

Copy link
Copy Markdown
Owner

@Peyman20122 I'd rather follow the Unix philosophy, especially for such small libraries.

One more piece of advice: there are many Shamsi date conversion libraries in Python. The reason jdatetime has gained traction is that it serves as a drop-in replacement for Python's standard datetime library. I'd suggest taking the same approach and releasing a small jcalendar library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants