-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat [date]: add jalaali to georgian date.
add function JalaaliToGregorianDate which returns georgian format time.Time. add function JalaaliToGregorian which returns string in format %04d/%02d/%02d.
- Loading branch information
Showing
5 changed files
with
196 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package date | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestJalaaliToGregorianDate(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
outPut time.Time | ||
err error | ||
shouldError bool | ||
}{ | ||
{"1401/02/03", time.Date(2022, 4, 23, 0, 0, 0, 0, time.UTC), nil, false}, | ||
{"1401-02-03", time.Date(2022, 4, 23, 0, 0, 0, 0, time.UTC), nil, false}, | ||
{"1401-02/03", time.Time{}, ErrInvalidFormat, true}, | ||
{"1401/02-03", time.Time{}, ErrInvalidFormat, true}, | ||
{"1401-02-0A", time.Time{}, nil, true}, | ||
{"1401-0B-03", time.Time{}, nil, true}, | ||
{"140C-02-03", time.Time{}, nil, true}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(fmt.Sprintf("Testing input: %s", tt.input), func(t *testing.T) { | ||
georgianDate, err := JalaaliToGregorianDate(tt.input) | ||
if tt.shouldError { | ||
if err == nil { | ||
t.Errorf("expected error for input %v but got none", tt.input) | ||
} else if tt.err != nil && !errors.Is(err, tt.err) { | ||
t.Errorf("unexpected error for input %v: got %v, expected %v", tt.input, err, tt.err) | ||
} | ||
} else { | ||
if err != nil { | ||
t.Errorf("did not expect error for input %v but got %v", tt.input, err) | ||
} else if !georgianDate.Equal(tt.outPut) { | ||
t.Errorf("wrong result for input %v: got %v, expected %v", tt.input, georgianDate, tt.outPut) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestJalaaliToGregorian(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
outPut string | ||
err error | ||
shouldError bool | ||
}{ | ||
{"1401/02/03", "2022/04/23", nil, false}, | ||
{"1401-02-03", "2022/04/23", nil, false}, | ||
{"1401-02/03", "", ErrInvalidFormat, true}, | ||
{"1401/02-03", "", ErrInvalidFormat, true}, | ||
{"1401-02-0A", "", nil, true}, | ||
{"1401-0B-03", "", nil, true}, | ||
{"140C-02-03", "", nil, true}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(fmt.Sprintf("Testing input: %s", tt.input), func(t *testing.T) { | ||
georgian, err := JalaaliToGregorian(tt.input) | ||
if tt.shouldError { | ||
if err == nil { | ||
t.Errorf("expected error for input %v but got none", tt.input) | ||
} else if tt.err != nil && !errors.Is(err, tt.err) { | ||
t.Errorf("unexpected error for input %v: got %v, expected %v", tt.input, err, tt.err) | ||
} | ||
} else { | ||
if err != nil { | ||
t.Errorf("did not expect error for input %v but got %v", tt.input, err) | ||
} else if georgian != tt.outPut { | ||
t.Errorf("wrong result for input %v: got %v, expected %v", tt.input, georgian, tt.outPut) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package date | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/jalaali/go-jalaali" | ||
) | ||
|
||
var ErrInvalidFormat error = fmt.Errorf("invalid date format") | ||
|
||
func JalaaliToGregorianDate(jalaaliDate string) (time.Time, error) { | ||
var parts []string | ||
|
||
if strings.Contains(jalaaliDate, "/") { | ||
parts = strings.Split(jalaaliDate, "/") | ||
} else if strings.Contains(jalaaliDate, "-") { | ||
parts = strings.Split(jalaaliDate, "-") | ||
} else { | ||
return time.Time{}, ErrInvalidFormat | ||
} | ||
|
||
if len(parts) != 3 { | ||
return time.Time{}, ErrInvalidFormat | ||
} | ||
|
||
year, err := strconv.Atoi(parts[0]) | ||
if err != nil { | ||
return time.Time{}, err | ||
} | ||
|
||
month, err := strconv.Atoi(parts[1]) | ||
if err != nil { | ||
return time.Time{}, err | ||
} | ||
|
||
day, err := strconv.Atoi(parts[2]) | ||
if err != nil { | ||
return time.Time{}, err | ||
} | ||
|
||
gYear, gMonth, gDay, err := jalaali.ToGregorian(year, jalaali.Month(month), day) | ||
if err != nil { | ||
return time.Time{}, err | ||
} | ||
|
||
return time.Date(gYear, time.Month(gMonth), gDay, 0, 0, 0, 0, time.UTC), nil | ||
} | ||
|
||
func JalaaliToGregorian(jalaaliDate string) (string, error) { | ||
var parts []string | ||
|
||
if strings.Contains(jalaaliDate, "/") { | ||
parts = strings.Split(jalaaliDate, "/") | ||
} else if strings.Contains(jalaaliDate, "-") { | ||
parts = strings.Split(jalaaliDate, "-") | ||
} else { | ||
return "", ErrInvalidFormat | ||
} | ||
|
||
if len(parts) != 3 { | ||
return "", ErrInvalidFormat | ||
} | ||
|
||
year, err := strconv.Atoi(parts[0]) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
month, err := strconv.Atoi(parts[1]) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
day, err := strconv.Atoi(parts[2]) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// Convert Jalaali to Gregorian | ||
gYear, gMonth, gDay, err := jalaali.ToGregorian(year, jalaali.Month(month), day) | ||
if err != nil { | ||
return "", err | ||
} | ||
// Return the Gregorian date as time.Time | ||
return fmt.Sprintf("%04d/%02d/%02d", gYear, gMonth, gDay), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters