jtime

package module
v0.0.0-...-08c9432 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2022 License: MIT Imports: 3 Imported by: 0

README

JTIME - GO

A go library for calculating the jalaali (shamsi) date.

You can calculating jalaali date with this library.

  • Supports JTime.Format(...) function like time.Time.

EXAMPLE

// jtime.FromTime(time.Now())
var start jtime.JTime = jtime.Now()

for i := 0; i < 10; i++ {
    // ...
}

// jtime.Now().Sub(j)
var end time.Duration = jtime.Since(start)

fmt.Println(end)

Install it

go get -u github.com/aWolver/jtime

Useful Functions:

FromTime returns the t Jalaali time

func FromTime(t time.Time) JTime

Now Now returns the current Jalaali time. it is shorthand for jtime.FromTime(time.Now()).

func Now() JTime

Convert Convert converts gregorian date to jalaali date, or jalaali date to gregorian date.

func Convert(year, month, day int, to convertTo) (toyear, tomonth, today int)

Date Date returns the Time corresponding to yyyy-mm-dd hh:mm:ss + nsec nanoseconds

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

JTime Type:

type JTime struct {...}

JTime functions like time.Time and more new functions.

  • Format
  • AppendFormat
  • Sub
  • Date
  • Add
  • AddDate
  • Before
  • Equal
  • YearDay
  • WeekYear
  • ...

NOTE
Convert(..., jtime.ToJalaali) have a bug on YYYY-03-20 date's. often returns (day + 1) on non-leap years.

Documentation

Index

Constants

View Source
const (
	ToJalaali convertTo = 1 << iota
	ToGregorian
)
View Source
const (
	Layout  = "01/02 03:04:05 '06"
	ANSIC   = "Monday January 02 15:04:05 2006"
	RFC822  = "02 January 06 15:04"
	RFC850  = "Monday, 02-January-06 15:04:05"
	RFC1123 = "Monday, 02 January 2006 15:04:05"
	Kitchen = "03:04"
	Stamp   = "January 02 15:04:05"
)

These are predefined layouts for use in Time.Format and time.Parse.

Year: "2006" "06"
Month: "January"
Textual day of the week: "Monday"
Numeric day of the month: "2" "02"
Numeric day of the year: "002"
Hour: "15" "03"
Minute: "4" "04"
Second: "5" "05"

Variables

View Source
var (
	TehranTimeZone, _ = time.LoadLocation("Asia/Tehran")
)

Asia/Tehran timezone

Functions

func Convert

func Convert(year, month, day int, to convertTo) (toyear, tomonth, today int)

Convert converts gregorian date to jalaali date, or jalaali date to gregorian date.

Convert gregorian date to Jalaali date:

Convert(2022, 4, 28, jtime.ToJalaali)

Convert Jalaali date to gregorian date:

Convert(1401, 2, 8, jtime.ToGregorian)

returns 0, 0, 0 if 'to' parameter is invalid.

func FromJTime

func FromJTime(t JTime) time.Time

FromJTime returns the t gregorian time

func Since

func Since(j JTime) time.Duration

it is shorthand for jtime.Now().Sub(j)

Types

type JTime

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

A JDate represents an instant in time with nanosecond precision.

func Date

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

Date returns the Time corresponding to

yyyy-mm-dd hh:mm:ss + nsec nanoseconds

If loc is nil, use time.Local

func FromTime

func FromTime(t time.Time) JTime

FromTime returns the t Jalaali time

func Now

func Now() JTime

Now returns the current Jalaali time

it is shorthand for jtime.FromTime(time.Now())

func Parse

func Parse(layout, value string) (JTime, error)

Parse uses time.Parse to parse formatted value.

NOTE: Parse cannots parse jtime formatted strings.

It is shorthand for jtime.FromTime(time.Parse(...))

func (JTime) Add

func (j JTime) Add(d time.Duration) JTime

Add returns the time j+d.

func (JTime) AddDate

func (j JTime) AddDate(year, month, day int) JTime

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

func (JTime) AppendFormat

func (j JTime) AppendFormat(b []byte, layout string) []byte

