From 9debf1d1f895d5f45e8ff79def7688a9e3af6307 Mon Sep 17 00:00:00 2001 From: Yinebeb-01 Date: Wed, 3 Jan 2024 20:01:16 +0300 Subject: [PATCH] typo-error fixed --- api/getAdFromEt.go | 12 ++-- api/getEtFromAd.go | 12 ++-- ethioGrego/{to_ethiopian.go => ethiopian.go} | 70 ++++++++++---------- ethioGrego/{to_gregorian.go => gregorian.go} | 45 ++++++------- ethioGrego/helper.go | 21 ++---- ethioGrego_test.go | 4 +- main.go | 6 +- 7 files changed, 81 insertions(+), 89 deletions(-) rename ethioGrego/{to_ethiopian.go => ethiopian.go} (55%) rename ethioGrego/{to_gregorian.go => gregorian.go} (57%) diff --git a/api/getAdFromEt.go b/api/getAdFromEt.go index 84b1afe..67b4b3a 100644 --- a/api/getAdFromEt.go +++ b/api/getAdFromEt.go @@ -27,17 +27,17 @@ func GetAdFromEt(ctx *gin.Context) { if state { dateString = strings.TrimPrefix(dateString, "date=") } - var splitedDate = strings.Split(dateString, "-") - if len(splitedDate) > 3 { + var splitDate = strings.Split(dateString, "-") + if len(splitDate) > 3 { ctx.JSON(http.StatusBadRequest, gin.H{ "response": "not a valid date", }) } else { - day, _ := strconv.Atoi(splitedDate[2]) - month, _ := strconv.Atoi(splitedDate[1]) - year, _ := strconv.Atoi(splitedDate[0]) + day, _ := strconv.Atoi(splitDate[2]) + month, _ := strconv.Atoi(splitDate[1]) + year, _ := strconv.Atoi(splitDate[0]) - date, err := ethioGrego.To_gregorian(year, month, day) + date, err := ethioGrego.ToGregorian(year, month, day) if err == nil { ctx.JSON(http.StatusOK, gin.H{ "response": date.Format("2006-01-02"), diff --git a/api/getEtFromAd.go b/api/getEtFromAd.go index 8ab48a0..6af3576 100644 --- a/api/getEtFromAd.go +++ b/api/getEtFromAd.go @@ -26,16 +26,16 @@ func GetEtFromAd(ctx *gin.Context) { if state { dateString = strings.TrimPrefix(dateString, "date=") } - var splitedDate = strings.Split(dateString, "-") - if len(splitedDate) > 3 { + var splitDate = strings.Split(dateString, "-") + if len(splitDate) > 3 { ctx.JSON(http.StatusBadRequest, gin.H{ "response": "not a valid date", }) } else { - day, _ := strconv.Atoi(splitedDate[2]) - month, _ := strconv.Atoi(splitedDate[1]) - year, _ := strconv.Atoi(splitedDate[0]) - EtDate, err := ethioGrego.To_ethiopian(year, month, day) + day, _ := strconv.Atoi(splitDate[2]) + month, _ := strconv.Atoi(splitDate[1]) + year, _ := strconv.Atoi(splitDate[0]) + EtDate, err := ethioGrego.ToEthiopian(year, month, day) if err == nil { ctx.JSON(http.StatusOK, gin.H{ "response": EtDate.Format("2006-01-02"), diff --git a/ethioGrego/to_ethiopian.go b/ethioGrego/ethiopian.go similarity index 55% rename from ethioGrego/to_ethiopian.go rename to ethioGrego/ethiopian.go index acf72db..95a6180 100755 --- a/ethioGrego/to_ethiopian.go +++ b/ethioGrego/ethiopian.go @@ -7,10 +7,10 @@ import ( "time" ) -// Ethiopian date string representation of provided Gregorian date -func To_ethiopian(year, month, date int) (time.Time, error) { - var tahissas int - var ethiopian_date int +// ToEthiopian gives Ethiopian date string representation of provided Gregorian date +func ToEthiopian(year, month, date int) (time.Time, error) { + var december int + var ethiopianDate int var dateResult string if !isValid(year, month, date) { @@ -25,77 +25,77 @@ func To_ethiopian(year, month, date int) (time.Time, error) { // Number of days in gregorian months starting with January (index 1) // Index 0 is reserved for leap years switches. - gregorian_months := []int{0, 31, 28, 31, 30, 31, 30, + gregorianMonths := []int{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} - ethiopian_months := []int{0, 30, 30, 30, 30, 30, 30, 30, + ethiopianMonths := []int{0, 30, 30, 30, 30, 30, 30, 30, 30, 30, 5, 30, 30, 30, 30} // if gregorian leap year, February has 29 days. if (year%4 == 0 && year%100 != 0) || year%400 == 0 { - gregorian_months[2] = 29 + gregorianMonths[2] = 29 } // September sees 8y difference - ethiopian_year := year - 8 + ethiopianYear := year - 8 // if ethiopian leap year pagumain has 6 days - if ethiopian_year%4 == 3 { - ethiopian_months[10] = 6 + if ethiopianYear%4 == 3 { + ethiopianMonths[10] = 6 } else { - ethiopian_months[10] = 5 + ethiopianMonths[10] = 5 } // Ethiopian new year in Gregorian calendar - new_year_day := start_day_of_ethiopian(year - 8) + newYearDay := startDayOfEthiopian(year - 8) // calculate number of days up to that date until := 0 for i := 1; i < month; i++ { - until += gregorian_months[i] + until += gregorianMonths[i] } until += date - // # update tahissas (december) to match january 1st - if ethiopian_year%4 == 0 { - tahissas = 26 + // # update december to match january 1st + if ethiopianYear%4 == 0 { + december = 26 } else { - tahissas = 25 + december = 25 } // take into account the 1582 change if year < 1582 { - ethiopian_months[1] = 0 - ethiopian_months[2] = tahissas + ethiopianMonths[1] = 0 + ethiopianMonths[2] = december } else if until <= 277 && year == 1582 { - ethiopian_months[1] = 0 - ethiopian_months[2] = tahissas + ethiopianMonths[1] = 0 + ethiopianMonths[2] = december } else { - tahissas = new_year_day - 3 - ethiopian_months[1] = tahissas + december = newYearDay - 3 + ethiopianMonths[1] = december } // calculate month and date incremently m := 0 - for m = range ethiopian_months { - if until <= ethiopian_months[m] { - if m == 1 || ethiopian_months[m] == 0 { - ethiopian_date = until + (30 - tahissas) + for m = range ethiopianMonths { + if until <= ethiopianMonths[m] { + if m == 1 || ethiopianMonths[m] == 0 { + ethiopianDate = until + (30 - december) } else { - ethiopian_date = until + ethiopianDate = until } break } else { - until -= ethiopian_months[m] + until -= ethiopianMonths[m] } } // if m > 4, we're already on next Ethiopian year if m > 10 { - ethiopian_year += 1 + ethiopianYear += 1 } // Ethiopian months ordered according to Gregorian order := []int{0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4} - ethiopian_month := order[m] + ethiopianMonth := order[m] - da := strconv.Itoa(ethiopian_date) - mon := strconv.Itoa(ethiopian_month) + da := strconv.Itoa(ethiopianDate) + mon := strconv.Itoa(ethiopianMonth) if len(da) == 1 { da = "0" + da } @@ -103,10 +103,10 @@ func To_ethiopian(year, month, date int) (time.Time, error) { mon = "0" + mon } - dateResult = strconv.Itoa(ethiopian_year) + "-" + mon + "-" + da + dateResult = strconv.Itoa(ethiopianYear) + "-" + mon + "-" + da res, err := time.Parse("2006-01-02", dateResult) if err != nil { - fmt.Print("unabe to parse dateResult.", err) //for debugging purpose + fmt.Print("unable to parse dateResult.", err) return time.Time{}, errors.New("not a valid date") } return res, nil diff --git a/ethioGrego/to_gregorian.go b/ethioGrego/gregorian.go similarity index 57% rename from ethioGrego/to_gregorian.go rename to ethioGrego/gregorian.go index 48a9891..5759f8a 100755 --- a/ethioGrego/to_gregorian.go +++ b/ethioGrego/gregorian.go @@ -7,37 +7,37 @@ import ( "time" ) -// Gregorian date object representation of provided Ethiopian date -func To_gregorian(year, month, date int) (time.Time, error) { - var gregorian_date int +// ToGregorian gives Gregorian date object representation of provided Ethiopian date +func ToGregorian(year, month, date int) (time.Time, error) { + var gregorianDate int var dateResult string if !isValid(year, month, date) { return time.Time{}, errors.New("not a valid date") } // Ethiopian new year in Gregorian calendar - new_year_day := start_day_of_ethiopian(year) + newYearDay := startDayOfEthiopian(year) // September (Ethiopian) sees 7y difference - gregorian_year := year + 7 + gregorianYear := year + 7 // Number of days in gregorian months starting with September (index 1) // Index 0 is reserved for leap years switches. - gregorian_months := []int{0, 30, 31, 30, 31, 31, 28, + gregorianMonths := []int{0, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30} //if next gregorian year is leap year, February has 29 days. - next_year := gregorian_year + 1 - if (next_year%4 == 0 && next_year%100 != 0) || next_year%400 == 0 { - gregorian_months[6] = 29 + nextYear := gregorianYear + 1 + if (nextYear%4 == 0 && nextYear%100 != 0) || nextYear%400 == 0 { + gregorianMonths[6] = 29 } // calculate number of days up to that date until := ((month - 1) * 30) + date if until <= 37 && year <= 1575 { //mysterious rule until += 28 - gregorian_months[0] = 31 + gregorianMonths[0] = 31 } else { - until += new_year_day - 1 + until += newYearDay - 1 } // if ethiopian year is leap year, paguemain has six days @@ -45,28 +45,28 @@ func To_gregorian(year, month, date int) (time.Time, error) { until += 1 } - //calculate month and date incremently + //calculate month and date incrementally m := 0 - for i := range gregorian_months { - if until <= gregorian_months[i] { + for i := range gregorianMonths { + if until <= gregorianMonths[i] { m = i - gregorian_date = until + gregorianDate = until break } else { m = i - until -= gregorian_months[i] + until -= gregorianMonths[i] } } // if m > 4, we're already on next Gregorian year if m > 4 { - gregorian_year += 1 + gregorianYear += 1 } // Gregorian months ordered according to Ethiopian order := []int{8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9} - gregorian_month := order[m] + gregorianMonth := order[m] - da := strconv.Itoa(gregorian_date) - mon := strconv.Itoa(gregorian_month) + da := strconv.Itoa(gregorianDate) + mon := strconv.Itoa(gregorianMonth) if len(da) == 1 { da = "0" + da } @@ -74,12 +74,11 @@ func To_gregorian(year, month, date int) (time.Time, error) { mon = "0" + mon } - dateResult = "" + strconv.Itoa(gregorian_year) + "-" + mon + "-" + da + dateResult = "" + strconv.Itoa(gregorianYear) + "-" + mon + "-" + da res, err := time.Parse("2006-01-02", dateResult) if err != nil { - fmt.Print("unabe to parse dateResult.", err) + fmt.Print("unable to parse dateResult.", err) return time.Time{}, errors.New("not a valid date") - } return res, nil } diff --git a/ethioGrego/helper.go b/ethioGrego/helper.go index 1502751..e69d973 100755 --- a/ethioGrego/helper.go +++ b/ethioGrego/helper.go @@ -1,22 +1,18 @@ package ethioGrego -type response struct { - msg string -} - // returns first day of that Ethiopian year -func start_day_of_ethiopian(year int) int { +func startDayOfEthiopian(year int) int { //magic formula gives start of year - new_year_day := (year / 100) - (year / 400) - 4 + newYearDay := (year / 100) - (year / 400) - 4 - //if the prev ethiopian year is a leap year, new-year occrus on 12th + //if the prev ethiopian year is a leap year, new-year occurs on 12th if (year-1)%4 == 3 { - new_year_day += 1 + newYearDay += 1 } - return new_year_day + return newYearDay } -// prevent incorect input +// prevent incorrect input func isValid(year, month, date int) bool { inputs := []int{year, month, date} for i := range inputs { @@ -24,8 +20,5 @@ func isValid(year, month, date int) bool { return false } } - if len(inputs) != 3 || inputs[2] > 31 || inputs[1] > 12 { - return false - } - return true + return !(len(inputs) != 3 || inputs[2] > 31 || inputs[1] > 12) } diff --git a/ethioGrego_test.go b/ethioGrego_test.go index e002681..3d137d8 100755 --- a/ethioGrego_test.go +++ b/ethioGrego_test.go @@ -32,11 +32,11 @@ func TestEthiopianDate(t *testing.T) { ethiopianDate := "2015-01-18 00:00:00 +0000 UTC" gregorianDate := "2022-09-28 00:00:00 +0000 UTC" - time, err := ethioGrego.To_ethiopian(2022, 9, 28) + time, err := ethioGrego.ToEthiopian(2022, 9, 28) if err == nil { expectedEthio = time.String() } - time, err = ethioGrego.To_gregorian(2015, 1, 18) + time, err = ethioGrego.ToGregorian(2015, 1, 18) if err == nil { expectedGrego = time.String() } diff --git a/main.go b/main.go index 4969b7d..8a00479 100755 --- a/main.go +++ b/main.go @@ -25,11 +25,11 @@ import ( // @BasePath /api/v1 // @securityDefinitions.basic BasicAuth func main() { - // programmatically set swagger info + // set swagger info //docs.SwaggerInfo.Title = "Swagger Example API" - //docs.SwaggerInfo.Description = "This is a sample server Petstore server." + //docs.SwaggerInfo.Description = "This is a ethioGrego server." //docs.SwaggerInfo.Version = "1.0" - //docs.SwaggerInfo.Host = "petstore.swagger.io" + //docs.SwaggerInfo.Host = "swagger.io" //docs.SwaggerInfo.BasePath = "api/v1" //docs.SwaggerInfo.Schemes = []string{"http", "https"}