gojalaali

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2025 License: ISC Imports: 8 Imported by: 1

README

Go Jalaali

gojalaali is a Go package that provides implementation of Jalaali (Persian) calendar for standard go time interface. It supports standard Go time package layout for formatting and parsing jalaali dates and times, as well as converting between Jalaali and Gregorian dates.

Installation

To install the package, use the following command:

go get github.com/mekramy/gojalaali

Usage

Here is an example of how to use the gojalaali package:

package main

import (
    "fmt"
    "time"

    "github.com/mekramy/gojalaali"
)

func main() {
    // Create a new Jalaali date from the current time
    j := gojalaali.Now()

    // Print the Jalaali date
    fmt.Println("Current Jalaali date:", j)

    // Convert Jalaali date to Gregorian date
    gregorian := j.Time()
    fmt.Println("Gregorian date:", gregorian)

    // Parse a Jalaali date from a string
    layout := "2006-01-02 15:04:05"
    datetime := "1400-07-01 12:30:45"
    parsedJalaali, err := gojalaali.Parse(layout, datetime)
    if err != nil {
        fmt.Println("Error parsing Jalaali date:", err)
        return
    }
    fmt.Println("Parsed Jalaali date:", parsedJalaali)
}

Constructors

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

Parses a Jalaali date from a string according to the specified layout. Returns a Jalaali instance and an error if the parsing fails.

Example:

layout := "2006-01-02 15:04:05"
datetime := "1400-07-01 12:30:45"
parsedJalaali, err := gojalaali.Parse(layout, datetime)
if err != nil {
    fmt.Println("Error parsing Jalaali date:", err)
    return
}
fmt.Println("Parsed Jalaali date:", parsedJalaali)
New(t time.Time) Jalaali

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

Example:

t := time.Now()
j := gojalaali.New(t)
fmt.Println("Jalaali date:", j)
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.

Example:

j := gojalaali.Date(1400, gojalaali.Mehr, 1, 12, 30, 45, 0, gojalaali.TehranTz())
fmt.Println("Jalaali date:", j)
Unix(sec, nsec int64) Jalaali

Creates a new Jalaali instance from a Unix timestamp.

Example:

sec := time.Now().Unix()
nsec := int64(0)
j := gojalaali.Unix(sec, nsec)
fmt.Println("Jalaali date:", j)
Now() Jalaali

Creates a new Jalaali instance from the current time.

Example:

j := gojalaali.Now()
fmt.Println("Current Jalaali date:", j)
TehranTz() *time.Location

Returns the time zone for Tehran.

Example:

tehranTz := gojalaali.TehranTz()
fmt.Println("Tehran time zone:", tehranTz)
KabulTz() *time.Location

Returns the time zone for Kabul.

Example:

kabulTz := gojalaali.KabulTz()
fmt.Println("Kabul time zone:", kabulTz)

API Documentation

IsZero() bool

Returns true if the Jalaali date is a zero time instance.

IsLeap() bool

Returns true if the year of the Jalaali date is a leap year.

Since(t2 Jalaali) time.Duration

Returns the number of seconds between the current Jalaali date and another Jalaali date t2.

AmPm() AmPm

Returns the 12-hour marker (AM/PM) of the Jalaali date.

Zone() (string, int)

Returns the time zone name and its offset in seconds east of UTC for the Jalaali date.

In(loc *time.Location) Jalaali

Sets the location of the Jalaali date and returns a new instance. If loc is nil, it returns the same instance.

Add(d time.Duration) Jalaali

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

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

Adds the specified hours, minutes, seconds, and nanoseconds to the Jalaali date and returns a new instance.

AddDate(year, month, day int) Jalaali

Adds the specified years, months, and days to the Jalaali date and returns a new instance.

AddDatetime(year, month, day, hour, min, sec, nsec int) Jalaali

Adds the specified years, months, days, hours, minutes, seconds, and nanoseconds to the Jalaali date and returns a new instance.

Yesterday() Jalaali

Returns a new instance of the Jalaali date representing the day before the current instance.

Tomorrow() Jalaali

Returns a new instance of the Jalaali date representing the day after the current instance.

BeginningOfDay() Jalaali

Returns a new instance of the Jalaali date representing the 00:00:00.000000000 time of today.

EndOfDay() Jalaali

Returns a new instance of the Jalaali date representing the 23:59:59.999999999 time of today.

