Skip to content

Commit

Permalink
Merge pull request #3 from Yinebeb-01/fix-typo-error
Browse files Browse the repository at this point in the history
typo-error fixed
  • Loading branch information
yinebebt authored Jan 3, 2024
2 parents c8022e7 + 9debf1d commit ee90eb7
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 89 deletions.
12 changes: 6 additions & 6 deletions api/getAdFromEt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
12 changes: 6 additions & 6 deletions api/getEtFromAd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
70 changes: 35 additions & 35 deletions ethioGrego/to_ethiopian.go → ethioGrego/ethiopian.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -25,88 +25,88 @@ 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
}
if len(mon) == 1 {
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
Expand Down
45 changes: 22 additions & 23 deletions ethioGrego/to_gregorian.go → ethioGrego/gregorian.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,79 +7,78 @@ 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
if year-1%4 == 3 {
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
}
if len(mon) == 1 {
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
}
21 changes: 7 additions & 14 deletions ethioGrego/helper.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
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 {
if inputs[i] <= 0 {
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)
}
4 changes: 2 additions & 2 deletions ethioGrego_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand Down

0 comments on commit ee90eb7

Please sign in to comment.