ptime

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2024 License: MIT Imports: 5 Imported by: 15

README

Go Persian Calendar

godoc Test and Build goreportcard License

Go Persian Calendar provides functionality for conversion among Persian (Solar Hijri) and Gregorian calendars. A Julian calendar is used as an interface for all conversions. The package name is ptime and it is compatible with the package time. All months are available with both Iranian and Dari Persian names. This source code is licensed under MIT license that can be found in the LICENSE file.

Installation

go get github.com/yaa110/go-persian-calendar

Getting started

1- Import the package ptime. Most of the time you need to import time and fmt packages, too.

import (
    ptime "github.com/yaa110/go-persian-calendar"
    "time"
    "fmt"
)

2- Convert Gregorian calendar to Persian calendar.

// Create a new instance of time.Time
var t time.Time = time.Date(2016, time.January, 1, 12, 1, 1, 0, ptime.Iran())

// Get a new instance of ptime.Time using time.Time
pt := ptime.New(t)

// Get the date in Persian calendar
fmt.Println(pt.Date()) // output: 1394 دی 11

3- Convert Persian calendar to Gregorian calendar.

// Create a new instance of ptime.Time
var pt ptime.Time = ptime.Date(1394, ptime.Mehr, 2, 12, 59, 59, 0, ptime.Iran())

// Get a new instance of time.Time
t := pt.Time()

// Get the date in Gregorian calendar
fmt.Println(t.Date()) // output: 2015 September 24

4- Get the current time.

// Get a new instance of ptime.Time representing the current time
pt := ptime.Now()

// Get year, month, day
fmt.Println(pt.Date()) // output: 1394 بهمن 11
fmt.Println(pt.Year(), pt.Month(), pt.Day()) // output: 1394 بهمن 11

// Get hour, minute, second
fmt.Println(pt.Clock()) // output: 21 54 30
fmt.Println(pt.Hour(), pt.Minute(), pt.Second()) // output: 21 54 30

// Get Unix timestamp (the number of seconds since January 1, 1970 UTC)
fmt.Println(pt.Unix()) // output: 1454277270

// Get yesterday, today and tomorrow
fmt.Println(pt.Yesterday().Weekday()) // output: شنبه
fmt.Println(pt.Weekday()) // output: یک‌شنبه
fmt.Println(pt.Tomorrow().Weekday()) // output: دوشنبه

// Get First and last day of week
fmt.Println(pt.FirstWeekDay().Date()) // output: 1394 بهمن 10
fmt.Println(pt.LastWeekday().Date()) // output: 1394 بهمن 16

// Get First and last day of month
fmt.Println(pt.FirstMonthDay().Weekday()) // output: پنج‌شنبه
fmt.Println(pt.LastMonthDay().Weekday()) // output: جمعه

// Get First and last day of year
fmt.Println(pt.FirstYearDay().Weekday()) // output: شنبه
fmt.Println(pt.LastYearDay().Weekday()) // output: شنبه

// Get the week of month
fmt.Println(pt.MonthWeek()) // output: 3

// Get the week of year
fmt.Println(pt.YearWeek()) // output: 46

// Get the number of remaining weeks of the year
fmt.Println(pt.RYearWeek()) // output: 6

5- Format the time.

// Get a new instance of ptime.Time using Unix timestamp
pt := ptime.Unix(1454277270, 0)

fmt.Println(pt.Format("yyyy/MM/dd E hh:mm:ss a")) // output: 1394/11/11 یک‌شنبه 09:54:30 ب.ظ

// yyyy, yyy, y     year (e.g. 1394)
// yy               2-digits representation of year (e.g. 94)
// MMM              the Persian name of month (e.g. فروردین)
// MMI              the Dari name of month (e.g. حمل)
// MM               2-digits representation of 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-digits representation of day (e.g. 01)
// d                day (e.g. 1)
// E                the Persian name of weekday (e.g. شنبه)
// e                the Persian short name of weekday (e.g. ش)
// A                the Persian name of 12-Hour marker (e.g. قبل از ظهر)
// a                the Persian short name of 12-Hour marker (e.g. ق.ظ)
// HH               2-digits representation of hour [00-23]
// H                hour [0-23]
// kk               2-digits representation of hour [01-24]
// k                hour [1-24]
// hh               2-digits representation of hour [01-12]
// h                hour [1-12]
// KK               2-digits representation of hour [00-11]
// K                hour [0-11]
// mm               2-digits representation of minute [00-59]
// m                minute [0-59]
// ss               2-digits representation of seconds [00-59]
// s                seconds [0-59]
// ns               nanoseconds
// S                3-digits representation of milliseconds (e.g. 001)
// z                the name of location
// Z                zone offset (e.g. +03:30)