FirstWeekDay() Jalaali

Returns a new instance of the Jalaali date representing the first day of the week of the current instance.

LastWeekDay() Jalaali

Returns a new instance of the Jalaali date representing the last day of the week of the current instance.

BeginningOfWeek() Jalaali

Returns a new instance of the Jalaali date representing the first day of the week of the current instance, with the time set to 00:00:00.000000000.

EndOfWeek() Jalaali

Returns a new instance of the Jalaali date representing the last day of the week of the current instance, with the time set to 23:59:59.999999999.

FirstMonthDay() Jalaali

Returns a new instance of the Jalaali date representing the first day of the month of the current instance.

LastMonthDay() Jalaali

Returns a new instance of the Jalaali date representing the last day of the month of the current instance.

BeginningOfMonth() Jalaali

Returns a new instance of the Jalaali date representing the first day of the month of the current instance, with the time set to 00:00:00.000000000.

EndOfMonth() Jalaali

Returns a new instance of the Jalaali date representing the last day of the month of the current instance, with the time set to 23:59:59.999999999.

FirstYearDay() Jalaali

Returns a new instance of the Jalaali date representing the first day of the year of the current instance.

LastYearDay() Jalaali

Returns a new instance of the Jalaali date representing the last day of the year of the current instance.

BeginningOfYear() Jalaali

Returns a new instance of the Jalaali date representing the first day of the year of the current instance, with the time set to 00:00:00.000000000.

EndOfYear() Jalaali

Returns a new instance of the Jalaali date representing the last day of the year of the current instance, with the time set to 23:59:59.999999999.

SetYear(year int)

Sets the year of the Jalaali date.

SetMonth(month Month)

Sets the month of the Jalaali date.

SetDay(day int)

Sets the day of the Jalaali date.

SetHour(hour int)

Sets the hour of the Jalaali time.

SetMinute(min int)

Sets the minute of the Jalaali time.

SetSecond(sec int)

Sets the second of the Jalaali time.

SetNanosecond(nsec int)

Sets the nanosecond of the Jalaali time.

SetTime(hour, min, sec, nsec int)

Sets the hour, minute, second, and nanosecond of the Jalaali time. Pass -1 to ignore a parameter.

SetDate(year, month, day int)

Sets the year, month, and day of the Jalaali date. Pass -1 to ignore a parameter.

SetDateTime(year, month, day, hour, min, sec, nsec int)

Sets the year, month, day, hour, minute, second, and nanosecond of the Jalaali date and time. Pass -1 to ignore a parameter.

Year() int

Returns the year of the Jalaali date.

YearDay() int

Returns the day of the year of the Jalaali date.

YearRemainDays() int

Returns the number of remaining days in the year of the Jalaali date.

Month() Month

Returns the month of the Jalaali date in the range [1, 12].

Weekday() Weekday

Returns the weekday of the Jalaali date.

MonthWeek() int

Returns the week of the month of the Jalaali date.

YearWeek() int

Returns the week of the year of the Jalaali date.

YearRemainWeeks() int

Returns the number of remaining weeks in the year of the Jalaali date.

Day() int

Returns the day of the month of the Jalaali date.

MonthRemainDays() int

Returns the number of remaining days in the month of the Jalaali date.

Hour() int

Returns the hour of the Jalaali time in the range [0, 23].

Hour12() int

Returns the hour of the Jalaali time in the range [0, 11].

Minute() int

Returns the minute of the Jalaali time in the range [0, 59].

Second() int

Returns the second of the Jalaali time in the range [0, 59].

Nanosecond() int

Returns the nanosecond of the Jalaali time in the range [0, 999999999].

DayTime() DayTime

Returns the part of the day for the Jalaali time.

Location() *time.Location

Returns a pointer to the time location of the Jalaali date.

Date() (int, Month, int)

Returns the year, month, and day of the Jalaali date.

Clock() (int, int, int)

Returns the hour, minute, and second of the Jalaali time.

Unix() int64

Returns the number of seconds since January 1, 1970 UTC.

UnixNano() int64

Returns the number of nanoseconds since January 1, 1970 UTC.

Time() time.Time

Converts the Jalaali date to a Gregorian date and returns it as a Go time.Time object.

String() string

Returns the Jalaali date in RFC3339 format.

Format(layout string) string

Formats the Jalaali date according to the specified layout.

Layout Description Example
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"

License

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

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. 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