jalaali

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2025 License: ISC Imports: 8 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Jalaali Library

GitHub Tag Go Reference License Go Report Card Contributors Issues

The Jalaali library provides an interface and implementation for manipulating Jalaali (Persian) calendar dates and times. It supports standard Go time package formats and includes functions for setting and getting various components of Jalaali dates and times, as well as converting between Jalaali and Gregorian dates. The library also includes utility functions for working with time zones specific to Tehran and Kabul.

Features

  • Full support for Jalaali (Persian) calendar.
  • Conversion between Jalaali and Gregorian dates.
  • Utility functions for date and time manipulation.
  • Support for custom time zones (e.g., Tehran and Kabul).
  • Parsing and formatting Jalaali dates using Go's time package layout.

Installation

To install the library, use the following command:

go get github.com/go-universal/jalaali

API Documentation

Constructors
New
New(t time.Time) Jalaali

Creates a new Jalaali instance from a time.Time object. If the year is less than 1097, it returns a zero Jalaali instance.

jalaaliDate := jalaali.New(time.Now())
fmt.Println(jalaaliDate.String()) // Outputs the current Jalaali date in RFC3339 format.
Date
Date(year int, month Month, day, hour, min, sec, nsec int, loc *time.Location) Jalaali

Creates a new Jalaali instance from the specified Jalaali date and time components.

jalaaliDate := jalaali.Date(1403, jalaali.Farvardin, 15, 20, 14, 0, 0, jalaali.TehranTz())
fmt.Println(jalaaliDate.String()) // Outputs: "1403-01-15T20:14:00+03:30"
Unix
Unix(sec, nsec int64) Jalaali

Creates a new Jalaali instance from a Unix timestamp.

jalaaliDate := jalaali.Unix(1672531200, 0)
fmt.Println(jalaaliDate.String()) // Outputs the Jalaali date corresponding to the Unix timestamp.
Now
Now() Jalaali

Creates a new Jalaali instance representing the current time.

jalaaliDate := jalaali.Now()
fmt.Println(jalaaliDate.String()) // Outputs the current Jalaali date and time.
TehranTz
TehranTz() *time.Location

Returns the Tehran time zone (UTC+03:30).

KabulTz
KabulTz() *time.Location

Returns the Kabul time zone (UTC+04:30).

Date and Time Manipulation
Add(d time.Duration) Jalaali

Adds a duration to the Jalaali instance and returns a new instance.

AddDate(year, month, day int) Jalaali

Adds the specified number of years, months, and days to the Jalaali instance.

AddTime(hour, min, sec, nsec int) Jalaali

Adds the specified time components to the Jalaali instance.

Yesterday() Jalaali

Returns a new instance representing the previous day.

Tomorrow() Jalaali

Returns a new instance representing the next day.

BeginningOfDay() Jalaali

Returns a new instance representing the start of the day (00:00:00).

EndOfDay() Jalaali

Returns a new instance representing the end of the day (23:59:59.999999999).

BeginningOfMonth() Jalaali

Returns a new instance representing the first day of the month.

EndOfMonth() Jalaali

Returns a new instance representing the last day of the month.

BeginningOfYear() Jalaali

Returns a new instance representing the first day of the year.

EndOfYear() Jalaali

Returns a new instance representing the last day of the year.

Date and Time Components
Year() int

Returns the year of the Jalaali instance.

Month() Month

Returns the month of the Jalaali instance.

Day() int

Returns the day of the Jalaali instance.

Hour() int

Returns the hour of the Jalaali instance.

Minute() int

Returns the minute of the Jalaali instance.

Second() int

Returns the second of the Jalaali instance.

Nanosecond() int

Returns the nanosecond of the Jalaali instance.

Weekday() Weekday

Returns the weekday of the Jalaali instance.

IsLeap() bool

Returns whether the year of the Jalaali instance is a leap year.

Formatting and Parsing
Format(layout string) string

Formats the Jalaali date using Go's time package layout.

jalaaliDate := jalaali.Date(1403, jalaali.Farvardin, 15, 20, 14, 0, 0, jalaali.TehranTz())
formatted := jalaaliDate.Format("2006-01-02 15:04:05")
fmt.Println(formatted) // Outputs: "1403-01-15 20:14:00"
Parse(layout, datetime string) (Jalaali, error)

Parses a Jalaali date from a string using the specified layout.

jalaaliDate, err := jalaali.Parse("2006-01-02", "1403-01-15")
if err != nil {
    fmt.Println("Error parsing date:", err)
} else {
    fmt.Println(jalaaliDate.String()) // Outputs: "1403-01-15T00:00:00+03:30"
}
Time Zones
Zone() (string, int)

Returns the time zone name and offset in seconds.

In(loc *time.Location) Jalaali

Sets the location of the Jalaali instance and returns a new instance.

Examples

Create a Jalaali Date
date := jalaali.Date(1403, jalaali.Ordibehesht, 10, 14, 30, 0, 0, jalaali.TehranTz())
fmt.Println(date.String()) // Outputs: "1403-02-10T14:30:00+03:30"
Add Time to a Jalaali Date
date := jalaali.Date(1403, jalaali.Farvardin, 15, 20, 14, 0, 0, jalaali.TehranTz())
newDate := date.AddDate(1, 1, 1)
fmt.Println(newDate.String()) // Outputs: "1404-02-16T20:14:00+03:30"
Format a Jalaali Date
date := jalaali.Date(1403, jalaali.Farvardin, 15, 20, 14, 0, 0, jalaali.TehranTz())
formatted := date.Format("Monday, 02 January 2006")
fmt.Println(formatted) // Outputs: "شنبه, 15 فروردین 1403"
Parse a Jalaali Date
parsedDate, err := jalaali.Parse("2006-01-02", "1403-01-15")
if err != nil {
    fmt.Println("Error:", err)
} else {
    fmt.Println(parsedDate.String()) // Outputs: "1403-01-15T00:00:00+03:30"
}

License

This library is licensed under the ISC License. See the LICENSE file for details.

Documentation

Overview

Package gojalaali provides an interface and implementation for manipulating Jalaali (Persian) calendar dates and times. It supports standard Go time package formats and includes functions for setting and getting various components of Jalaali dates and times, as well as converting between Jalaali and Gregorian dates. The package also includes utility functions for working with time zones specific to Tehran and Kabul.

Conversion inspired from github.com/yaa110/go-persian-calendar library.

This package jalaali conversion inspired from github.com/yaa110/go-persian-calendar library.

This package jalaali conversion inspired from github.com/yaa110/go-persian-calendar library.

This package jalaali conversion inspired from github.com/yaa110/go-persian-calendar library.

This package jalaali conversion inspired from github.com/yaa110/go-persian-calendar library.

Copyright (c) 2016 Navid Fathollahzade

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func KabulTz

func KabulTz() *time.Location

KabulTz get kabul time zone.

func TehranTz

func TehranTz() *time.Location

TehranTz get tehran time zone.

Types

type AmPm

type AmPm int

A AmPm specifies the 12-Hour marker.

const (
	Am AmPm = 0 + iota
	Pm
)

List of 12-Hour markers.

func (AmPm) Short

func (a AmPm) Short() string

Short returns the Persian short name of 12-Hour marker.

func (AmPm) String

func (a AmPm) String() string

String returns the Persian name of 12-Hour marker.

type DayTime

type DayTime int

A DayTime represents a part of the day based on hour.

const (
	Midnight DayTime = iota
	Dawn
	Morning
	BeforeNoon
	Noon
	AfterNoon
	Evening
	Night
)

List of day times.

func (DayTime) String

func (d DayTime) String() string

String returns the Persian name of day time.

type Jalaali

type Jalaali interface {
	// IsZero returns true if jalaali is zero time instance.
	IsZero() bool

	// IsLeap returns true if the year of t is a leap year.
	IsLeap() bool

	// Since returns the number of seconds between t and t2.
	Since(t2 Jalaali) time.Duration

	// AmPm returns the 12-Hour marker of instance.
	AmPm() AmPm

	// Zone returns the zone name and its offset
	// in seconds east of UTC of instance.
	Zone() (string, int)

	// In sets the location of jalaali date and returns a new instance.
	// If nil loc passed this method returns same instance.
	In(loc *time.Location) Jalaali

	// Add add duration amount to Jalaali and returns a new instance.
	Add(d time.Duration) Jalaali

	// AddTime add hour, minute, second and nanoseconds
	// to Jalaali and returns a new instance.
	AddTime(hour, min, sec, nsec int) Jalaali

	// AddDate add year, month and day
	// to Jalaali and returns a new instance.
	AddDate(year, month, day int) Jalaali

	// AddDatetime add the year, month, day,
	// hour, minute, second and nanoscond to Jalaali
	// and returns a new instance..
	AddDatetime(year, month, day, hour, min, sec, nsec int) Jalaali

	// Yesterday returns a new instance of Jalaali
	// representing a day before the day of instance.
	Yesterday() Jalaali

	// Tomorrow returns a new instance of Jalaali
	// representing a day after the day of instance.
	Tomorrow() Jalaali

	// BeginningOfDay returns a new instance of Jalaali
	// representing the 00:00:00.000000000 time of today.
	BeginningOfDay() Jalaali

	// EndOfDay returns a new instance of Jalaali
	// representing the 23:59:59.999999999 time of today.
	EndOfDay() Jalaali

	// FirstWeekDay returns a new instance of Jalaali
	// representing the first day of the week of instance.
	FirstWeekDay() Jalaali

	// LastWeekDay returns a new instance of Jalaali
	// representing the last day of the week of instance.
	LastWeekDay() Jalaali

	// BeginningOfWeek returns a new instance of Jalaali
	// representing the first day of the week of instance
	// and time is set to 00:00:00.000000000.
	BeginningOfWeek() Jalaali

	// EndOfWeek returns a new instance of Jalaali
	// representing the last day of the week of instance
	// and time is set to 23:59:59.999999999.
	EndOfWeek() Jalaali

	// FirstMonthDay returns a new instance of Jalaali
	// representing the first day of the month of instance.
	FirstMonthDay() Jalaali

	// LastMonthDay returns a new instance of Jalaali
	// representing the last day of the month of instance.
	LastMonthDay() Jalaali

	// BeginningOfMonth returns a new instance of Jalaali
	// representing the first day of the month of instance
	// and time is set to 00:00:00.000000000.
	BeginningOfMonth() Jalaali

	// EndOfMonth returns a new instance of Jalaali
	// representing the first day of the month of instance
	// and time is set to 23:59:59.999999999.
	EndOfMonth() Jalaali

	// FirstYearDay returns a new instance of Jalaali
	// representing the first day of the year of instance.
	FirstYearDay() Jalaali

	// LastYearDay returns a new instance of Jalaali
	// representing the last day of the year of instance.
	LastYearDay() Jalaali

	// BeginningOfYear returns a new instance of Jalaali
	// representing the first day of the year of instance
	// and time is set to 00:00:00.000000000.
	BeginningOfYear() Jalaali

	// EndOfYear returns a new instance of Jalaali
	// representing the first day of the year of instance
	// and time is set to 23:59:59.999999999.
	EndOfYear() Jalaali

	// SetYear sets the year of the instance.
	SetYear(year int)

	// SetMonth sets the month of the instance.
	SetMonth(month Month)

	// SetDay sets the day of the instance.
	SetDay(day int)

	// SetHour sets the hour offset of the Jalaali time.
	SetHour(hour int)

	// SetMinute sets the minute offset of the Jalaali time.
	SetMinute(min int)

	// SetSecond sets the second offset of the Jalaali time.
	SetSecond(sec int)

	// SetNanosecond sets the nanosecond offset of the Jalaali time.
	SetNanosecond(nsec int)

	// SetTime sets the hour, minute, second and nanosecond of the Jalaali time.
	// Pass -1 to ignore parameter.
	SetTime(hour, min, sec, nsec int)

	// SetDate sets the year, month, and day of the Jalaali date.
	// Pass -1 to ignore parameter.
	SetDate(year, month, day int)

	// SetDateTime sets the year, month, day,
	//  hour, minute, and second of the Jalaali date and time.
	// Pass -1 to ignore parameter.
	SetDateTime(year, month, day, hour, min, sec, nsec int)

	// Year returns the year of t.
	Year() int

	// YearDay returns the day of year of instance.
	YearDay() int

	// YearRemainDays returns the number of remaining days of the year of instance.
	YearRemainDays() int

	// Month returns the month of t in the range [1, 12].
	Month() Month

	// Weekday returns the weekday of instance.
	Weekday() Weekday

	// MonthWeek returns the week of month of instance.
	MonthWeek() int

	// YearWeek returns the week of year of instance.
	YearWeek() int

	// YearRemainWeeks returns the number of remaining weeks of the year of instance.
	YearRemainWeeks() int

	// Day returns the day of month of t.
	Day() int

	// MonthRemainDays returns the number of remaining days of the month of instance.
	MonthRemainDays() int

	// Hour returns the hour of t in the range [0, 23].
	Hour() int

	// Hour12 returns the hour of t in the range [0, 11].
	Hour12() int

	// Minute returns the minute offset of t in the range [0, 59].
	Minute() int

	// Second returns the seconds offset of t in the range [0, 59].
	Second() int

	// Nanosecond returns the nanoseconds offset of t in the range [0, 999999999].
	Nanosecond() int

	// DayTime returns the dayTime of that part of the day.
	// [0,3]   -> Midnight
	// [3,6]   -> Dawn
	// [6,9]   -> Morning
	// [9,12]  -> BeforeNoon
	// [12,15] -> Noon
	// [15,18] -> AfterNoon
	// [18,21] -> Evening
	// [21,24] -> Night
	DayTime() DayTime

	// Location returns a pointer to time.Location of instance.
	Location() *time.Location

	// Date returns the year, month, day of instance.
	Date() (int, Month, int)

	// Clock returns the hour, minute, seconds offsets of instance.
	Clock() (int, int, int)

	// Unix returns the number of seconds since January 1, 1970 UTC.
	Unix() int64

	// UnixNano seturns the number of nanoseconds since January 1, 1970 UTC.
	UnixNano() int64

	// Time converts the Shamsi (Solar Hijri) to Gregorian
	// and returns it as a Go time.Time object.
	Time() time.Time

	// String returns t in RFC3339 format.
	String() string

	// TimeFormat formats in standard time package layout.
	//
	// Year
	// 2006				Four-digit year								"1403"
	// 06				Two-digit year 								"03"
	//
	// Month
	// January			Full month name								"اسفند"
	// Jan				Three-letter abbreviation of the month		"اسف"
	// 01				Two-digit month with a leading 0			"07"
	// 1				One-digit month								"7"
	//
	// Day
	// 02				Two-digit month day with a leading 0		"08"
	// _2				Two-digit month day with a leading space	" 9"
	// 2				One-digit month day							"3"
	//
	// Weekday
	// Monday			Full weekday name							"شنبه"
	// Mon				abbreviation of the weekday					"ش"
	//
	// Hour
	// 15				Two-digit 24 hour format					"15"
	// 03				Two-digit 12 hour format					"03"
	// 3				One-digit 12 hour format					"9"
	//
	// Minute
	// 04				Two-digit minute with leading 0				"03"
	// 4				One-digit minute							"3"
	//
	// Second
	// 05				Two-digit second with leading 0				"09"
	// 5				One-digit second							"2"
	//
	// Milliseconds
	// .000				Millisecond									".120"
	// .000000			Microsecond									".123400"
	// .000000000		Nanosecond									".123456000"
	// .999				Trailing zeros removed millisecond			".12"
	// .999999			Trailing zeros removed microsecond			".1234"
	// .999999999		Trailing zeros removed nanosecond			".123456"
	//
	// Daytime
	// Morning			day time									"صبح"
	// PM				Full 12-Hour marker							"قبل از ظهر"
	// pm				Short 12-Hour marker						"ق.ظ"
	//
	// Timezone
	// MST				Abbreviation of the time zone				"UTC"
	// Z070000			zone offset	Hour, Minute and second			"Z" or "+033000"
	// Z0700			zone offset Hour and Minute					"Z" or "+0330"
	// Z07:00:00		zone offset	Hour, Minute and second			"Z" or "+03:30:00"
	// Z07:00			zone offset Hour and Minute					"Z" or "+03:30"
	// Z07				zone offset Hour							"Z" or "+03"
	// -070000			zone offset	Hour, Minute and second			"+033000"
	// -0700			zone offset Hour and Minute					"+0330"
	// -07:00:00		zone offset Hour, Minute and second			"+03:30:00"
	// -07:00			zone offset Hour and Minute					"+03:30"
	// -07				zone offset Hour							"+03"
	Format(layout string) string
}

Jalaali represents an interface for manipulating Jalaali (Persian) calendar dates and times. It support standard go time package formats.

func Date

func Date(year int, month Month, day, hour, min, sec, nsec int, loc *time.Location) Jalaali

Date create a new jalaali instance from jalaali date.

year, month and day represent a day in Persian calendar.

hour, min minute, sec seconds, nsec nanoseconds offsets represent a moment in time.

loc is a pointer to time.Location, if loc is nil then the local time is used.

func New

func New(t time.Time) Jalaali

New create new jalaali instance from time.

func NewTz added in v0.0.2

func NewTz(t time.Time, loc *time.Location) Jalaali

NewTz create new jalaali instance from time. If location is nil then the local time is used.

func Now

func Now() Jalaali

Now create a new jalaali instance from current time.

func Parse

func Parse(layout, datetime string) (Jalaali, error)

Parse parse jalaali datetime from string with layout.www It returns a Jalaali instance and an error if the parsing fails.

func Unix

func Unix(sec, nsec int64) Jalaali

Unix create a new jalaali instance from unix timestamp.

sec seconds and nsec nanoseconds since January 1, 1970 UTC.

type Month

type Month int

A Month specifies a month of the year starting from Farvardin = 1.

const (
	Farvardin Month = 1 + iota
	Ordibehesht
	Khordad
	Tir
	Mordad
	Shahrivar
	Mehr
	Aban
	Azar
	Dey
	Bahman
	Esfand
)

List of months in Persian calendar.

const (
	Hamal Month = 1 + iota
	Sur
	Jauza
	Saratan
	Asad
	Sonboleh
	Mizan
	Aqrab
	Qos
	Jady
	Dolv
	Hut
)

List of Dari months in Persian calendar.

func (Month) Dari

func (m Month) Dari() string

Dari returns the Dari name of the month.

func (Month) DariShort

func (m Month) DariShort() string

Dari returns the Dari short name of the month.

func (Month) Short

func (m Month) Short() string

String returns the Persian short name of the month.

func (Month) String

func (m Month) String() string

String returns the Persian name of the month.

type Weekday

type Weekday int

A Weekday specifies a day of the week starting from Shanbe = 0.

const (
	Shanbeh Weekday = iota
	Yekshanbeh
	Doshanbeh
	Seshanbeh
	Charshanbeh
	Panjshanbeh
	Jomeh
)

List of days in a week.

func JWeekday

func JWeekday(wd time.Weekday) Weekday

JWeekday get weekday from time.Weekday.

func (Weekday) Short

func (d Weekday) Short() string

Short returns the Persian short name of the day in week.

func (Weekday) String

func (d Weekday) String() string

String returns the Persian name of the day in week.

func (Weekday) Weekday

func (d Weekday) Weekday() time.Weekday

Weekday get time.Weekday.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址