From c10cc9758645110ea9bb91900663c179906efa3b Mon Sep 17 00:00:00 2001 From: Teodros Date: Sat, 1 Jun 2024 09:03:58 +1000 Subject: [PATCH] new file: date/awareh.go new file: date/awareh_test.go new file: date/cmd/main.go new file: date/example_test.go --- date/awareh.go | 129 +++++++++++++++++++++++++++++++++++++++++++ date/awareh_test.go | 35 ++++++++++++ date/cmd/main.go | 20 +++++++ date/example_test.go | 18 ++++++ 4 files changed, 202 insertions(+) create mode 100644 date/awareh.go create mode 100644 date/awareh_test.go create mode 100644 date/cmd/main.go create mode 100644 date/example_test.go diff --git a/date/awareh.go b/date/awareh.go new file mode 100644 index 0000000..97bff68 --- /dev/null +++ b/date/awareh.go @@ -0,0 +1,129 @@ +// Package date implements functions for converting Gregorian calendar to Geez calander. +package date + +import ( + "fmt" + "strconv" + "strings" + "time" + + "gday.express/ted/geez/gn" +) + +type Gdate struct { + d, m, y int +} + +// Geezday return a geez date (eg "2024-01-09" to "፴ ታኅሣሥ ፳፻፲፮") taking Gregorian calander date of format yyyy-mm-dd +func Geezday(date string) Gdate { + s := Convert(date) + lst := strings.Split(s, "-") + g := Gdate{} + g.d, _ = strconv.Atoi(lst[0]) + g.m, _ = strconv.Atoi(lst[1]) + g.y, _ = strconv.Atoi(lst[2]) + return g +} + +// Today return a todays date according to Geez calander +func Today() Gdate { + t := time.Now() + ls := strings.Split(t.String(), " ") + td := ls[0] + s := Convert(td) + lst := strings.Split(s, "-") + g := Gdate{} + g.d, _ = strconv.Atoi(lst[0]) + g.m, _ = strconv.Atoi(lst[1]) + g.y, _ = strconv.Atoi(lst[2]) + + return g +} + +func (g Gdate) String() string { + month := []string{"መስከረም", "ጥቅምት", "ኅዳር", "ታኅሣሥ", "ጥር", "የካቲት", "መጋቢት", "ሚያዝያ", "ግንቦት", "ሰኔ", "ሐምሌ", "ነሐሴ", "ጳጉሜ"} + d := gn.Fmtint(g.d) + m := month[g.m-1] + y := gn.Fmtint(g.y) + str := fmt.Sprintf("%s %s %s", d, m, y) + return str +} + +// Convert return a Geez calander date ,take string Gregorian calendar date ("1991-05-24" to 16-9-1983) +func Convert(date string) string { + d, _ := time.Parse("2006-01-02", date) + var z time.Time + spt := z.AddDate(d.Year()-1, 10, 8) //spt 11 1st day or pagume of geez calander + f29 := spt.AddDate(0, 0, 112) // return feb 29(leap) or march 1(not leap) + //dates jan 1 and after + x := d.AddDate(0, 0, 111) + if f29.Month() != 2 { + mm, dd, yy := convert(x) + str := fmt.Sprintf("%v-%v-%v", dd, mm, yy) + return str + } + //dates b/n ጳጐሜ 6 and jan 1 + if x.YearDay() != 365 && d.Year() != x.Year() { + mm, dd, yy := convert(x) + str := fmt.Sprintf("%v-%v-%v", dd, mm, yy) + return str + } + //dates befor ጳጐሜ 6 + if x.YearDay() != 365 { + x = d.AddDate(0, 0, 112) + mm, dd, yy := convert(x) + str := fmt.Sprintf("%v-%v-%v", dd, mm, yy) + return str + } + // Leap day. ጳጐሜ 6 + x = d.AddDate(0, 0, 110) + _, _, yy := convert(x) + mm := 13 + dd := 6 + str := fmt.Sprintf("%v-%v-%v", dd, mm, yy) + return str + +} + +// daysBefore[m] counts the number of days in a non-leap year +// before month m begins. There is an entry for m=13, counting +// the number of days before Meskerem of next year (365). +var daysBefore = [...]int32{ + 0, + 30, + 30 + 30, + 30 + 30 + 30, + 30 + 30 + 30 + 30, + 30 + 30 + 30 + 30 + 30, + 30 + 30 + 30 + 30 + 30 + 30, + 30 + 30 + 30 + 30 + 30 + 30 + 30, + 30 + 30 + 30 + 30 + 30 + 30 + 30 + 30, + 30 + 30 + 30 + 30 + 30 + 30 + 30 + 30 + 30, + 30 + 30 + 30 + 30 + 30 + 30 + 30 + 30 + 30 + 30, + 30 + 30 + 30 + 30 + 30 + 30 + 30 + 30 + 30 + 30 + 30, + 30 + 30 + 30 + 30 + 30 + 30 + 30 + 30 + 30 + 30 + 30 + 30, + 30 + 30 + 30 + 30 + 30 + 30 + 30 + 30 + 30 + 30 + 30 + 30 + 5, +} + +func convert(x time.Time) (int, int, int) { + yr := x.Year() + day := x.YearDay() - 1 + month := int(day / 30) + end := int(daysBefore[month+1]) + var begin int + if day >= end { + month++ + begin = end + } else { + begin = int(daysBefore[month]) + } + month++ + day = day - begin + 1 + if month > 13 { + month = month - 13 + yr = yr - 7 //geez year + return month, day, yr + } + yr = yr - 8 //geez year + return month, day, yr +} diff --git a/date/awareh_test.go b/date/awareh_test.go new file mode 100644 index 0000000..4c6b21b --- /dev/null +++ b/date/awareh_test.go @@ -0,0 +1,35 @@ +package date + +import ( + "testing" +) + +var tests = []struct { + in string + want string +}{ + {"2024-01-09", "2016-04-30 00:00:00 +0000 UTC"}, + {"1991-05-24", "1983-9-16 00:00:00 +0000 UTC"}, + {"2023-09-10", "2015-13-05 00:00:00 +0000 UTC"}, + {"2023-09-11", "pag 2015-13-06"}, + {"2023-09-12", "2016-01-01 00:00:00 +0000 UTC"}, + {"2023-09-19", "2016-01-08 00:00:00 +0000 UTC"}, + {"2023-12-01", "2016-03-21 00:00:00 +0000 UTC"}, + {"2023-12-31", "2016-04-21 00:00:00 +0000 UTC"}, + {"2024-01-01", "2016-04-22 00:00:00 +0000 UTC"}, + {"2024-02-29", "2016-06-21 00:00:00 +0000 UTC"}, + {"2024-03-01", "2016-06-22 00:00:00 +0000 UTC"}, + {"2024-04-01", "2016-07-23 00:00:00 +0000 UTC"}, + {"2024-09-10", "2016-13-05 00:00:00 +0000 UTC"}, + {"2024-09-11", "2017-01-01 00:00:00 +0000 UTC"}, +} + +func TestConvert(t *testing.T) { + for _, v := range tests { + got := Convert(v.in) + if got != v.want { + t.Errorf("got %s want %s", got, v.want) + } + } + +} diff --git a/date/cmd/main.go b/date/cmd/main.go new file mode 100644 index 0000000..0dba8e1 --- /dev/null +++ b/date/cmd/main.go @@ -0,0 +1,20 @@ +package main + +import ( + "fmt" + "strings" + "time" + + "gday.express/ted/geez/date" +) + +func main() { + var t time.Time + t = t.AddDate(2023, 00, 06) //Jan 7 2024 + l := strings.Split(t.String(), " ") + s := date.Convert(l[0]) + fmt.Printf("xmass %s\n", s) + tt := date.Today() + fmt.Printf("xmass %s\n", tt) + +} diff --git a/date/example_test.go b/date/example_test.go new file mode 100644 index 0000000..a65c61e --- /dev/null +++ b/date/example_test.go @@ -0,0 +1,18 @@ +package date + +import ( + "fmt" + + "strings" + "time" +) + +func ExampleConvert() { + var t time.Time + t = t.AddDate(2023, 00, 06) //Jan 7 2024 + l := strings.Split(t.String(), " ") + s := Convert(l[0]) + fmt.Printf("xmass %s\n", s) + //output: xmass 28-4-2016 + +}