package ptime

Go Persian Calendar

A complete, dependency-free Persian (Solar Hijri / Jalali) calendar for Go — shaped like the standard time package.

$ go get github.com/amiranmanesh/go-persian-calendar

What you get

Everything the standard time package gives you, in the Persian calendar.

Familiar API

Now, Date, Add, AddDate, Sub, Before, After, Compare, Truncate, Round — same names, same semantics.

Two layout languages

Pattern letters (yyyy/MM/dd) and the Go reference time (2006/01/02). Pick whichever your team already reads.

Parsing, not just formatting

Parse, ParseInLocation and their reference-time twins, with a ParseError that names the element that failed.

JSON and SQL ready

Implements json.Marshaler, encoding.TextMarshaler, driver.Valuer and sql.Scanner. No wrapper type needed.

Iranian and Dari names

Dari month names are selected automatically in Asia/Kabul, or explicitly with the MMI pattern.

Exact conversions

Every conversion goes through the Julian Day Number, so it stays correct on both sides of the 1582 Gregorian reform.

Public holidays

An optional holiday package answers whether a date is a day off — and says so when a lunar date is still an estimate.

Public holidays, honestly

Iran fixes lunar holidays by moon sighting. No algorithm can predict that, so the package does not pretend otherwise.

import "github.com/amiranmanesh/go-persian-calendar/holiday"

cal := holiday.Iran()

cal.IsHoliday(pt)      // is this a day off, Fridays included?
cal.Lookup(pt).Title() // روز طبیعت، سیزده به‌در
cal.NextWorkday(pt)    // the next working day
cal.Workdays(from, to) // working days in a range

Solar — always exact

Nowruz, 22 Bahman, 13 Farvardin. Fixed by law, expressed as rules in code, correct forever. Reported as Confirmed.

Lunar — flagged until settled

Eid al-Fitr, Ashura, Arbaeen. The arithmetic calendar disagrees with the announced date 43% of the time, so an unsettled year is reported as Estimated.

One-off — data only

An air pollution closure or an election cannot be predicted at all. A monthly workflow reconciles them and opens a pull request for review.

The data is embedded at build time, so the package does no I/O. The same file is served at /data/v1/iran.json for runtime reloads and non-Go consumers.

In practice

A few lines each, straight from the test suite.

import ptime "github.com/amiranmanesh/go-persian-calendar"

// Gregorian to Persian
gt := time.Date(2016, time.January, 1, 12, 1, 1, 0, ptime.Iran())
pt := ptime.New(gt)
fmt.Println(pt.Date()) // 1394 دی 11

// Persian to Gregorian
pt = ptime.Date(1394, ptime.Mehr, 2, 12, 59, 59, 0, ptime.Iran())
fmt.Println(pt.Time().Format(time.DateOnly)) // 2015-09-24

Predefined layouts

Constants for the shapes you reach for most.

ConstantLayoutExample
RFC3339yyyy-MM-ddTHH:mm:ssZ1394-07-02T12:59:59+03:30
RFC3339Nanoyyyy-MM-ddTHH:mm:ss.999999999Z1394-07-02T12:59:59.052+03:30
DateTimeyyyy-MM-dd HH:mm:ss1394-07-02 12:59:59
DateOnlyyyyy-MM-dd1394-07-02
TimeOnlyHH:mm:ss12:59:59
Kitchenh:mm a12:59 ب.ظ
LongDateE d MMM yyyyپنج‌شنبه 2 مهر 1394

The full pattern-letter table lives in the API reference.

به فارسی

پکیج ptime تبدیل بین تقویم هجری شمسی و میلادی را انجام می‌دهد و نوع Time را در اختیار شما می‌گذارد که مانند time.Time رفتار می‌کند: همان نام متدها، همان معنای مقداری و همان روش قالب‌بندی. قالب‌بندی، تجزیه، JSON و SQL همگی پشتیبانی می‌شوند و نام ماه‌ها هم به شکل ایرانی و هم دری در دسترس است.