6- Format the time using standard format.

pt := ptime.Date(1394, 7, 2, 14, 7, 8, 0, Iran())

fmt.Println(pt.TimeFormat("2 Jan 2006")) // output: 2 مهر 1394

// 2006        four digit year (e.g. 1399)
// 06          two digit year (e.g. 99)
// 01          two digit month (e.g. 01)
// 1           one digit month (e.g. 1)
// Jan         month name (e.g. آذر)
// January     month name (e.g. آذر)
// 02          two digit day (e.g. 07)
// 2           one digit day (e.g. 7)
// _2          right justified two character day (e.g.  7)
// Mon         weekday (e.g. شنبه)
// Monday      weekday (e.g. شنبه)
// 03          two digit 12 hour format (e.g. 03)
// 3           one digit 12 hour format (e.g. 3)
// 15          two digit 24 hour format (e.g. 15)
// 04          two digit minute (e.g. 03)
// 4           one digit minute (e.g. 03)
// 05          two digit minute (e.g. 09)
// 5           one digit minute (e.g. 9)
// .000        millisecond (e.g. .120)
// .000000     microsecond (e.g. .123400)
// .000000000  nanosecond (e.g. .123456000)
// .999        trailing zeros removed millisecond (e.g. .12)
// .999999     trailing zeros removed microsecond (e.g. .1234)
// .999999999  trailing zeros removed nanosecond (e.g. .123456)
// PM          full 12-Hour marker (e.g. قبل از ظهر)
// pm          short 12-Hour marker (e.g. ق.ظ)
// MST         the name of location
// -0700       zone offset (e.g. +0330)
// -07         zone offset (e.g. +03)
// -07:00      zone offset (e.g. +03:30)
// Z0700       zone offset (e.g. +0330)
// Z07:00      zone offset (e.g. +03:30)

Limitations

  • The minimum value of Gregorian year is 1097, otherwise a zero instance of ptime.Time is returned.

Documentation

Please read the documentation for more information about methods and functionality available for ptime.Time, ptime.Month, ptime.Weekday and ptime.AmPm.

Documentation

Overview

Package ptime provides functionality for implementation of Persian (Solar Hijri) Calendar.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Afghanistan

func Afghanistan() *time.Location

Afghanistan returns a pointer to time.Location of Asia/Kabul

func Iran

func Iran() *time.Location

Iran returns a pointer to time.Location of Asia/Tehran

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 added in v1.1.0

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 added in v1.1.0

func (d DayTime) String() string

String returns the Persian name of day time.

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) String

func (m Month) String() string

String returns the Persian name of the month.

type Time

type Time struct {
	// contains filtered or unexported fields
}

A Time represents a moment in time in Persian (Jalali) Calendar.

func Date

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

Date returns a new instance of Time.

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) Time

New converts Gregorian calendar to Persian calendar and

returns a new instance of Time corresponding to the time of t or a zero instance of time if Gregorian year is less than 1097.

t is an instance of time.Time in Gregorian calendar.

func Now

func Now() Time

Now returns a new instance of Time corresponding to the current time.

func Unix

func Unix(sec, nsec int64) Time

Unix returns a new instance of PersianDate from unix timestamp.

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

func (Time) Add

func (t Time) Add(d time.Duration) Time

Add returns a new instance of Time for t+d.

func (Time) AddDate

func (t Time) AddDate(years, months, days int) Time

AddDate returns a new instance of Time for t.year+years, t.month+months and t.day+days.

func (Time) AmPm

func (t Time) AmPm() AmPm

AmPm returns the 12-Hour marker of t.

func (*Time) At

func (t *Time) At(hour, min, sec, nsec int)

At sets the hour, min minute, sec second and nsec nanoseconds offsets of t.

func (Time) BeginningOfMonth added in v0.5.0

func (t Time) BeginningOfMonth() Time

BeginningOfMonth returns a new instance of Time representing the first day of the month of t. The time is reset to 00:00:00

func (Time) BeginningOfWeek added in v0.5.0

func (t Time) BeginningOfWeek() Time

BeginningOfWeek returns a new instance of Time representing the first day of the week of t. The time is reset to 00:00:00

func (Time) BeginningOfYear added in v0.5.0

func (t Time) BeginningOfYear() Time