AppendFormat is like Format but appends the textual representation to b and returns the extended buffer.

func (JTime) Before

func (j JTime) Before(u JTime) bool

Before reports whether the time instant j is before u.

func (JTime) Date

func (j JTime) Date() (year, month, day int)

Date returns the year, month, and day in which j occurs.

func (JTime) Day

func (j JTime) Day() int

Day returns the day in which j occurs.

func (JTime) Equal

func (j JTime) Equal(u JTime) bool

Equal reports whether t and u represent the same time instant. Two times can be equal even if they are in different locations. For example, 6:00 +0200 and 4:00 UTC are Equal. See the documentation on the Time type for the pitfalls of using == with Time values; most code should use Equal instead.

func (JTime) Format

func (j JTime) Format(layout string) string

Format returns a textual representation of the time value formatted according to the layout defined by the argument.

Supported Layout's:

Year: "2006" "06"
Month: "January"
Textual day of the week: "Monday"
Numeric day of the month: "2" "02"
Numeric day of the year: "002"
Hour: "15" "03"
Minute: "4" "04"
Second: "5" "05"

func (JTime) Hour

func (j JTime) Hour() int

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

func (JTime) ISOWeekday

func (j JTime) ISOWeekday() Weekday

ISOWeekday returns j.Weekday() + 1.

func (JTime) In

func (j JTime) In(loc *time.Location) JTime

In returns a copy of t representing the same time instant, but with the copy's location information set to loc for display purposes.

In panics if loc is nil.

func (JTime) IsLeap

func (j JTime) IsLeap() bool

IsLeap returns true if year is leap

func (JTime) Location

func (j JTime) Location() *time.Location

Location returns the time zone information associated with j.

func (JTime) Minute

func (j JTime) Minute() int

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

func (JTime) Month

func (j JTime) Month() Month

Month returns the month in which j occurs.

func (JTime) Second

func (j JTime) Second() int

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

func (JTime) String

func (j JTime) String() string

String returns the time formatted using the format string

"2006-01-02 15:04:05"

func (JTime) Sub

func (j JTime) Sub(u JTime) time.Duration

Sub returns the duration t-u. If the result exceeds the maximum (or minimum) value that can be stored in a Duration, the maximum (or minimum) duration will be returned. To compute j-d for a duration d, use j.Add(-d).

func (JTime) SubDuration

func (j JTime) SubDuration(d time.Duration) JTime

func (JTime) Time

func (j JTime) Time() time.Time

Time returns gregorian time of j

func (JTime) Unix

func (j JTime) Unix() int64

Unix returns j as a Unix time

func (JTime) Weekday

func (j JTime) Weekday() Weekday

Weekday returns the day of the week specified by j.

func (JTime) Year

func (j JTime) Year() int

Year returns the year in which j occurs.

func (JTime) YearDay

func (j JTime) YearDay() int

YearDay returns the day of the year specified by j, in the range [1,365] for non-leap years, and [1,366] in leap years.

func (JTime) YearWeek

func (j JTime) YearWeek() int

YearWeek returns the week number in which j occurs.

type Month

type Month int8

A Month specifies a month of the year (فروردین = 1, ...)

const (
	Farvardin   Month = 1
	Ordibehesht Month = 2
	Khordad     Month = 3
	Tir         Month = 4
	Mordad      Month = 5
	Shahrivar   Month = 6
	Mehr        Month = 7
	Aban        Month = 8
	Azar        Month = 9
	Dey         Month = 10
	Bahman      Month = 11
	Esfand      Month = 12
)

Month of the Jalaali year

func (Month) String

func (m Month) String() string

type Weekday

type Weekday int8

A Weekday specifies a day of the week (شنبه = 0, ...).

const (
	Shanbeh       Weekday = 0
	YekShanbeh    Weekday = 1
	DoShanbeh     Weekday = 2
	SeShanbeh     Weekday = 3
	CheharShanbeh Weekday = 4
	PanjShanbeh   Weekday = 5
	Jomeh         Weekday = 6
)

Day of the Jalaali week

func (Weekday) String

func (w Weekday) String() string

Jump to

Keyboard shortcuts

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