Documentation
¶
Overview ¶
Package ptime provides a complete implementation of the Persian (Solar Hijri / Jalali) calendar, designed to mirror the standard library time package as closely as possible.
Overview ¶
The central type is Time, an immutable value describing a moment in the Persian calendar together with its time.Location. Conversions between the Persian and Gregorian calendars go through the Julian Day Number, which keeps them exact for both the Julian and the Gregorian era.
pt := ptime.Date(1394, ptime.Mehr, 2, 12, 59, 59, 0, ptime.Iran()) fmt.Println(pt.Time().Format(time.DateOnly)) // 2015-09-24
Creating a Time ¶
Use Now for the current moment, Date to build one from Persian calendar components, New to convert an existing time.Time, Unix to convert a Unix timestamp, or Parse to read one from text.
Formatting and parsing ¶
Two layout languages are supported:
- The ptime layout language used by Time.Format and Parse, built from repeated pattern letters such as "yyyy/MM/dd HH:mm:ss".
- The standard library reference-time language used by Time.TimeFormat and ParseTimeFormat, such as "2006/01/02 15:04:05".
Predefined layouts such as RFC3339, DateTime and DateOnly cover the common cases.
Encoding ¶
Time implements encoding.TextMarshaler, encoding.TextUnmarshaler, [json.Marshaler], [json.Unmarshaler], driver.Valuer and [sql.Scanner], so it can be stored in JSON documents and SQL databases without a wrapper type.
Localization ¶
Both Iranian and Dari month names are available. Time.TimeFormat picks the Dari names automatically when the location is Afghanistan.
Limitations ¶
Gregorian years below 1097 are not representable; New returns the zero Time for them.
Index ¶
- Constants
- Variables
- func Afghanistan() *time.Location
- func Iran() *time.Location
- type AmPm
- type DayTime
- type Month
- type ParseError
- type Time
- func Date(year int, month Month, day, hour, minute, sec, nsec int, loc *time.Location) Time
- func New(t time.Time) Time
- func Now() Time
- func Parse(layout, value string) (Time, error)
- func ParseInLocation(layout, value string, loc *time.Location) (Time, error)
- func ParseTimeFormat(layout, value string) (Time, error)
- func ParseTimeFormatInLocation(layout, value string, loc *time.Location) (Time, error)
- func Unix(sec, nsec int64) Time
- func UnixMicro(usec int64) Time
- func UnixMilli(msec int64) Time
- func (t Time) Add(d time.Duration) Time
- func (t Time) AddDate(years, months, days int) Time
- func (t Time) After(u Time) bool
- func (t Time) AmPm() AmPm
- func (t Time) AppendFormat(b []byte, layout string) []byte
- func (t Time) AppendTimeFormat(b []byte, layout string) []byte
- func (t *Time) At(hour, minute, sec, nsec int)
- func (t Time) Before(u Time) bool
- func (t Time) BeginningOfMonth() Time
- func (t Time) BeginningOfWeek() Time
- func (t Time) BeginningOfYear() Time
- func (t Time) Clock() (int, int, int)
- func (t Time) Compare(u Time) int
- func (t Time) Date() (int, Month, int)
- func (t Time) Day() int
- func (t Time) DayTime() DayTime
- func (t Time) Equal(u Time) bool
- func (t Time) FirstMonthDay() Time
- func (t Time) FirstWeekDay() Time
- func (t Time) FirstYearDay() Time
- func (t Time) Format(layout string) string
- func (t Time) Hour() int
- func (t Time) Hour12() int
- func (t Time) In(loc *time.Location) Time
- func (t Time) IsLeap() bool
- func (t Time) IsZero() bool
- func (t Time) LastMonthDay() Time
- func (t Time) LastWeekday() Time
- func (t Time) LastYearDay() Time
- func (t Time) Location() *time.Location
- func (t Time) MarshalJSON() ([]byte, error)
- func (t Time) MarshalText() ([]byte, error)
- func (t Time) Minute() int
- func (t Time) Month() Month
- func (t Time) MonthWeek() int
- func (t Time) Nanosecond() int
- func (t Time) RMonthDay() int
- func (t Time) RYearDay() int
- func (t Time) RYearWeek() int
- func (t Time) Round(d time.Duration) Time
- func (t *Time) Scan(src any) error
- func (t Time) Second() int
- func (t *Time) Set(year int, month Month, day, hour, minute, sec, nsec int, loc *time.Location)
- func (t *Time) SetDay(day int)
- func (t *Time) SetHour(hour int)
- func (t *Time) SetMinute(minute int)
- func (t *Time) SetMonth(month Month)
- func (t *Time) SetNanosecond(nsec int)
- func (t *Time) SetSecond(sec int)
- func (t *Time) SetTime(ti time.Time)
- func (t *Time) SetUnix(sec, nsec int64)
- func (t *Time) SetYear(year int)
- func (t Time) Since(t2 Time) int64deprecated
- func (t Time) String() string
- func (t Time) Sub(u Time) time.Duration
- func (t Time) Time() time.Time
- func (t Time) TimeFormat(layout string) string
- func (t Time) Tomorrow() Time
- func (t Time) Truncate(d time.Duration) Time
- func (t Time) Unix() int64
- func (t Time) UnixMicro() int64
- func (t Time) UnixMilli() int64
- func (t Time) UnixNano() int64
- func (t *Time) UnmarshalJSON(data []byte) error
- func (t *Time) UnmarshalText(data []byte) error
- func (t Time) Value() (driver.Value, error)
- func (t Time) Weekday() Weekday
- func (t Time) Year() int
- func (t Time) YearDay() int
- func (t Time) YearWeek() int
- func (t Time) Yesterday() Time
- func (t Time) Zone() (string, int)
- func (t Time) ZoneOffset(layout ...string) string
- type Weekday
Examples ¶
Constants ¶
const ( // RFC3339 is the RFC 3339 profile of ISO 8601, e.g. "1394-07-02T12:59:59+03:30". RFC3339 = "yyyy-MM-ddTHH:mm:ssZ" // RFC3339Nano is RFC3339 with a fractional second, trailing zeros removed. RFC3339Nano = "yyyy-MM-ddTHH:mm:ss.999999999Z" // DateTime is a date and a clock reading, e.g. "1394-07-02 12:59:59". DateTime = "yyyy-MM-dd HH:mm:ss" // DateOnly is a date without a clock reading, e.g. "1394-07-02". DateOnly = "yyyy-MM-dd" // TimeOnly is a clock reading without a date, e.g. "12:59:59". TimeOnly = "HH:mm:ss" // Kitchen is a short 12-hour clock reading, e.g. "12:59 ب.ظ". Kitchen = "h:mm a" // LongDate spells out the weekday and month, e.g. "پنجشنبه 2 مهر 1394". LongDate = "E d MMM yyyy" )
Predefined layouts for use with Time.Format and Parse.
Variables ¶
var ErrScanSource = errors.New("ptime: unsupported source type for Scan")
ErrScanSource is returned by Time.Scan when the database driver hands over a value that cannot be converted into a Time.
Functions ¶
func Afghanistan ¶
Afghanistan returns the "Asia/Kabul" time zone.
If the time zone database is unavailable, a fixed UTC+04:30 zone is returned instead. The result is cached, so calling Afghanistan repeatedly is cheap.
Types ¶
type AmPm ¶
type AmPm int
An AmPm specifies the 12-hour clock marker.
type DayTime ¶
type DayTime int
A DayTime represents a part of the day, derived from the hour.
Parts of the day, each covering a three hour window.
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 )
Months of the Persian calendar, as used in Iran.
Months of the Persian calendar, as used in Afghanistan (Dari names). They are aliases of the Iranian names above and share the same values.
func (Month) Dari ¶
Dari returns the Dari (Afghan Persian) name of the month. Out of range values are clamped to Hamal and Hut.
Example ¶
package main
import (
"fmt"
ptime "github.com/amiranmanesh/go-persian-calendar"
)
func main() {
fmt.Println(ptime.Mehr.String(), ptime.Mehr.Dari())
}
Output: مهر میزان
type ParseError ¶
type ParseError struct {
// Layout is the layout that was given to the parse function.
Layout string
// Value is the value that was given to the parse function.
Value string
// LayoutElem is the layout element that failed to match.
LayoutElem string
// ValueElem is the remainder of the value at the point of failure.
ValueElem string
// Message describes the failure when it is not a plain mismatch.
Message string
}
A ParseError describes a problem parsing a time string.
func (*ParseError) Error ¶
func (e *ParseError) Error() string
Error implements the error interface.
type Time ¶
type Time struct {
// contains filtered or unexported fields
}
A Time represents an instant in the Persian (Solar Hijri) calendar with nanosecond precision.
The zero value is reported by Time.IsZero. Like time.Time, a Time is small enough to pass by value; every method with a value receiver returns a new Time instead of mutating the receiver.
func Date ¶
Date returns the Time corresponding to the given Persian calendar date and clock reading.
The arguments may be outside their usual ranges and are normalized during the conversion. If loc is nil, the local time zone is used.
Example ¶
package main
import (
"fmt"
"time"
ptime "github.com/amiranmanesh/go-persian-calendar"
)
func main() {
pt := ptime.Date(1394, ptime.Mehr, 2, 12, 59, 59, 0, ptime.Iran())
fmt.Println(pt.Time().Format(time.DateOnly))
}
Output: 2015-09-24
func New ¶
New converts a Gregorian time into the Persian calendar.
It returns the zero Time if the Gregorian year of t is below 1097.
Example ¶
package main
import (
"fmt"
"time"
ptime "github.com/amiranmanesh/go-persian-calendar"
)
func main() {
gt := time.Date(2016, time.January, 1, 12, 1, 1, 0, ptime.Iran())
pt := ptime.New(gt)
fmt.Println(pt.Date())
}
Output: 1394 دی 11
func Now ¶
func Now() Time
Now returns the current time in the Persian calendar and the local time zone.
func Parse ¶
Parse parses a formatted Persian date string using the layout language of Time.Format.
Fields absent from the layout default to Farvardin 1 of year 1 at 00:00:00. Computed fields such as the day of year or the week of month are matched and discarded, since they carry no information the other fields do not.
In the absence of a time zone in the value, Parse returns a time in UTC. Use ParseInLocation to pick a different default.
Example ¶
package main
import (
"fmt"
ptime "github.com/amiranmanesh/go-persian-calendar"
)
func main() {
pt, err := ptime.Parse("yyyy/MM/dd HH:mm:ss", "1394/07/02 12:59:59")
if err != nil {
panic(err)
}
fmt.Println(pt.Format(ptime.LongDate))
}
Output: پنجشنبه 2 مهر 1394
func ParseInLocation ¶
ParseInLocation is like Parse but interprets a value without a time zone as being in the given location.
Example ¶
package main
import (
"fmt"
ptime "github.com/amiranmanesh/go-persian-calendar"
)
func main() {
pt, err := ptime.ParseInLocation(ptime.DateOnly, "1403-12-30", ptime.Iran())
if err != nil {
panic(err)
}
fmt.Println(pt.IsLeap(), pt.Format(ptime.RFC3339))
}
Output: true 1403-12-30T00:00:00+03:30
func ParseTimeFormat ¶
ParseTimeFormat parses a formatted Persian date string using the standard library reference-time layout language of Time.TimeFormat.
In the absence of a time zone in the value, ParseTimeFormat returns a time in UTC. Use ParseTimeFormatInLocation to pick a different default.
func ParseTimeFormatInLocation ¶
ParseTimeFormatInLocation is like ParseTimeFormat but interprets a value without a time zone as being in the given location.
func Unix ¶
Unix returns the Time corresponding to the given Unix time: sec seconds and nsec nanoseconds since January 1, 1970 UTC.
func UnixMicro ¶
UnixMicro returns the Time corresponding to the given Unix time: usec microseconds since January 1, 1970 UTC.
func UnixMilli ¶
UnixMilli returns the Time corresponding to the given Unix time: msec milliseconds since January 1, 1970 UTC.
func (Time) AddDate ¶
AddDate returns the time corresponding to adding the given number of years, months and days to t. The result is normalized the same way Time.Set is.
Example ¶
package main
import (
"fmt"
ptime "github.com/amiranmanesh/go-persian-calendar"
)
func main() {
pt := ptime.Date(1394, ptime.Esfand, 29, 0, 0, 0, 0, ptime.Iran())
fmt.Println(pt.AddDate(0, 0, 1).Format(ptime.DateOnly))
}
Output: 1395-01-01
func (Time) AppendFormat ¶
AppendFormat is like Time.Format but appends the result to b and returns the extended buffer, avoiding a string allocation.
func (Time) AppendTimeFormat ¶
AppendTimeFormat is like Time.TimeFormat but appends the result to b and returns the extended buffer.
func (Time) BeginningOfMonth ¶
BeginningOfMonth returns the first day of the month of t at 00:00:00.
Example ¶
package main
import (
"fmt"
ptime "github.com/amiranmanesh/go-persian-calendar"
)
func main() {
pt := ptime.Date(1394, ptime.Mehr, 17, 13, 45, 12, 0, ptime.Iran())
fmt.Println(pt.BeginningOfMonth().Format(ptime.DateTime))
fmt.Println(pt.LastMonthDay().Format(ptime.DateOnly))
}
Output: 1394-07-01 00:00:00 1394-07-30
func (Time) BeginningOfWeek ¶
BeginningOfWeek returns the first day of the week of t at 00:00:00.
func (Time) BeginningOfYear ¶
BeginningOfYear returns the first day of the year of t at 00:00:00.
func (Time) Compare ¶
Compare compares t with u: it returns -1 if t is before u, +1 if t is after u, and 0 if they represent the same instant.
func (Time) DayTime ¶
DayTime returns the part of the day t falls into:
[0,3) midnight [3,6) dawn [6,9) morning [9,12) before noon [12,15) noon [15,18) afternoon [18,21) evening [21,24) night
func (Time) Equal ¶
Equal reports whether t and u represent the same instant.
Two times can be equal even if they are in different locations: 6:00 +0200 and 4:00 UTC are equal.
func (Time) FirstMonthDay ¶
FirstMonthDay returns the first day of the month of t, keeping the clock of t.
func (Time) FirstWeekDay ¶
FirstWeekDay returns the first day of the week of t, keeping the clock of t.
func (Time) FirstYearDay ¶
FirstYearDay returns the first day of the year of t, keeping the clock of t.
func (Time) Format ¶
Format returns the textual representation of t according to layout.
The layout is built from the following pattern letters. Anything else is copied to the output verbatim.
yyyy, yyy, y year (e.g. 1394) yy 2-digit year (e.g. 94) MMM Persian month name (e.g. فروردین) MMI Dari month name (e.g. حمل) MM 2-digit month (e.g. 01) M month (e.g. 1) rw remaining weeks of year w week of year W week of month RD remaining days of year D day of year rd remaining days of month dd 2-digit day of month (e.g. 01) d day of month (e.g. 1) E Persian weekday name (e.g. شنبه) e short Persian weekday name (e.g. ش) A Persian 12-hour marker (e.g. قبل از ظهر) a short Persian 12-hour marker (e.g. ق.ظ) HH 2-digit hour [00-23] H hour [0-23] kk 2-digit hour [01-24] k hour [1-24] hh 2-digit hour [01-12] h hour [1-12] KK 2-digit hour [00-11] K hour [0-11] mm 2-digit minute [00-59] m minute [0-59] ss 2-digit second [00-59] s second [0-59] n part of the day (e.g. صبح) ns nanosecond, as a plain decimal number S 3-digit millisecond (e.g. 001) .000 fractional second, 3 digits .000000 fractional second, 6 digits .000000000 fractional second, 9 digits .999 fractional second, 3 digits, trailing zeros removed .999999 fractional second, 6 digits, trailing zeros removed .999999999 fractional second, 9 digits, trailing zeros removed z time zone name (e.g. Asia/Tehran) Z time zone offset (e.g. +03:30)
A ".999" style fraction is omitted entirely, dot included, when the fractional second is zero.
Example ¶
package main
import (
"fmt"
ptime "github.com/amiranmanesh/go-persian-calendar"
)
func main() {
pt := ptime.Date(1394, ptime.Bahman, 11, 21, 54, 30, 0, ptime.Iran())
fmt.Println(pt.Format("yyyy/MM/dd E hh:mm:ss a"))
}
Output: 1394/11/11 یکشنبه 09:54:30 ب.ظ
func (Time) In ¶
In returns a copy of t associated with loc.
It changes only the location, not the calendar fields. loc must not be nil.
func (Time) LastMonthDay ¶
LastMonthDay returns the last day of the month of t, keeping the clock of t.
func (Time) LastWeekday ¶
LastWeekday returns the last day of the week of t, keeping the clock of t.
func (Time) LastYearDay ¶
LastYearDay returns the last day of the year of t, keeping the clock of t.
func (Time) MarshalJSON ¶
MarshalJSON implements [json.Marshaler].
The time is written as a quoted string in RFC3339Nano format. The zero Time is written as null.
Example ¶
package main
import (
"encoding/json"
"fmt"
ptime "github.com/amiranmanesh/go-persian-calendar"
)
func main() {
type event struct {
Name string `json:"name"`
At ptime.Time `json:"at"`
}
encoded, err := json.Marshal(event{
Name: "نوروز",
At: ptime.Date(1404, ptime.Farvardin, 1, 0, 0, 0, 0, ptime.Iran()),
})
if err != nil {
panic(err)
}
fmt.Println(string(encoded))
}
Output: {"name":"نوروز","at":"1404-01-01T00:00:00+03:30"}
func (Time) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
The time is written in RFC3339Nano format. The zero Time is written as an empty string.
func (Time) Nanosecond ¶
Nanosecond returns the nanosecond of t in the range [0, 999999999].
func (Time) Round ¶
Round returns t rounded to the nearest multiple of d since the zero time, rounding half away from zero. If d <= 0, t is returned unchanged.
func (*Time) Scan ¶
Scan implements [sql.Scanner].
It accepts NULL, a time.Time, and a Gregorian timestamp in text form. Text is read as Gregorian rather than Persian because that is what Time.Value hands to the driver, and the two are indistinguishable on the wire.
func (*Time) Set ¶
Set sets every field of t at once.
The arguments may be outside their usual ranges and are normalized. loc must not be nil.
func (*Time) SetNanosecond ¶
SetNanosecond sets the nanosecond of t.
func (*Time) SetUnix ¶
SetUnix sets t to the Unix time sec seconds and nsec nanoseconds since January 1, 1970 UTC.
func (Time) String ¶
String returns t in RFC3339Nano format.
The zero Time has no calendar date to render and is reported as "0000-00-00T00:00:00Z".
func (Time) Sub ¶
Sub returns the duration t-u.
Example ¶
package main
import (
"fmt"
ptime "github.com/amiranmanesh/go-persian-calendar"
)
func main() {
from := ptime.Date(1394, ptime.Mehr, 2, 8, 0, 0, 0, ptime.Iran())
to := ptime.Date(1394, ptime.Mehr, 3, 12, 30, 0, 0, ptime.Iran())
fmt.Println(to.Sub(from))
}
Output: 28h30m0s
func (Time) TimeFormat ¶
TimeFormat returns the textual representation of t according to a standard library reference-time layout, as used by time.Time.Format.
2006 4-digit year (e.g. 1394) 06 2-digit year (e.g. 94) 01 2-digit month (e.g. 01) 1 month (e.g. 1) Jan month name (e.g. مهر) January month name (e.g. مهر) 02 2-digit day of month (e.g. 07) 2 day of month (e.g. 7) _2 day of month, space padded to 2 characters (e.g. " 7") Mon short weekday name (e.g. ش) Monday weekday name (e.g. شنبه) Morning part of the day (e.g. صبح) 03 2-digit hour [01-12] 3 hour [1-12] 15 2-digit hour [00-23] 04 2-digit minute 4 minute 05 2-digit second 5 second .000 fractional second, 3 digits .000000 fractional second, 6 digits .000000000 fractional second, 9 digits .999 fractional second, 3 digits, trailing zeros removed .999999 fractional second, 6 digits, trailing zeros removed .999999999 fractional second, 9 digits, trailing zeros removed PM 12-hour marker (e.g. قبل از ظهر) pm short 12-hour marker (e.g. ق.ظ) MST time zone name -0700 time zone offset (e.g. +0330) -07 time zone offset (e.g. +03) -07:00 time zone offset (e.g. +03:30) Z0700 time zone offset (e.g. +0330) Z07:00 time zone offset (e.g. +03:30)
Month names are Dari when the location of t is Afghanistan and Iranian Persian otherwise.
Example ¶
package main
import (
"fmt"
ptime "github.com/amiranmanesh/go-persian-calendar"
)
func main() {
pt := ptime.Date(1394, ptime.Mehr, 2, 14, 7, 8, 0, ptime.Iran())
fmt.Println(pt.TimeFormat("2 Jan 2006"))
}
Output: 2 مهر 1394
func (Time) Truncate ¶
Truncate returns t rounded down to a multiple of d since the zero time. If d <= 0, t is returned unchanged.
func (*Time) UnmarshalJSON ¶
UnmarshalJSON implements [json.Unmarshaler].
It expects a quoted string in RFC3339Nano format. A null sets t to the zero Time.
func (*Time) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
It expects RFC3339Nano format. An empty input sets t to the zero Time.
func (Time) Value ¶
Value implements driver.Valuer.
The value is handed to the driver as a Gregorian time.Time, so that it is stored as an ordinary SQL timestamp. The zero Time becomes NULL.
func (Time) ZoneOffset ¶
ZoneOffset returns the time zone offset of t as [+|-]HH:mm.
An optional layout selects a different shape; it must be one of "-0700", "-07", "-07:00", "Z0700" or "Z07:00". The "Z" forms render UTC as "Z".
type Weekday ¶
type Weekday int
A Weekday specifies a day of the week, starting from Shanbeh = 0.
Days of the week in the Persian calendar.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package holiday answers whether a Persian date is a public holiday, and what is commemorated on it.
|
Package holiday answers whether a Persian date is a public holiday, and what is commemorated on it. |
|
internal/gen
command
Command gen reconciles the computed holiday calendar with published Iranian calendars and rewrites holiday/data/iran.json.
|
Command gen reconciles the computed holiday calendar with published Iranian calendars and rewrites holiday/data/iran.json. |
|
internal
|
|
|
jdn
Package jdn converts calendar dates to and from the Julian Day Number, a continuous count of days that is independent of any calendar.
|
Package jdn converts calendar dates to and from the Julian Day Number, a continuous count of days that is independent of any calendar. |