BeginningOfYear returns a new instance of Time representing the first day of the year of t. The time is reset to 00:00:00

func (Time) Clock

func (t Time) Clock() (int, int, int)

Clock returns the hour, minute, seconds offsets of t.

func (Time) Date

func (t Time) Date() (int, Month, int)

Date returns the year, month, day of t.

func (Time) Day

func (t Time) Day() int

Day returns the day of month of t.

func (Time) DayTime added in v1.1.0

func (t Time) DayTime() DayTime

DayTime returns the dayTime of that part of the day. [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) FirstMonthDay

func (t Time) FirstMonthDay() Time

FirstMonthDay returns a new instance of Time representing the first day of the month of t.

func (Time) FirstWeekDay

func (t Time) FirstWeekDay() Time

FirstWeekDay returns a new instance of Time representing the first day of the week of t.

func (Time) FirstYearDay

func (t Time) FirstYearDay() Time

FirstYearDay returns a new instance of Time representing the first day of the year of t.

func (Time) Format

func (t Time) Format(format string) string

Format returns the formatted representation of t.

yyyy, yyy, y     year (e.g. 1394)
yy               2-digits representation of year (e.g. 94)
MMM              the Persian name of month (e.g. فروردین)
MMI              the Dari name of month (e.g. حمل)
MM               2-digits representation of month (e.g. 01)
M                month (e.g. 1)
rw               remaining weeks of year
w                week of year
RW               remaining weeks of month
W                week of month
RD               remaining days of year
D                day of year
rd               remaining days of month
dd               2-digits representation of day (e.g. 01)
d                day (e.g. 1)
E                the Persian name of weekday (e.g. شنبه)
e                the Persian short name of weekday (e.g. ش)
A                the Persian name of 12-Hour marker (e.g. قبل از ظهر)
a                the Persian short name of 12-Hour marker (e.g. ق.ظ)
HH               2-digits representation of hour [00-23]
H                hour [0-23]
kk               2-digits representation of hour [01-24]
k                hour [1-24]
hh               2-digits representation of hour [01-12]
h                hour [1-12]
KK               2-digits representation of hour [00-11]
K                hour [0-11]
mm               2-digits representation of minute [00-59]
m                minute [0-59]
ss               2-digits representation of seconds [00-59]
s                seconds [0-59]
n				 hour name (e.g. صبح)
ns               nanoseconds
S                3-digits representation of milliseconds (e.g. 001)
z                the name of location
Z                zone offset (e.g. +03:30)

func (Time) Hour

func (t Time) Hour() int

Hour returns the hour of t in the range [0, 23].

func (Time) Hour12

func (t Time) Hour12() int

Hour12 returns the hour of t in the range [0, 11].

func (Time) In

func (t Time) In(loc *time.Location) Time

In sets the location of t.

loc is a pointer to time.Location and must not be nil.

func (Time) IsLeap

func (t Time) IsLeap() bool

IsLeap returns true if the year of t is a leap year.

func (Time) IsZero added in v1.1.5

func (t Time) IsZero() bool

IsZero returns true if t is zero time instance

func (Time) LastMonthDay

func (t Time) LastMonthDay() Time

LastMonthDay returns a new instance of Time representing the last day of the month of t.

func (Time) LastWeekday

func (t Time) LastWeekday() Time

LastWeekday returns a new instance of Time representing the last day of the week of t.

func (Time) LastYearDay

func (t Time) LastYearDay() Time

LastYearDay returns a new instance of Time representing the last day of the year of t.

func (Time) Location

func (t Time) Location() *time.Location

Location returns a pointer to time.Location of t.

func (Time) Minute

func (t Time) Minute() int

Minute returns the minute offset of t in the range [0, 59].

func (Time) Month

func (t Time) Month() Month

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

func (Time) MonthWeek

func (t Time) MonthWeek() int

MonthWeek returns the week of month of t.

func (Time) Nanosecond

func (t Time) Nanosecond() int

Nanosecond returns the nanoseconds offset of t in the range [0, 999999999].

func (Time) RMonthDay

func (t Time) RMonthDay() int

RMonthDay returns the number of remaining days of the month of t.

func (Time) RYearDay

func (t Time) RYearDay() int

RYearDay returns the number of remaining days of the year of t.

func (Time) RYearWeek

func (t Time) RYearWeek() int

RYearWeek returns the number of remaining weeks of the year of t.

func (Time) Second

func (t Time) Second() int

Second returns the seconds offset of t in the range [0, 59].

