Documentation
¶
Overview ¶
Package nepdate converts between the Bikram Sambat (Nepali) and Gregorian calendars, and formats, parses and does arithmetic on BS dates.
The supported range is BS 1901-01-01 to 2199-12-30 (AD 1844-04-11 to 2143-04-15). Dates outside it return ErrOutOfRange. The zero Date is "no date": IsZero reports true and every operation that can fail returns ErrOutOfRange.
Import it as
import nepdate "github.com/sanamhub/nepdate-go"
Tithi, public holidays and events live in the separate package github.com/sanamhub/nepdate-go/patro. This package never imports it, so a program that only converts dates links none of the patro data.
A Date marshals to JSON as "YYYY-MM-DD". Marshalling the zero Date returns ErrOutOfRange, so an optional date should be a *Date: nil marshals as null. Unmarshalling null is rejected with ErrOutOfRange for the same reason.
The zero Date is the only invalid value the type can hold, and it means "no date". Its methods never invent a date:
- IsZero reports true, and the accessors Year, Month, Day, YearDay, MonthLength, YearLength, YMD and Quarter return 0, Gregorian returns 0, 0, 0, Month returns 0, and String, Short, Long and Format return "".
- Operations that return an error return ErrOutOfRange: AddDays, AddMonths, AddYears, MarshalText, AppendText, MarshalJSON, UnmarshalJSON, and ToEnglishDates.
- IsToday, IsYesterday and IsTomorrow report false.
- The zero Range is empty, and the range methods return the zero of their result type.
Use IsZero before an accessor when 0 would be ambiguous. A valid date is never zero, so a plain comparison d == nepdate.Date{} is also correct.
Index ¶
- Variables
- func ToEnglishDates(dates []Date) ([]time.Time, error)
- func ToEnglishDatesFromStrings(texts []string) ([]time.Time, error)
- func WeekdayName(w time.Weekday, lang Lang) string
- func WeekdayShortName(w time.Weekday, lang Lang) string
- type Date
- func FromGregorian(year int, month time.Month, day int) (Date, error)
- func FromTime(t time.Time) (Date, error)
- func FromYMD(n int) (Date, error)
- func MustNew(year int, month Month, day int) Date
- func New(year int, month Month, day int) (Date, error)
- func Parse(s string) (Date, error)
- func ParseLenient(s string) (Date, error)
- func ToNepaliDates(times []time.Time) ([]Date, error)
- func Today(loc *time.Location) (Date, error)
- func TryParse(s string) (Date, bool)
- func TryParseLenient(s string) (Date, bool)
- func (d Date) AddDays(n int) (Date, error)
- func (d Date) AddMonths(n int, o Overflow) (Date, error)
- func (d Date) AddYears(n int, o Overflow) (Date, error)
- func (d Date) After(other Date) bool
- func (d Date) AppendFormat(b []byte, pattern string, lang Lang) []byte
- func (d Date) AppendLong(b []byte, opts LongOptions, lang Lang) []byte
- func (d Date) AppendShort(b []byte, o DateOrder, sep Separator, pad bool, lang Lang) []byte
- func (d Date) AppendText(b []byte) ([]byte, error)
- func (d Date) Before(other Date) bool
- func (d Date) Compare(other Date) int
- func (d Date) Day() int
- func (d Date) DaysUntil(other Date) int
- func (d Date) Diff(other Date) Diff
- func (d Date) FirstOfMonth() Date
- func (d Date) FiscalYear() FiscalYear
- func (d Date) Format(pattern string, lang Lang) string
- func (d Date) Gregorian() (year int, month time.Month, day int)
- func (d Date) IsToday(loc *time.Location) bool
- func (d Date) IsTomorrow(loc *time.Location) bool
- func (d Date) IsYesterday(loc *time.Location) bool
- func (d Date) IsZero() bool
- func (d Date) LastOfMonth() Date
- func (d Date) Long(opts LongOptions, lang Lang) string
- func (d Date) MarshalJSON() ([]byte, error)
- func (d Date) MarshalText() ([]byte, error)
- func (d Date) Month() Month
- func (d Date) MonthLength() int
- func (d Date) Next() (Date, bool)
- func (d Date) Prev() (Date, bool)
- func (d Date) Quarter() Quarter
- func (d Date) Short(o DateOrder, sep Separator, pad bool, lang Lang) string
- func (d Date) String() string
- func (d Date) Time(loc *time.Location) time.Time
- func (d *Date) UnmarshalJSON(data []byte) error
- func (d *Date) UnmarshalText(text []byte) error
- func (d Date) Weekday() time.Weekday
- func (d Date) YMD() int
- func (d Date) Year() int
- func (d Date) YearDay() int
- func (d Date) YearLength() int
- type DateOrder
- type Diff
- type FiscalYear
- type Lang
- type LongOptions
- type Month
- type Overflow
- type ParseError
- type ParseErrorKind
- type Quarter
- type Range
- func CurrentCalendarYear(loc *time.Location) (Range, error)
- func CurrentFiscalYear(loc *time.Location) (Range, error)
- func CurrentMonth(loc *time.Location) (Range, error)
- func FromDayCount(start Date, days int) (Range, error)
- func MonthRange(year int, month Month) (Range, error)
- func NewRange(start, end Date) (Range, bool)
- func SingleDay(d Date) Range
- func YearRange(year int) (Range, error)
- func (r Range) Contains(d Date) bool
- func (r Range) ContainsRange(other Range) bool
- func (r Range) DatesWithInterval(interval int) (iter.Seq[Date], error)
- func (r Range) Days() iter.Seq[Date]
- func (r Range) End() Date
- func (r Range) Except(other Range) []Range
- func (r Range) Format(o DateOrder, sep Separator, lang Lang) string
- func (r Range) Intersect(other Range) (Range, bool)
- func (r Range) IsAdjacentTo(other Range) bool
- func (r Range) Len() int
- func (r Range) Overlaps(other Range) bool
- func (r Range) SplitByFiscalQuarter() []Range
- func (r Range) SplitByMonth() []Range
- func (r Range) Start() Date
- func (r Range) String() string
- func (r Range) Union(other Range) Range
- func (r Range) WeekendDays(includeSunday bool) iter.Seq[Date]
- func (r Range) WorkingDays(excludeSunday bool) iter.Seq[Date]
- type Separator
Examples ¶
- CurrentCalendarYear
- CurrentFiscalYear
- CurrentMonth
- Date
- Date.AddDays
- Date.AddMonths
- Date.AddYears
- Date.AppendLong
- Date.AppendShort
- Date.DaysUntil
- Date.Diff
- Date.Format
- Date.Long
- Date.MarshalText
- Date.Short
- Date.UnmarshalText
- Date.YearLength
- DateOrder
- Diff
- FiscalYear
- FiscalYear.QuarterRange
- FiscalYearOf
- FromDayCount
- FromGregorian
- FromTime
- FromYMD
- Lang
- LongOptions
- Month
- MonthRange
- MustNew
- New
- NewRange
- Overflow
- Parse
- ParseError
- ParseErrorKind
- ParseLenient
- Quarter
- Range
- Range.Days
- Separator
- SingleDay
- ToEnglishDates
- ToEnglishDatesFromStrings
- ToNepaliDates
- Today
- TryParse
- TryParseLenient
- WeekdayName
- WeekdayShortName
- YearRange
Constants ¶
This section is empty.
Variables ¶
var ( Min = Date{1901, 1, 1} Max = Date{2199, 12, 30} )
Min and Max are the first and last supported dates, BS 1901-01-01 and 2199-12-30.
var ( // ErrOutOfRange reports a date outside BS 1901-01-01..2199-12-30, a Gregorian date outside // AD 1844-04-11..2143-04-15, an operation whose result leaves the range, or the zero Date. ErrOutOfRange = errors.New("nepdate: date outside BS 1901-01-01..2199-12-30") // ErrInvalidMonth reports a month outside 1..12. ErrInvalidMonth = errors.New("nepdate: month must be 1..12") // ErrInvalidDay reports a day outside 1 to the length of its month. ErrInvalidDay = errors.New("nepdate: day outside the month") // ErrInvalidGregorian reports an AD date that does not exist, such as 2023-02-29. ErrInvalidGregorian = errors.New("nepdate: invalid Gregorian date") )
Sentinel errors. Test for them with errors.Is.
Functions ¶
func ToEnglishDates ¶
ToEnglishDates converts every date to midnight UTC on its Gregorian day, keeping the input order. It returns ErrOutOfRange for the zero Date. It allocates the result slice and nothing else.
Example ¶
package main
import (
"fmt"
"log"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
dates := []nepdate.Date{
nepdate.MustNew(2081, nepdate.Shrawan, 15),
nepdate.MustNew(2081, nepdate.Shrawan, 16),
}
times, err := nepdate.ToEnglishDates(dates)
if err != nil {
log.Fatal(err)
}
fmt.Println(times[0].Format("2006-01-02"), times[1].Format("2006-01-02"))
}
Output: 2024-07-30 2024-07-31
func ToEnglishDatesFromStrings ¶
ToEnglishDatesFromStrings parses every string with Parse and converts it to midnight UTC on its Gregorian day, keeping the input order. It stops at the first invalid string and returns nil with that error. It allocates the result slice and, on a failed parse, the parse error.
Example ¶
package main
import (
"fmt"
"log"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
times, err := nepdate.ToEnglishDatesFromStrings([]string{"2081-04-15", "2081-04-16"})
if err != nil {
log.Fatal(err)
}
fmt.Println(times[0].Format("2006-01-02"), times[1].Format("2006-01-02"))
}
Output: 2024-07-30 2024-07-31
func WeekdayName ¶
WeekdayName returns the full name of w in lang, or "" if w is not Sunday..Saturday or lang is unknown. It does not allocate.
Example ¶
package main
import (
"fmt"
"time"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
fmt.Println(nepdate.WeekdayName(time.Tuesday, nepdate.English))
fmt.Println(nepdate.WeekdayName(time.Tuesday, nepdate.Nepali))
}
Output: Tuesday मङ्गलवार
func WeekdayShortName ¶
WeekdayShortName returns the short name of w in lang ("Tue", "मङ्गल"), or "" if w is not Sunday..Saturday or lang is unknown. It does not allocate.
Example ¶
package main
import (
"fmt"
"time"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
fmt.Println(nepdate.WeekdayShortName(time.Tuesday, nepdate.English))
fmt.Println(nepdate.WeekdayShortName(time.Tuesday, nepdate.Nepali))
}
Output: Tue मङ्गल
Types ¶
type Date ¶
type Date struct {
// contains filtered or unexported fields
}
Date is a Bikram Sambat calendar date. It is a small comparable value: use == and as a map key. The zero value is "no date" (PARITY D-01): IsZero reports true, accessors return 0, and every operation that returns an error returns ErrOutOfRange.
Example ¶
package main
import (
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d := nepdate.MustNew(2081, nepdate.Shrawan, 15)
y, m, day := d.Gregorian()
fmt.Println(d.Year(), d.Month(), d.Day(), d.Weekday())
fmt.Println(y, int(m), day)
}
Output: 2081 Shrawan 15 Tuesday 2024 7 30
func FromGregorian ¶
FromGregorian returns the BS date of the AD date year-month-day. It returns ErrInvalidGregorian if that AD date does not exist (2023-02-29) and ErrOutOfRange if it is outside AD 1844-04-11..2143-04-15, checked in that order (ALGORITHM §3). It does not allocate.
Example ¶
package main
import (
"fmt"
"log"
"time"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d, err := nepdate.FromGregorian(2024, time.August, 16)
if err != nil {
log.Fatal(err)
}
fmt.Println(d)
}
Output: 2081/04/32
func FromTime ¶
FromTime returns the BS date of t's calendar date in t's own location. FromTime(t) and FromTime(t.UTC()) can differ by a day: Nepal is UTC+05:45, so 2024-07-30 18:30 UTC is already 2024-07-31 in Kathmandu (BS 2081-04-16, against 2081-04-15 in UTC). Convert t with In first to get the date somewhere else. It returns ErrOutOfRange outside AD 1844-04-11..2143-04-15. It does not allocate.
Example ¶
package main
import (
"fmt"
"log"
"time"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d, err := nepdate.FromTime(time.Date(2024, time.August, 16, 0, 0, 0, 0, time.UTC))
if err != nil {
log.Fatal(err)
}
fmt.Println(d)
}
Output: 2081/04/32
func FromYMD ¶
FromYMD returns the date of an integer in YYYYMMDD form, so 20810415 is 2081-04-15. It returns New's errors: ErrOutOfRange for a year outside 1901..2199, ErrInvalidMonth for a month outside 1..12 and ErrInvalidDay for a day outside the month. It does not allocate.
Example ¶
package main
import (
"fmt"
"log"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d, err := nepdate.FromYMD(20810415)
if err != nil {
log.Fatal(err)
}
fmt.Println(d, d.YMD())
}
Output: 2081/04/15 20810415
func MustNew ¶
MustNew is New for constants and tests. It panics with New's error.
Example ¶
package main
import (
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d := nepdate.MustNew(2081, nepdate.Shrawan, 15)
fmt.Println(d)
}
Output: 2081/04/15
func New ¶
New returns the date year-month-day. It checks the year first, then the month, then the day (ALGORITHM §3), and returns ErrOutOfRange for a year outside 1901..2199, ErrInvalidMonth for a month outside 1..12 and ErrInvalidDay for a day outside the month. It does not allocate.
Example ¶
package main
import (
"fmt"
"log"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d, err := nepdate.New(2081, nepdate.Shrawan, 32)
if err != nil {
log.Fatal(err)
}
y, m, day := d.Gregorian()
fmt.Println(y, int(m), day)
}
Output: 2024 8 16
func Parse ¶
Parse reads a date in the strict form of shared/spec/PARSING.md §1: year, month and day in that order, ASCII or Devanagari digits (they may be mixed, PARITY D-05), at most 4 digits per number, separated by any of - / . _ \ space । |. It returns a *ParseError when the text is not in that form, and New's error when it names an invalid date. It does not allocate on success; a failed call allocates one *ParseError.
Example ¶
package main
import (
"fmt"
"log"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d, err := nepdate.Parse("2081-04-15")
if err != nil {
log.Fatal(err)
}
fmt.Println(d, d.Weekday())
}
Output: 2081/04/15 Tuesday
func ParseLenient ¶
ParseLenient reads a date written by a person (shared/spec/PARSING.md §2), such as "15 Shrawan 2080", "Shrawan 15, 2080", "१५ श्रावण २०८०" or "15.04.2080". It tries Parse first. Otherwise it splits the text into numbers and words, drops era markers (BS, वि.सं.) and the words गते and मिति, and accepts a closed list of month names (PARSING §3) or three numbers with the 4-digit year first or last. It never guesses (PARITY D-06): a 2-digit year is Ambiguous. It returns a *ParseError when no rule matches, and New's error when the date is invalid. It does not allocate on success; a failed call allocates one *ParseError.
Example ¶
package main
import (
"fmt"
"log"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d, err := nepdate.ParseLenient("15 Shrawan 2080")
if err != nil {
log.Fatal(err)
}
fmt.Println(d)
}
Output: 2080/04/15
func ToNepaliDates ¶
ToNepaliDates converts every time to its BS date in the time's own location, keeping the input order. It stops at the first time outside AD 1844-04-11..2143-04-15 and returns nil with that error. It allocates the result slice and nothing else.
Example ¶
package main
import (
"fmt"
"log"
"time"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
times := []time.Time{
time.Date(2024, time.July, 30, 0, 0, 0, 0, time.UTC),
time.Date(2024, time.July, 31, 0, 0, 0, 0, time.UTC),
}
dates, err := nepdate.ToNepaliDates(times)
if err != nil {
log.Fatal(err)
}
fmt.Println(dates)
}
Output: [2081/04/15 2081/04/16]
func Today ¶
Today returns today's BS date in loc. A nil loc means Asia/Kathmandu, or the fixed zone NPT (UTC+05:45) when the system has no time zone database; the library doesn't embed one. A server clock in UTC is 5 h 45 min behind Nepal, so pass the zone you mean. It returns ErrOutOfRange if the clock is outside the supported range.
Example ¶
package main
import (
"fmt"
"log"
"time"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
today, err := nepdate.Today(time.FixedZone("NPT", 5*3600+45*60))
if err != nil {
log.Fatal(err)
}
// The date depends on the clock; the time of the same date does not.
fmt.Println(today.Time(nil).Format("15:04"))
}
Output: 00:00
func TryParse ¶
TryParse is Parse without an error: it returns the date and true, or the zero Date and false when the text is not in the strict form or names an invalid date.
Example ¶
package main
import (
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d, ok := nepdate.TryParse("2081/04/15")
fmt.Println(d, ok)
d, ok = nepdate.TryParse("2081/04")
fmt.Println(d.IsZero(), ok)
}
Output: 2081/04/15 true true false
func TryParseLenient ¶
TryParseLenient is ParseLenient without an error: it returns the date and true, or the zero Date and false when the input is not a date.
Example ¶
package main
import (
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d, ok := nepdate.TryParseLenient("15 Shrawan 2081")
fmt.Println(d, ok)
}
Output: 2081/04/15 true
func (Date) AddDays ¶
AddDays returns d moved by n days. It returns ErrOutOfRange if the result is outside the supported range or d is the zero Date. It does not allocate.
Example ¶
package main
import (
"fmt"
"log"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d := nepdate.MustNew(2081, nepdate.Shrawan, 15)
later, err := d.AddDays(16)
if err != nil {
log.Fatal(err)
}
fmt.Println(later)
}
Output: 2081/04/31
func (Date) AddMonths ¶
AddMonths returns d moved by n months, with o deciding what happens to a day past the end of the target month. It returns ErrOutOfRange if the result is outside the supported range or d is the zero Date. It does not allocate.
Example ¶
package main
import (
"fmt"
"log"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d := nepdate.MustNew(2081, nepdate.Shrawan, 15)
later, err := d.AddMonths(4, nepdate.Clamp)
if err != nil {
log.Fatal(err)
}
fmt.Println(later)
}
Output: 2081/08/15
func (Date) AddYears ¶
AddYears returns d moved by n years, which is AddMonths(12*n, o). It returns ErrOutOfRange like AddMonths. It does not allocate.
Example ¶
package main
import (
"fmt"
"log"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d := nepdate.MustNew(2080, nepdate.Falgun, 30)
later, err := d.AddYears(1, nepdate.Clamp)
if err != nil {
log.Fatal(err)
}
fmt.Println(later)
}
Output: 2081/11/29
func (Date) AppendFormat ¶
AppendFormat is Format appending to b. It does not allocate when b has room.
func (Date) AppendLong ¶
func (d Date) AppendLong(b []byte, opts LongOptions, lang Lang) []byte
AppendLong is Long appending to b. It does not allocate when b has room.
Example ¶
package main
import (
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d, _ := nepdate.New(2081, nepdate.Shrawan, 15)
var buf []byte
buf = d.AppendLong(buf, nepdate.LongOptions{Weekday: true}, nepdate.Nepali)
fmt.Println(string(buf))
}
Output: मङ्गलवार, साउन १५, २०८१
func (Date) AppendShort ¶
AppendShort is Short appending to b. It does not allocate when b has room.
Example ¶
package main
import (
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d := nepdate.MustNew(2081, nepdate.Shrawan, 15)
var b []byte
b = d.AppendShort(b, nepdate.YMD, nepdate.Dot, true, nepdate.English)
fmt.Println(string(b))
}
Output: 2081.04.15
func (Date) AppendText ¶
AppendText appends "YYYY-MM-DD" to b (encoding.TextAppender). It does not allocate when b has room, and returns b unchanged with ErrOutOfRange for the zero Date.
func (Date) Compare ¶
Compare returns -1 if d is before other, 0 if they are equal and +1 if d is after other. The zero Date sorts before every valid date. It does not allocate.
func (Date) Day ¶
Day returns the day of the month, 1..32, or 0 for the zero Date. It does not allocate.
func (Date) DaysUntil ¶
DaysUntil returns the signed number of days from d to other (PARITY D-07): positive when other is later. It returns 0 if either date is the zero Date. It does not allocate.
Example ¶
package main
import (
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
start := nepdate.MustNew(2081, nepdate.Baishakh, 1)
end := nepdate.MustNew(2081, nepdate.Shrawan, 15)
fmt.Println(start.DaysUntil(end))
}
Output: 108
func (Date) Diff ¶
Diff returns the years, months and days from d to other: the largest whole number of months that, added to the earlier date with Clamp, doesn't pass the later one, then the remaining days. It returns the zero Diff if either date is the zero Date. It does not allocate.
Example ¶
package main
import (
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
start := nepdate.MustNew(2081, nepdate.Baishakh, 1)
end := nepdate.MustNew(2081, nepdate.Shrawan, 15)
diff := end.Diff(start)
fmt.Println(diff.Years, diff.Months, diff.Days)
}
Output: 0 3 14
func (Date) FirstOfMonth ¶
FirstOfMonth returns the first day of d's month, or the zero Date for the zero Date. It does not allocate.
func (Date) FiscalYear ¶
func (d Date) FiscalYear() FiscalYear
FiscalYear returns the fiscal year that contains d, or 0 for the zero Date. It does not allocate.
func (Date) Format ¶
Format returns d formatted by pattern (shared/spec/FORMATTING.md §5) in lang, or "" for the zero Date. The single-letter patterns "", "G", "g" and "d" give "YYYY/MM/DD", "D" the default Long format and "s" "YYYY-MM-DD". Any other pattern is read left to right: runs of y, M and d are the year, month and day; '...' and \x are literal; everything else is copied.
Example ¶
package main
import (
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d, _ := nepdate.New(2081, nepdate.Shrawan, 15)
fmt.Println(d.Format("MMM d, yyyy", nepdate.Nepali))
}
Output: साउन १५, २०८१
func (Date) Gregorian ¶
Gregorian returns the AD date of d, or (0, 0, 0) for the zero Date. It does not allocate.
func (Date) IsToday ¶
IsToday reports whether d is today in loc. A nil loc means Asia/Kathmandu, like Today. It returns false for the zero Date and when the clock is outside the supported range.
func (Date) IsTomorrow ¶
IsTomorrow reports whether d is the day after today in loc. A nil loc means Asia/Kathmandu, like Today. It returns false for the zero Date and when the clock is outside the supported range.
func (Date) IsYesterday ¶
IsYesterday reports whether d is the day before today in loc. A nil loc means Asia/Kathmandu, like Today. It returns false for the zero Date and when the clock is outside the supported range.
func (Date) IsZero ¶
IsZero reports whether d is the zero Date, which is "no date". It does not allocate.
func (Date) LastOfMonth ¶
LastOfMonth returns the last day of d's month, or the zero Date for the zero Date. It does not allocate.
func (Date) Long ¶
func (d Date) Long(opts LongOptions, lang Lang) string
Long returns d as "[Weekday, ]Month DD[, YYYY]" in lang (FORMATTING §4), or "" for the zero Date.
Example ¶
package main
import (
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d := nepdate.MustNew(2081, nepdate.Shrawan, 15)
fmt.Println(d.Long(nepdate.LongOptions{Weekday: true}, nepdate.Nepali))
}
Output: मङ्गलवार, साउन १५, २०८१
func (Date) MarshalJSON ¶
MarshalJSON returns d as the JSON string "YYYY-MM-DD" (PARITY D-11). It returns ErrOutOfRange for the zero Date, so an optional date should be a *Date, whose nil value marshals as null.
func (Date) MarshalText ¶
MarshalText returns d as "YYYY-MM-DD" (PARITY D-11), so encoding/json and other encoders write a Date as a string and accept it as a map key. It returns ErrOutOfRange for the zero Date.
Example ¶
package main
import (
"encoding/json"
"fmt"
"log"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d := nepdate.MustNew(2081, nepdate.Shrawan, 15)
payload, err := json.Marshal(struct {
Date nepdate.Date `json:"date"`
}{d})
if err != nil {
log.Fatal(err)
}
fmt.Println(string(payload))
}
Output: {"date":"2081-04-15"}
func (Date) MonthLength ¶
MonthLength returns the number of days in d's month, 29..32, or 0 for the zero Date. It does not allocate.
func (Date) Next ¶
Next returns the day after d, or (Date{}, false) at Max and for the zero Date. It does not allocate.
func (Date) Prev ¶
Prev returns the day before d, or (Date{}, false) at Min and for the zero Date. It does not allocate.
func (Date) Quarter ¶
Quarter returns the fiscal quarter that contains d, or 0 for the zero Date. It does not allocate.
func (Date) Short ¶
Short returns the three numbers of d in order o, separated by sep, in lang's digits. With pad the year has 4 digits and the month and day 2. It returns "" for the zero Date or an unknown order or separator.
Example ¶
package main
import (
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d := nepdate.MustNew(2081, nepdate.Shrawan, 15)
fmt.Println(d.Short(nepdate.DMY, nepdate.Dash, false, nepdate.English))
fmt.Println(d.Short(nepdate.YMD, nepdate.Slash, true, nepdate.Nepali))
}
Output: 15-4-2081 २०८१/०४/१५
func (Date) String ¶
String returns d as "YYYY/MM/DD" with ASCII digits (FORMATTING §2), or "" for the zero Date.
func (Date) Time ¶
Time returns midnight at the start of d in loc. A nil loc means Asia/Kathmandu, the same default as Today, or the fixed zone NPT (UTC+05:45) when the system has no time zone database. Pass time.UTC for midnight UTC. The zero Date returns the zero time.Time.
func (*Date) UnmarshalJSON ¶
UnmarshalJSON sets *d from a JSON string in the strict form Parse accepts. It rejects null with ErrOutOfRange (PARITY D-01) and any non-string value with a *ParseError. On error *d is unchanged.
func (*Date) UnmarshalText ¶
UnmarshalText sets *d from text in the strict form Parse accepts. On error *d is unchanged. It parses the bytes directly, so it does not allocate on success.
Example ¶
package main
import (
"encoding/json"
"fmt"
"log"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
var payload struct {
Date nepdate.Date `json:"date"`
}
if err := json.Unmarshal([]byte(`{"date":"2081-04-15"}`), &payload); err != nil {
log.Fatal(err)
}
fmt.Println(payload.Date)
}
Output: 2081/04/15
func (Date) Weekday ¶
Weekday returns the day of the week of d (ALGORITHM §7). The zero Date returns Sunday, the zero value of time.Weekday. It does not allocate.
func (Date) YMD ¶
YMD returns d as YYYYMMDD, so 2081-04-15 is 20810415, or 0 for the zero Date. It does not allocate.
func (Date) Year ¶
Year returns the BS year, 1901..2199, or 0 for the zero Date. It does not allocate.
func (Date) YearDay ¶
YearDay returns the day of the BS year, 1 for Baishakh 1, or 0 for the zero Date. It does not allocate.
func (Date) YearLength ¶
YearLength returns the number of days in d's BS year, 364..367, or 0 for the zero Date. It is the sum of the twelve month lengths (PARITY D-12); it is not Gregorian leap-ness. It does not allocate.
Example ¶
package main
import (
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d := nepdate.MustNew(2081, nepdate.Baishakh, 1)
fmt.Println(d.YearLength())
}
Output: 366
type DateOrder ¶
type DateOrder uint8
DateOrder is the order of the three numbers in Short (shared/spec/FORMATTING.md §3).
Example ¶
package main
import (
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d := nepdate.MustNew(2081, nepdate.Shrawan, 15)
fmt.Println(d.Short(nepdate.DMY, nepdate.Dash, false, nepdate.English))
fmt.Println(d.Short(nepdate.YMD, nepdate.Dash, true, nepdate.English))
}
Output: 15-4-2081 2081-04-15
type Diff ¶
type Diff struct {
Negative bool // the other date is earlier
// Years, Months and Days are the non-negative magnitudes.
Years, Months, Days int
TotalDays int // the signed day count, the same as DaysUntil
}
Diff is the calendar distance between two dates (ALGORITHM §8a). Years, Months and Days are magnitudes; Negative and TotalDays carry the sign.
Example ¶
package main
import (
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
start := nepdate.MustNew(2081, nepdate.Baishakh, 1)
end := nepdate.MustNew(2081, nepdate.Shrawan, 15)
diff := start.Diff(end)
fmt.Println(diff.Years, diff.Months, diff.Days, diff.TotalDays)
back := end.Diff(start)
fmt.Println(back.Negative, back.TotalDays)
}
Output: 0 3 14 108 true -108
type FiscalYear ¶
type FiscalYear int
FiscalYear is a Nepal fiscal year, Shrawan 1 to the end of Ashad, named by its starting BS year: FY 2082/83 is FiscalYear(2082) (ALGORITHM §8b, PARITY D-08).
Example ¶
package main
import (
"fmt"
"log"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
f := nepdate.MustNew(2081, nepdate.Shrawan, 15).FiscalYear()
start, err := f.Start()
if err != nil {
log.Fatal(err)
}
end, err := f.End()
if err != nil {
log.Fatal(err)
}
fmt.Println(f.Label(nepdate.English), start, end)
}
Output: 2081/82 2081/04/01 2082/03/32
func FiscalYearOf ¶
func FiscalYearOf(d Date) FiscalYear
FiscalYearOf returns the fiscal year that contains d, or 0 for the zero Date. It does not allocate.
Example ¶
package main
import (
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d := nepdate.MustNew(2081, nepdate.Shrawan, 15)
fmt.Println(nepdate.FiscalYearOf(d).Label(nepdate.English))
}
Output: 2081/82
func (FiscalYear) End ¶
func (f FiscalYear) End() (Date, error)
End returns the last day of Ashad of f+1. It returns ErrOutOfRange if that day is outside the range. It does not allocate.
func (FiscalYear) Label ¶
func (f FiscalYear) Label(lang Lang) string
Label returns the usual name of f, "2082/83" in English or "२०८२/८३" in Nepali. It does not allocate.
func (FiscalYear) QuarterRange ¶
func (f FiscalYear) QuarterRange(q Quarter) (Range, error)
QuarterRange returns the days of quarter q of f. It returns ErrOutOfRange if the quarter is outside the supported range, and also if q is not Q1..Q4: ErrOutOfRange is the error for any argument outside the supported domain. It does not allocate.
Example ¶
package main
import (
"fmt"
"log"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
f := nepdate.MustNew(2081, nepdate.Shrawan, 15).FiscalYear()
r, err := f.QuarterRange(nepdate.Q1)
if err != nil {
log.Fatal(err)
}
fmt.Println(r.Start(), r.End(), r.Len())
}
Output: 2081/04/01 2081/06/30 93
func (FiscalYear) Range ¶
func (f FiscalYear) Range() (Range, error)
Range returns every day of f. It returns ErrOutOfRange if either end is outside the range. It does not allocate.
func (FiscalYear) Start ¶
func (f FiscalYear) Start() (Date, error)
Start returns Shrawan 1 of f. It returns ErrOutOfRange if that day is outside the range. It does not allocate.
type Lang ¶
type Lang uint8
Lang selects the language of names and digits in formatted output.
Example ¶
package main
import (
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
fmt.Println(string(nepdate.English.AppendDigits(nil, 2081, 4)))
fmt.Println(string(nepdate.Nepali.AppendDigits(nil, 2081, 4)))
}
Output: 2081 २०८१
The supported languages. English uses ASCII digits, Nepali uses Devanagari digits.
func (Lang) AppendDigits ¶
AppendDigits appends n in decimal to b and returns the extended slice. Nepali writes Devanagari digits (U+0966 to U+096F), English writes ASCII digits. The number is zero-padded to at least minWidth digits; minWidth counts digits, not bytes. A negative n gets a leading '-'. It does not allocate when b has room.
type LongOptions ¶
type LongOptions struct {
Weekday bool // start with the weekday name
OmitYear bool // leave out ", <year>"
NoPad bool // write the day without a leading zero
}
LongOptions changes Long. The zero value is C#'s default: no weekday, year shown, day padded to 2 digits.
Example ¶
package main
import (
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d := nepdate.MustNew(2081, nepdate.Shrawan, 15)
fmt.Println(d.Long(nepdate.LongOptions{}, nepdate.English))
fmt.Println(d.Long(nepdate.LongOptions{Weekday: true, OmitYear: true}, nepdate.English))
}
Output: Shrawan 15, 2081 Tuesday, Shrawan 15
type Month ¶
type Month uint8
Month is a Bikram Sambat month, Baishakh = 1 to Chaitra = 12.
Example ¶
package main
import (
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
fmt.Println(nepdate.Shrawan.Name(nepdate.English))
fmt.Println(nepdate.Shrawan.Name(nepdate.Nepali))
fmt.Println(nepdate.Ashad.ShortName(nepdate.English))
}
Output: Shrawan साउन Asa
const ( Baishakh Month = 1 + iota Jestha Ashad Shrawan Bhadra Ashoj Kartik Mangsir Poush Magh Falgun Chaitra )
The twelve BS months.
func (Month) Name ¶
Name returns the month's full name in lang, or "" if m is not 1..12 or lang is unknown. It does not allocate.
type Overflow ¶
type Overflow uint8
Overflow says what AddMonths and AddYears do when the day doesn't exist in the target month (ALGORITHM §8).
Example ¶
package main
import (
"fmt"
"log"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
last := nepdate.MustNew(2081, nepdate.Shrawan, 32)
clamped, err := last.AddMonths(1, nepdate.Clamp)
if err != nil {
log.Fatal(err)
}
spilled, err := last.AddMonths(1, nepdate.Spill)
if err != nil {
log.Fatal(err)
}
fmt.Println(clamped, spilled)
}
Output: 2081/05/31 2081/06/01
type ParseError ¶
type ParseError struct {
// Input is the text that failed to parse.
Input string
// Kind is the syntax rule that failed.
Kind ParseErrorKind
}
ParseError is returned when the input is not a date in the accepted syntax. A string that parses but names an invalid date returns ErrOutOfRange, ErrInvalidMonth or ErrInvalidDay instead.
Example ¶
package main
import (
"errors"
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
_, err := nepdate.Parse("2081/04/xx")
var parseErr *nepdate.ParseError
if errors.As(err, &parseErr) {
fmt.Println(parseErr.Input)
fmt.Println(err)
}
}
Output: 2081/04/xx nepdate: cannot parse "2081/04/xx": invalid character
func (*ParseError) Error ¶
func (e *ParseError) Error() string
Error returns a message naming the input and the kind.
type ParseErrorKind ¶
type ParseErrorKind uint8
ParseErrorKind says why the syntax step of Parse or ParseLenient failed (shared/spec/PARSING.md §4).
Example ¶
package main
import (
"errors"
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
_, err := nepdate.Parse("2081/04/xx")
var parseErr *nepdate.ParseError
if errors.As(err, &parseErr) {
switch parseErr.Kind {
case nepdate.InvalidCharacter:
fmt.Println("the date contains a character that is not a digit or a separator")
case nepdate.WrongGroupCount:
fmt.Println("the date has the wrong number of parts")
case nepdate.NumberTooLong:
fmt.Println("a number in the date is too long")
case nepdate.Ambiguous:
fmt.Println("the date is ambiguous")
case nepdate.Unrecognized:
fmt.Println("the date is not a recognized form")
}
}
}
Output: the date contains a character that is not a digit or a separator
const ( InvalidCharacter ParseErrorKind = iota + 1 // a character no rule accepts WrongGroupCount // not exactly three groups NumberTooLong // a number with more than 4 digits Ambiguous // the input fits more than one reading Unrecognized // the lenient rules found no date )
The parse error kinds.
type Quarter ¶
type Quarter uint8
Quarter is a quarter of the fiscal year, Q1 = Shrawan to Ashoj up to Q4 = Baishakh to Ashad.
Example ¶
package main
import (
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
fmt.Println(nepdate.Q1.Months())
}
Output: [Shrawan Bhadra Ashoj]
type Range ¶
type Range struct {
// contains filtered or unexported fields
}
Range is an inclusive span of dates, start <= end. The zero Range is empty. Equality is the equality of the two bounds, so == works and a Range can be a map key.
Example ¶
package main
import (
"fmt"
"log"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
start := nepdate.MustNew(2081, nepdate.Shrawan, 1)
end := nepdate.MustNew(2081, nepdate.Shrawan, 3)
r, ok := nepdate.NewRange(start, end)
if !ok {
log.Fatal("start is after end")
}
fmt.Println(r.Start(), r.End(), r.Len(), r.Contains(end))
}
Output: 2081/04/01 2081/04/03 3 true
func CurrentCalendarYear ¶
CurrentCalendarYear returns the days of the BS calendar year that contains today in loc. A nil loc means Asia/Kathmandu, like Today. It returns Today's error when the clock is outside the supported range.
Example ¶
package main
import (
"fmt"
"log"
"time"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
r, err := nepdate.CurrentCalendarYear(time.UTC)
if err != nil {
log.Fatal(err)
}
fmt.Println(r.Len() > 0)
}
Output: true
func CurrentFiscalYear ¶
CurrentFiscalYear returns the days of the fiscal year that contains today in loc. A nil loc means Asia/Kathmandu, like Today. It returns Today's error when the clock is outside the supported range, and ErrOutOfRange when the fiscal year ends after Max.
Example ¶
package main
import (
"fmt"
"log"
"time"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
r, err := nepdate.CurrentFiscalYear(time.UTC)
if err != nil {
log.Fatal(err)
}
fmt.Println(r.Len() > 0)
}
Output: true
func CurrentMonth ¶
CurrentMonth returns the days of the BS month that contains today in loc. A nil loc means Asia/Kathmandu, like Today. It returns Today's error when the clock is outside the supported range.
Example ¶
package main
import (
"fmt"
"log"
"time"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
r, err := nepdate.CurrentMonth(time.UTC)
if err != nil {
log.Fatal(err)
}
fmt.Println(r.Len() > 0)
}
Output: true
func FromDayCount ¶
FromDayCount returns the range of days dates starting at start. It returns ErrOutOfRange if days is below 1, start is the zero Date, or the last day would be after Max. It does not allocate.
Example ¶
package main
import (
"fmt"
"log"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
r, err := nepdate.FromDayCount(nepdate.MustNew(2081, nepdate.Shrawan, 15), 5)
if err != nil {
log.Fatal(err)
}
fmt.Println(r.Start(), r.End(), r.Len())
}
Output: 2081/04/15 2081/04/19 5
func MonthRange ¶
MonthRange returns every day of the given BS month. It returns New's errors for an invalid year or month. It does not allocate.
Example ¶
package main
import (
"fmt"
"log"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
r, err := nepdate.MonthRange(2081, nepdate.Shrawan)
if err != nil {
log.Fatal(err)
}
fmt.Println(r.Start(), r.End(), r.Len())
}
Output: 2081/04/01 2081/04/32 32
func NewRange ¶
NewRange returns the range start..end. It returns false if start is after end or either date is the zero Date. It does not allocate.
Example ¶
package main
import (
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
start := nepdate.MustNew(2081, nepdate.Shrawan, 1)
end := nepdate.MustNew(2081, nepdate.Shrawan, 3)
r, ok := nepdate.NewRange(start, end)
fmt.Println(ok, r.Len())
}
Output: true 3
func SingleDay ¶
SingleDay returns the range that holds only d, or the zero Range for the zero Date. It does not allocate.
Example ¶
package main
import (
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
r := nepdate.SingleDay(nepdate.MustNew(2081, nepdate.Shrawan, 15))
fmt.Println(r.Start(), r.End(), r.Len())
}
Output: 2081/04/15 2081/04/15 1
func YearRange ¶
YearRange returns every day of the BS year, Baishakh 1 to the last day of Chaitra. It returns ErrOutOfRange for a year outside 1901..2199. It does not allocate.
Example ¶
package main
import (
"fmt"
"log"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
r, err := nepdate.YearRange(2081)
if err != nil {
log.Fatal(err)
}
fmt.Println(r.Start(), r.End(), r.Len())
}
Output: 2081/01/01 2081/12/31 366
func (Range) Contains ¶
Contains reports whether d is in r. The zero Date is in no range. It does not allocate.
func (Range) ContainsRange ¶
ContainsRange reports whether r fully contains other. A non-empty range contains the zero Range, and the zero Range contains nothing. It does not allocate.
func (Range) DatesWithInterval ¶
DatesWithInterval returns every interval-th day of r, starting at Start and including End when the steps land on it. It returns ErrOutOfRange when interval is below 1. A nil sequence and an error are returned together, so check the error before ranging. It does not allocate while the sequence is ranged over.
func (Range) Days ¶
Days returns an iterator over the days of r in order. It yields every date through Max when the range ends there (PARITY D-16). It does not allocate when ranged over directly (ADR-0002 §6).
Example ¶
package main
import (
"fmt"
"log"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
start := nepdate.MustNew(2081, nepdate.Shrawan, 1)
end := nepdate.MustNew(2081, nepdate.Shrawan, 3)
r, ok := nepdate.NewRange(start, end)
if !ok {
log.Fatal("start is after end")
}
for d := range r.Days() {
fmt.Println(d)
}
}
Output: 2081/04/01 2081/04/02 2081/04/03
func (Range) End ¶
End returns the last day of r, or the zero Date for the zero Range. It does not allocate.
func (Range) Except ¶
Except returns the parts of r that are not in other, in date order: none, one or two ranges. It does not allocate when the result is nil or one range.
func (Range) Format ¶
Format returns r as its two dates in order o, separated by sep, in lang's digits, with " - " between them. It returns "" for the zero Range or an unknown order or separator.
func (Range) Intersect ¶
Intersect returns the dates in both r and other. It returns false when they do not overlap, so a caller has no empty range to inspect (PARITY D-01, D-16). It does not allocate.
func (Range) IsAdjacentTo ¶
IsAdjacentTo reports whether one range starts the day after the other ends. It returns false at Min and Max, so a valid range never fails because it touches the data end (PARITY D-16). It does not allocate.
func (Range) Len ¶
Len returns the number of days in r, counting both ends, or 0 for the zero Range. It does not allocate.
func (Range) Overlaps ¶
Overlaps reports whether r and other share a date. The zero Range overlaps nothing. It does not allocate.
func (Range) SplitByFiscalQuarter ¶
SplitByFiscalQuarter returns one range per fiscal quarter that r touches, in date order. The first and last pieces are clipped to r, and a range that reaches Max still returns its last piece (PARITY D-16). The result allocates one slice.
func (Range) SplitByMonth ¶
SplitByMonth returns one range per BS month that r touches, in date order. The first and last pieces are clipped to r, and a range that reaches Max still returns its last piece (PARITY D-16). The result allocates one slice.
func (Range) Start ¶
Start returns the first day of r, or the zero Date for the zero Range. It does not allocate.
func (Range) String ¶
String returns r as "start - end" with the default date form, or "" for the zero Range.
func (Range) Union ¶
Union returns the smallest range that covers both r and other. The zero Range adds nothing. It does not allocate.
func (Range) WeekendDays ¶
WeekendDays returns every Saturday in r, and every Sunday when includeSunday is true. It does not allocate while ranged over.
type Separator ¶
type Separator uint8
Separator is the character between the numbers in Short.
Example ¶
package main
import (
"fmt"
nepdate "github.com/sanamhub/nepdate-go"
)
func main() {
d := nepdate.MustNew(2081, nepdate.Shrawan, 15)
fmt.Println(d.Short(nepdate.YMD, nepdate.Dot, true, nepdate.English))
fmt.Println(d.Short(nepdate.YMD, nepdate.Space, true, nepdate.English))
}
Output: 2081.04.15 2081 04 15
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
ci
command
Command ci runs every quality gate of ADR-0004 in order and stops at the first failure.
|
Command ci runs every quality gate of ADR-0004 in order and stops at the first failure. |
|
coreonly
command
Command coreonly converts one date using only package nepdate.
|
Command coreonly converts one date using only package nepdate. |
|
gen
command
Command gen writes the generated tables of package nepdate from shared/data/ and the counts in shared/spec/vectors/patro-summary.json.
|
Command gen writes the generated tables of package nepdate from shared/data/ and the counts in shared/spec/vectors/patro-summary.json. |
|
withpatro
command
Command withpatro prints the English events of BS 2081-04-15.
|
Command withpatro prints the English events of BS 2081-04-15. |
|
Package patro is the Nepali almanac (पात्रो) for package nepdate: the tithi (lunar day), the public holiday flag and the events of a BS date.
|
Package patro is the Nepali almanac (पात्रो) for package nepdate: the tithi (lunar day), the public holiday flag and the events of a BS date. |
|
tools
|
|
|
api-check
command
Command api-check guards the exported API of nepdate and patro.
|
Command api-check guards the exported API of nepdate and patro. |
|
consumer-check
command
Command consumer-check builds a throwaway module outside this repository that imports github.com/sanamhub/nepdate-go through a replace directive to this checkout.
|
Command consumer-check builds a throwaway module outside this repository that imports github.com/sanamhub/nepdate-go through a replace directive to this checkout. |
|
data-status
command
Command data-status reports how long the compiled patro data lasts.
|
Command data-status reports how long the compiled patro data lasts. |
|
fuzz-check
command
Command fuzz-check runs every fuzz target in the module for a fixed budget.
|
Command fuzz-check runs every fuzz target in the module for a fixed budget. |
|
mutation-check
command
Command mutation-check runs Gremlins on the core package or patro and enforces the mutation gate.
|
Command mutation-check runs Gremlins on the core package or patro and enforces the mutation gate. |