Skip to content

Go

Package nepdate, module github.com/sanamhub/nepdate-go. The patro (tithi, public holidays and events) is the package github.com/sanamhub/nepdate-go/patro of the same module; the core never imports it, so a program that only converts dates links no patro data.

Zero dependencies, and zero allocations on conversion, arithmetic, comparison, successful parsing, Append* formatting and patro lookups (measured with -benchmem).

Go 1.24 or later.

Terminal window
go get github.com/sanamhub/nepdate-go@v0.1.0-beta.1
import (
"github.com/sanamhub/nepdate-go" // package nepdate
"github.com/sanamhub/nepdate-go/patro" // only if you need tithi, holidays or events
)

The snippets use fmt, log and time from the standard library, plus the imports above.

Construct a BS date and read its Gregorian date:

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)

Format in Nepali:

d, _ := nepdate.New(2081, nepdate.Shrawan, 15)
fmt.Println(d.Format("MMM d, yyyy", nepdate.Nepali))

Append a long form without allocating:

d, _ := nepdate.New(2081, nepdate.Shrawan, 15)
var buf []byte
buf = d.AppendLong(buf, nepdate.LongOptions{Weekday: true}, nepdate.Nepali)
fmt.Println(string(buf))

Parse what a person wrote:

d, err := nepdate.ParseLenient("15 Shrawan 2080")
if err != nil {
log.Fatal(err)
}
fmt.Println(d)

Convert a Go time:

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)

A Date marshals to JSON as "YYYY-MM-DD". Saving an unset (zero) Date returns an error, so use *Date for an optional field; nil marshals as null.

d, _ := nepdate.New(2081, nepdate.Baishakh, 1)
i := patro.Of(d)
t, _ := i.Tithi()
fmt.Println(t.Name(nepdate.Nepali), i.IsPublicHoliday())
for n := 0; n < i.EventCount(); n++ {
fmt.Println(i.Event(n).Name(nepdate.English))
}

The data covers BS 2077-01-01 to 2084-08-20.

Topic Where
Package docs pkg.go.dev
Every exported identifier docs/API.md
Speed and allocations, with the C# baseline docs/benchmarks.md
What changed CHANGELOG.md
Source sanamhub/nepdate-go