func (*Time) Set

func (t *Time) Set(year int, month Month, day, hour, min, sec, nsec int, loc *time.Location)

Set sets t.

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 and must not be nil.

func (*Time) SetDay

func (t *Time) SetDay(day int)

SetDay sets the day of t.

func (*Time) SetHour

func (t *Time) SetHour(hour int)

SetHour sets the hour of t.

func (*Time) SetMinute

func (t *Time) SetMinute(min int)

SetMinute sets the minute offset of t.

func (*Time) SetMonth

func (t *Time) SetMonth(month Month)

SetMonth sets the month of t.

func (*Time) SetNanosecond

func (t *Time) SetNanosecond(nsec int)

SetNanosecond sets the nanosecond offset of t.

func (*Time) SetSecond

func (t *Time) SetSecond(sec int)

SetSecond sets the second offset of t.

func (*Time) SetTime

func (t *Time) SetTime(ti time.Time)

SetTime sets t to the time of ti.

func (*Time) SetUnix

func (t *Time) SetUnix(sec, nsec int64)

SetUnix sets t to represent the corresponding unix timestamp of

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

func (*Time) SetYear

func (t *Time) SetYear(year int)

SetYear sets the year of t.

func (Time) Since

func (t Time) Since(t2 Time) int64

Since returns the number of seconds between t and t2.

func (Time) String

func (t Time) String() string

String returns t in RFC3339Nano format.

func (Time) Time

func (t Time) Time() time.Time

Time converts Persian date to Gregorian date and returns a new instance of time.Time

func (Time) TimeFormat added in v0.6.0

func (t Time) TimeFormat(format string) string

TimeFormat formats in standard time format.

2006        four digit year (e.g. 1399)
06          two digit year (e.g. 99)
01          two digit month (e.g. 01)
1           one digit month (e.g. 1)
Jan         month name (e.g. آذر)
January     month name (e.g. آذر)
02          two digit day (e.g. 07)
2           one digit day (e.g. 7)
_2          right justified two character day (e.g.  7)
Mon         weekday (e.g. شنبه)
Monday      weekday (e.g. شنبه)
Morning     hour name (e.g. صبح)
03          two digit 12 hour format (e.g. 03)
3           one digit 12 hour format (e.g. 3)
15          two digit 24 hour format (e.g. 15)
04          two digit minute (e.g. 03)
4           one digit minute (e.g. 03)
05          two digit minute (e.g. 09)
5           one digit minute (e.g. 9)
.000        millisecond (e.g. .120)
.000000     microsecond (e.g. .123400)
.000000000  nanosecond (e.g. .123456000)
.999        trailing zeros removed millisecond (e.g. .12)
.999999     trailing zeros removed microsecond (e.g. .1234)
.999999999  trailing zeros removed nanosecond (e.g. .123456)
PM          full 12-Hour marker (e.g. قبل از ظهر)
pm          short 12-Hour marker (e.g. ق.ظ)
MST         the name of location
-0700       zone offset (e.g. +0330)
-07         zone offset (e.g. +03)
-07:00      zone offset (e.g. +03:30)
Z0700       zone offset (e.g. +0330)
Z07:00      zone offset (e.g. +03:30)

func (Time) Tomorrow

func (t Time) Tomorrow() Time

Tomorrow returns a new instance of Time representing a day after the day of t.

func (Time) Unix

func (t Time) Unix() int64

Unix returns the number of seconds since January 1, 1970 UTC.

func (Time) UnixNano

func (t Time) UnixNano() int64

UnixNano seturns the number of nanoseconds since January 1, 1970 UTC.

func (Time) Weekday

func (t Time) Weekday() Weekday

Weekday returns the weekday of t.

func (Time) Year

func (t Time) Year() int

Year returns the year of t.

func (Time) YearDay

func (t Time) YearDay() int

YearDay returns the day of year of t.

func (Time) YearWeek

func (t Time) YearWeek() int

YearWeek returns the week of year of t.

func (Time) Yesterday

func (t Time) Yesterday() Time

Yesterday returns a new instance of Time representing a day before the day of t.

func (Time) Zone

func (t Time) Zone() (string, int)

Zone returns the zone name and its offset in seconds east of UTC of t.

func (Time) ZoneOffset

func (t Time) ZoneOffset(f ...string) string

ZoneOffset returns the zone offset of t in the format of [+|-]HH:mm. If `f` is set, then return format is based on `f`.

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

Jump to

Keyboard shortcuts

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