Looking in the rearview mirror

Back in 2005, I released a set controls and date libraries to work with dates, in particular Solar Hijri Calendar (a.k.a Persian calendar).

To quote from Wikipedia:

The Solar Hijri calendar is one of the oldest calendars in the world, as well as the most accurate solar calendar in use today.

The calendar is in use in many countries such as Iran, Afghanistan, Tajikstan (with minor differences in month names).

The library called FarsiLibrary and was originally built on top of .NET Framework 1.1 and then ported to .NET Framework 2.0, which lacked official solar hijro calendar support. The library was published as an article on CodeProject and was a great hit. The Right-To-Left controls were not a common place and the lack of calendar and date calculation was an impediment to multi-cultural business application. In late 2006/early 2007 WPF was the new cool kid around the block, so as a way to learn, I ported my controls over and subsequently published a new set of controls. This was published as another article on Code Project.

Throughout this process, one thing was a common denominator: I leared a lot as I was working on it.

Now, after more than a decade, I have re-written the library, this time in GoLang as a way to help brush up on my Go. The first initial version can be found on my Github repository and will be officially in the following weeks.

Date Functions

Two way date conversions are now possible, using time.Time and PersianDate structs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import (
"github.com/heskandari/farsilibrary.go/date"
"time"
)

func Test() {

// convert time.Now
pd := date.ToPersianDate(time.Now())

// create directly
pd2, err := date.NewPersianDate(1387, 7, 7) //7 Mehr 1387

// same as above, but with parsed strings
pd3, err := date.Parse("1387/7/7")

// convert back
gd := date.ToGregorianDate(pd) // returns a time.Time instance
}

The PersianDate struct implements Stringer interface as well as json marshal/unmarshal interfaces, so you can use it accordingly:

1
2
3
4
5
6
7
8
9
func Serialize() {
dob, _ := date.NewPersianDate(1400, 01, 01)
p := Person{
Name: "John",
DoB: &dob,
}

js, _ := json.Marshal(&p) // output is {"Name": "John", "DoB": "1400-01-01"}
}

Formatting

Other formatting functions are also available throught the Format() function.

1
2
3
4
5
6
pd, err := date.Parse("1388/02/30")
pd.Format(date.GenericFormat) // returns "۱۳۸۸/۰۲/۰۳"
pd.Format(date.MonthYearFormat) // returns "اردیبهشت ۱۳۸۸"
pd.Format(date.MonthDayFormat) // returns "۳ اردیبهشت"
pd.Format(date.GenericShortFormat) // returns "۱۳۸۸/۲/۳"
pd.Format(date.WrittenFormat) // returns "پنجشنبه ۳ اردیبهشت ۱۳۸۸"

Note that these string use the persian numerals. Other formats also exist that return arabic numerals:

1
2
3
pd.Format("S") // returns serializable format "1388-02-03"
pd.Format("") // returns "1388/02/03"
pd.String() // returns "1388/02/03"

Summary

The Go version of the FarsiLibrary is here! It is built on the trusted and matured .NET version of the library. Feel free to drop a comment if you have any comments/feedbacks.