The Jalali, Hijri and Gregorian calendars: conversion, day arithmetic, month names and time zones.
import calendar
This package works with three calendars: Jalali (Solar Hijri, the official calendar of Iran and Afghanistan), Hijri (lunar) and Gregorian. A date is a plain struct of year, month and day, and each calendar has its own conversion, validation, day arithmetic and month name functions.
The Hijri calendar here is arithmetic: months alternate between 30 and 29 days and are not adjusted by sighting the moon, so a date can be one day off from the official calendar of a given country.
There are three ways to turn an instant into a date: EpochToJalali and its siblings use the machine local time, WallClockJalali takes a fixed time zone such as Tehran, and LoadZone covers zones that observe daylight saving.
Use it to show dates the way people in Iran, Afghanistan or the Arab world read them, to take a Jalali date from a form, and to turn a meeting time in one city into the time in another. Instants, durations and the clock itself are in time; store instants there and convert to a calendar only for display. For Persian digits in the output, see i18n.
The things people get wrong:
•
Taking today's date on a server with TodayJalali or EpochToJalali. Those use the machine's zone, and a server set to UTC is still on yesterday until 03:30 in Tehran. Use WallClockJalali(time.Now(), TZTehran()).
•
Mixing the two weekday numberings. JalaliWeekday takes a Julian day number and counts from Saturday as 0; Weekday, WeekdayJalali and the other Weekday... functions count from Sunday as 0.
•
Converting unchecked input. The conversions do not validate, so 31 Esfand or month 13 gives a wrong date instead of an error. Check with IsValidJalaliDate and its siblings first.
•
Expecting the Hijri dates to match an official calendar to the day, or the fixed TZLondon and TZNewYork zones to follow daylight saving. They do not; use LoadZone for that.
The Jalali conversion is the standard arithmetic one (as in the jalaali libraries), which works for the years -61 to 3177 and agrees with the official Iranian calendar for the years in use today.
Birthdays and anniversaries in Jalali
Dates are plain structs, so date logic is ordinary code: build the date in the current year, compare it with today using CompareJalali, and move to the next year if it has passed. DaysBetweenJalali counts the days in between, and JalaliToGregorian gives the same day in the other calendar.
calendar-guide-birthday.salam
import calendar
funcnext_birthday(born: calendar.JalaliDate, today: calendar.JalaliDate): calendar.JalaliDate:
mut target_year := today.year
mut target_day := born.day
if born.month == 12and target_day == 30andnot calendar.IsLeapJalali(target_year):
target_day = 29endmut next := calendar.JalaliDate { year = target_year, month = born.month, day = target_day }
if calendar.CompareJalali(next, today) < 0:
target_year++
target_day = born.day
if born.month == 12and target_day == 30andnot calendar.IsLeapJalali(target_year):
target_day = 29end
next = calendar.JalaliDate { year = target_year, month = born.month, day = target_day }
endret next
endfuncmain:
today := calendar.JalaliDate { year = 1405, month = 7, day = 25 }
born := calendar.JalaliDate { year = 1370, month = 9, day = 3 }
mut age := today.year - born.year
birthday_now := calendar.JalaliDate { year = today.year, month = born.month, day = born.day }
if calendar.CompareJalali(birthday_now, today) > 0:
age--
end
next := next_birthday(born, today)
println"age:", age
println"next birthday:",
calendar.FormatJalali(next),
"(" + calendar.JalaliMonthNameEn(next.month) + ")"println"days to go:", calendar.DaysBetweenJalali(today, next)
println"in the Gregorian calendar:", calendar.FormatGregorian(calendar.JalaliToGregorian(next))
leap := calendar.JalaliDate { year = 1403, month = 12, day = 30 }
println"born on 30 Esfand 1403, celebrates in 1405 on:",
calendar.FormatJalali(next_birthday(leap, today))
end
Output
age: 34
next birthday: 1405-09-03 (Azar)
days to go: 38
in the Gregorian calendar: 2026-11-24
born on 30 Esfand 1403, celebrates in 1405 on: 1405-12-29
One case needs care: 30 Esfand exists only in leap years. Before building a date in another year, check it with IsLeapJalali or IsValidJalaliDate; the example moves such a birthday to 29 Esfand. AddYearsJalali and AddMonthsJalali do the same clamping for you when you only need to move a date.
Drawing a month
A month view needs two numbers: the weekday of the first day and the length of the month. JalaliWeekday numbers the days from Saturday as 0, which is how Persian calendars are laid out, and DaysInJalaliMonth gives the length.
calendar-guide-month.salam
import calendar
importstrfuncmain:
shown_year := 1405
shown_month := 7
first := calendar.JalaliDate { year = shown_year, month = shown_month, day = 1 }
println calendar.JalaliMonthNameEn(shown_month), shown_year
println"Sa Su Mo Tu We Th Fr"
offset := calendar.JalaliWeekday(calendar.JalaliToJDN(first))
mut line := ""repeat offset:
line += " "end
days := calendar.DaysInJalaliMonth(shown_year, shown_month)
repeat1to days in d:
if d < 10:
line += " "end
line += str.FromInt(d)
if (offset + d) % 7 == 0:
println line
line = ""else:
line += " "endendif line != "":
println line.Trim()
endprintln"starts on", calendar.FormatGregorianLongEn(calendar.JalaliToGregorian(first))
end
Output
Mehr 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
starts on September 23, 2026
For a week that starts on Sunday or Monday, use WeekdayJalali or WeekdayGregorian instead, which number from Sunday as 0, and shift by the day you want first.
A Hijri month in the other calendars
For Ramadan or Muharram you usually want the Hijri dates translated into Gregorian and Jalali ones. DaysInHijriMonth gives the length of the month and AddDaysHijri walks from its first day to its last.
calendar-guide-ramadan.salam
import calendar
funcmain:
start := calendar.HijriDate { year = 1448, month = 9, day = 1 }
days := calendar.DaysInHijriMonth(start.year, start.month)
last := calendar.AddDaysHijri(start, days - 1)
eid := calendar.AddDaysHijri(last, 1)
println calendar.HijriMonthNameEn(start.month), start.year, "has", days, "days"println"first day:",
calendar.FormatGregorian(calendar.HijriToGregorian(start)),
"=",
calendar.FormatJalali(calendar.HijriToJalali(start))
println"last day: ",
calendar.FormatGregorian(calendar.HijriToGregorian(last)),
"=",
calendar.FormatJalali(calendar.HijriToJalali(last))
println"Eid: ",
calendar.FormatGregorian(calendar.HijriToGregorian(eid)),
calendar.WeekdayNameEn(calendar.WeekdayHijri(eid))
println"in Arabic:", calendar.FormatHijriLongAr(eid)
end
Output
Ramadan 1448 has 30 days
first day: 2027-02-08 = 1405-11-19
last day: 2027-03-09 = 1405-12-18
Eid: 2027-03-10 Wednesday
in Arabic: 1 شوال 1448
Remember that this is the arithmetic Hijri calendar. The official start of a month depends on sighting the moon and can be a day earlier or later, and differs between countries, so show these dates as an estimate.
One meeting, several cities
To schedule across time zones, turn the local date and time in one zone into an instant with ZonedToEpoch, then show that instant in each other zone. LoadZone reads the system's time zone database, so daylight saving is handled: London is on GMT on 25 March 2026 and on BST a week later, while New York had already moved to EDT.
calendar-guide-meeting.salam
import calendar
funcshow(label: str, epoch: i64, zone_name: str):
mut z := calendar.LoadZone(zone_name)
tz := calendar.TimeZoneAt(z, epoch)
d := calendar.WallClockGregorian(epoch, tz)
t := calendar.WallClockTime(epoch, tz)
println label,
calendar.FormatGregorian(d),
calendar.FormatClockTime(t),
calendar.ZoneAbbrevAt(z, epoch)
calendar.FreeZone(z)
endfuncmain:
mut tehran := calendar.LoadZone("Asia/Tehran")
meeting_day := calendar.GregorianDate { year = 2026, month = 3, day = 25 }
at := calendar.ClockTime { hour = 18, minute = 30 }
meeting := calendar.ZonedToEpoch(meeting_day, at, tehran)
calendar.FreeZone(tehran)
show("Tehran: ", meeting, "Asia/Tehran")
show("London: ", meeting, "Europe/London")
show("New York:", meeting, "America/New_York")
show("Tokyo: ", meeting, "Asia/Tokyo")
show("one week later in London:", meeting + 7 * 86400, "Europe/London")
end
Output
Tehran: 2026-03-25 18:30:00 +0330
London: 2026-03-25 15:00:00 GMT
New York: 2026-03-25 11:00:00 EDT
Tokyo: 2026-03-26 00:00:00 JST
one week later in London: 2026-04-01 16:00:00 BST
LoadZone needs the zone files that Linux and macOS keep in /usr/share/zoneinfo; check ok on the result. For a zone with a fixed offset, such as Tehran since Iran stopped daylight saving, TZTehran() or FixedOffsetZone with WallClockJalali needs no files at all. Free a loaded zone with FreeZone when you are done with it.
Which function for which job
Job
Use
Convert between calendars
GregorianToJalali, JalaliToGregorian, HijriToJalali and the rest
Today's date
TodayJalali, TodayGregorian, TodayHijri
A date from a stored instant
WallClockJalali(epoch, TZTehran()), or EpochToJalali for local time