Skip to content

a lightweight package assists developers in effectively handling Jalali (Shamsi or Iranian) dates in Go

License

Notifications You must be signed in to change notification settings

ErfanMomeniii/jalali

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

image description

go version license version

jalali

The Jalali calendar is a solar calendar that was used in Persia. Variants of this calendar are still used today in Iran, Afghanistan and some other countries. You can find more information on Wikipedia or check out the Calendar Converter.

Documentation

Install

go get github.com/erfanmomeniii/jalali

Next, include it in your application:

import "github.com/erfanmomeniii/jalali"

Quick Start

Example

The following example illustrates how to use this package:

package main

import (
	"github.com/erfanmomeniii/jalali"
	"time"
)

func main() {
	// Return datetime of today
	jalali.Now() //1402-04-29 14:12:00

	// Return datetime of yesterday
	jalali.Yesterday() // 1402-04-28 14:12:59

	// Return datetime of tomorrow
	jalali.Tomorrow() // 1402-04-30 14:13:47

	// Convert gregorian datetime to jalali
	jalali.ConvertGregorianToJalali(time.Date(
		2023, 7, 20, 10, 45, 25, 0, time.UTC)) // 1402-04-29 14:15:25

	// Convert jalali datetime to gregorian
	jalali.ConvertJalaliToGregorian(jalali.New(
		1402, 4, 29, 14, 15, 25)) // 2023-07-20 10:45:25 +0000 UTC

	// Create instance of jalali datetime
	j := jalali.New(1402, 4, 29, 14, 15, 25)

	// Set locale for localizing the output
	j.SetLocale(jalali.PersianLanguage)
	j.String() // ۱۴۰۲-۰۴-۲۹ ۱۴:۱۵:۲۵

	// Return the localized string representation of the day of the week and month of the year for j
	j.MonthToString() // تیر
	j.WeekToString()  // پنجشنبه

	// Return the day of the week, month and year for the j
	j.DayOfWeek()  // 6
	j.DayOfMonth() // 29
	j.DayOfYear()  // 122

	// Return yesterday and tomorrow of j
	j.Yesterday() // ۱۴۰۲-۰۴-۲۸ ۱۴:۱۵:۲۵
	j.Tomorrow()  // ۱۴۰۲-۰۴-۳۰ ۱۴:۱۵:۲۵

	j.SetLocale(jalali.EnglishLanguage)

	// Return the datetime corresponding to adding the given number of years, months, and days to j
	j.AddDate(0, 0, 1) // 1402-04-30 14:15:25

	// Return year, month, day, hour, minute and second of j
	j.Year()   // 1402
	j.Month()  // 4
	j.Day()    // 29
	j.Hour()   // 14
	j.Minute() // 15
	j.Second() // 25
}

Usage

You can find the complete documentation here.

type jalaliDateTime

jalaliDateTime is an instantiation of jalali Date and Time.

func New

func New(year int, month int, day int, hour int, minute int, second int) *jalaliDateTime

New creates a new instance of a jalaliDateTime.

func (j *jalaliDateTime) SetLocale

func (j *jalaliDateTime) SetLocale(lang Lang)

SetLocale sets the locale of the jalaliDateTime.

func Now

func Now() *jalaliDateTime

Now returns the current jalaliDateTime.

func ConvertGregorianToJalali

func ConvertGregorianToJalali(t time.Time) *jalaliDateTime

ConvertGregorianToJalali returns converted date and time on t from gregorian to jalali.

func ConvertJalaliToGregorian

func ConvertJalaliToGregorian(j *jalaliDateTime) time.Time

ConvertJalaliToGregorian returns converted date and time on j from jalali to gregorian.

func ToGregorian

func ToGregorian(gregorianSeconds int64) time.Time

ToGregorian returns time.Time obtained from given seconds.

func ToJalali

func ToJalali(jalaliSeconds int64) *jalaliDateTime

ToJalali returns jalaliDateTime obtained from given seconds.

func IsLeapYear

func IsLeapYear(year int) int

IsLeapYear determines the year is leap or not in jalali date.

func Yesterday

func Yesterday() *jalaliDateTime

Yesterday returns datetime of yesterday.

func Tomorrow

func Tomorrow() *jalaliDateTime

Tomorrow returns datetime of tomorrow.

func (j *jalaliDateTime) Add

func (j *jalaliDateTime) Add(t jalaliDateTime) *jalaliDateTime

Add returns the time j+t.

func (j *jalaliDateTime) AddDate

func (j *jalaliDateTime) AddDate(year int, month int, day int) *jalaliDateTime

AddDate returns the time corresponding to adding the given number of years, months, and days to j

func (j *jalaliDateTime) Yesterday

func (j *jalaliDateTime) Yesterday() *jalaliDateTime

Yesterday returns datetime of yesterday on a given day.

func (j *jalaliDateTime) Tomorrow

func (j *jalaliDateTime) Tomorrow() *jalaliDateTime

Tomorrow returns datetime of tomorrow on a given day.

func (j *jalaliDateTime) Year

func (j *jalaliDateTime) Year() int

Year returns the year in which j occurs.

func (j *jalaliDateTime) Month

func (j *jalaliDateTime) Month() int

Month returns the month of the year specified by j.

func (j *jalaliDateTime) Day

func (j *jalaliDateTime) Day() int

Day returns the day of the month specified by j.

func (j *jalaliDateTime) Hour

func (j *jalaliDateTime) Hour() int

Hour returns the hour within the day specified by j, in the range [0, 23].

func (j *jalaliDateTime) Minute

func (j *jalaliDateTime) Minute() int

Minute returns the minute offset within the hour specified by j, in the range [0, 59].

func (j *jalaliDateTime) Second

func (j *jalaliDateTime) Second() int

Second returns the second offset within the minute specified by j, in the range [0, 59].

func (j *jalaliDateTime) TimeStamp

func (j *jalaliDateTime) TimeStamp() int64

TimeStamp returns the timestamp of the jalaliDateTime.

func (j *jalaliDateTime) DayOfYear

func (j *jalaliDateTime) DayOfYear() int

DayOfYear returns the day of the year for the jalaliDateTime.

func (j *jalaliDateTime) DayOfMonth

func (j *jalaliDateTime) DayOfMonth() int

DayOfMonth returns the day the month for the jalaliDateTime.

func (j *jalaliDateTime) DayOfWeek

func (j *jalaliDateTime) DayOfWeek() int

DayOfWeek returns the day the week for the jalaliDateTime.

func (j *jalaliDateTime) WeekToString

func (j *jalaliDateTime) WeekToString() string

WeekToString returns the localized string representation of the day of the week for the jalaliDateTime.

func (j *jalaliDateTime) MonthToString

func (j *jalaliDateTime) MonthToString() string

MonthToString returns the localized string representation of the month for the jalaliDateTime.

func (j *jalaliDateTime) Time

func (j *jalaliDateTime) Time() time.Time

Time returns the time.Time equivalent of the jalaliDateTime.

func (j *jalaliDateTime) String

func (j *jalaliDateTime) String() string

String returns the string representation of the jalaliDateTime.

Contributing

Pull requests are welcome. For changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.

About

a lightweight package assists developers in effectively handling Jalali (Shamsi or Iranian) dates in Go

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages