-
Notifications
You must be signed in to change notification settings - Fork 2
/
GregorianToJalali.go
66 lines (53 loc) · 1.16 KB
/
GregorianToJalali.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import "fmt"
type Date struct {
Year int
Month int
Day int
}
func (d Date) String() string {
return fmt.Sprintf("%d/%02d/%02d", d.Year, d.Month, d.Day)
}
func GregorianToJalali(year int, month int, day int) Date {
result := Date{}
array := [13]int{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}
if year <= 1600 {
year = year - 621
result.Year = 0
} else {
year = year - 1600
result.Year = 979
}
var temp int
if year > 2 {
temp = year + 1
} else {
temp = year
}
days := ((temp + 3) / 4) + (365 * year) - ((temp + 99) / 100) - 80 +
array[month-1] + ((temp + 399) / 400) + day
result.Year += 33 * (days / 12053)
days = days % 12053
result.Year += 4 * (days / 1461)
days = days % 1461
if days > 365 {
result.Year += (days - 1) / 365
days = (days - 1) % 365
}
if days < 186 {
result.Month = 1 + (days / 31)
} else {
result.Month = 7 + (days-186)/30
}
if days < 186 {
result.Day = 1 + (days % 31)
} else {
result.Day = 1 + (days-186)%30
}
return result
}
func main() {
fmt.Println(GregorianToJalali(2022, 1, 22))
fmt.Println(GregorianToJalali(2022, 4, 4))
fmt.Println(GregorianToJalali(2023, 10, 9))
}