Created
February 4, 2024 05:06
-
-
Save MVAodhan/4384d52719fc9b3b6874bf166fb1c973 to your computer and use it in GitHub Desktop.
rwc-iq-29-02-24
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
package main | |
import ( | |
"fmt" | |
"log" | |
"strconv" | |
"strings" | |
) | |
var daysInMonth = make(map[string]int) | |
var monthOfYear = make(map[string]int) | |
var indexedMonth = make(map[int]string) | |
var leapYears = "2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, and 2048" | |
func main() { | |
daysInMonth["Jan"] = 31 | |
daysInMonth["Feb"] = 28 | |
daysInMonth["Mar"] = 31 | |
daysInMonth["Apr"] = 30 | |
daysInMonth["May"] = 31 | |
daysInMonth["Jun"] = 30 | |
daysInMonth["Jul"] = 31 | |
daysInMonth["Aug"] = 31 | |
daysInMonth["Sep"] = 30 | |
daysInMonth["Oct"] = 31 | |
daysInMonth["Nov"] = 30 | |
daysInMonth["Dec"] = 31 | |
monthOfYear["Jan"] = 1 | |
monthOfYear["Feb"] = 2 | |
monthOfYear["Mar"] = 3 | |
monthOfYear["Apr"] = 4 | |
monthOfYear["May"] = 5 | |
monthOfYear["Jun"] = 6 | |
monthOfYear["Jul"] = 7 | |
monthOfYear["Aug"] = 8 | |
monthOfYear["Sep"] = 9 | |
monthOfYear["Oct"] = 10 | |
monthOfYear["Nov"] = 11 | |
monthOfYear["Dec"] = 12 | |
indexedMonth[1] = "Jan" | |
indexedMonth[2] = "Feb" | |
indexedMonth[3] = "Mar" | |
indexedMonth[4] = "Apr" | |
indexedMonth[5] = "May" | |
indexedMonth[6] = "Jun" | |
indexedMonth[7] = "Jul" | |
indexedMonth[8] = "Aug" | |
indexedMonth[9] = "Sep" | |
indexedMonth[10] = "Oct" | |
indexedMonth[11] = "Nov" | |
indexedMonth[12] = "Dec" | |
daysBetween("Feb 29, 2020", "Oct 31, 2024") | |
} | |
func daysBetween(first, second string) { | |
fMonth, fDate, fYear := splitDate(first) | |
sMonth, sDate, sYear := splitDate(second) | |
var daysBetweenDates int | |
if strings.Contains(leapYears, fmt.Sprint(fYear)) { | |
daysBetweenDates++ | |
} | |
if strings.Contains(leapYears, fmt.Sprint(sYear)) { | |
daysBetweenDates++ | |
} | |
if fMonth == sMonth && fYear == sYear { | |
daysBetweenDates = sDate - fDate | |
} else if fMonth != sMonth && fYear == sYear { | |
days := calcDiffdaysInMonths(fMonth, sMonth, fDate, sDate) | |
daysBetweenDates = daysBetweenDates + days | |
} else { | |
days := calcDiffdaysInYears(fYear, sYear, fDate, sDate, fMonth, sMonth) | |
daysBetweenDates = daysBetweenDates + days | |
} | |
fmt.Println(daysBetweenDates) | |
} | |
func calcDiffdaysInMonths(fMonth, sMonth string, fDate, sDate int) int { | |
fMonthOfYear := monthOfYear[fMonth] | |
sMonthOfYear := monthOfYear[sMonth] | |
var days int | |
for i := fMonthOfYear; i <= sMonthOfYear; i++ { | |
mStr := getMonthStr(i) | |
if i == fMonthOfYear { | |
daysInMonth := daysInMonth[mStr] | |
if fDate > 1 { | |
daysLeftInMonth := daysInMonth - fDate | |
days = days + daysLeftInMonth | |
} else { | |
days = days + daysInMonth | |
} | |
continue | |
} | |
if i > fMonthOfYear && i < sMonthOfYear { | |
daysInMonth := daysInMonth[mStr] | |
days = days + daysInMonth | |
} | |
if i == sMonthOfYear { | |
days = days + sDate | |
} | |
} | |
return days | |
} | |
func getMonthStr(i int) (month string) { | |
m := indexedMonth[i] | |
return m | |
} | |
func calcDiffdaysInYears(fYear, sYear, fDate, sDate int, fMonth, sMonth string) int { | |
var days int | |
for i := fYear; i <= sYear; i++ { | |
if i == fYear { | |
daysInYear := calcDiffdaysInMonths(fMonth, "Dec", fDate, 31) | |
days = days + daysInYear | |
} else if i == sYear { | |
daysInYear := daysFromNY(sMonth, sDate) | |
days = days + daysInYear | |
} else { | |
days = days + 365 | |
} | |
} | |
return days | |
} | |
func daysFromNY(sMonth string, sDate int) int { | |
var days int | |
last := monthOfYear[sMonth] | |
for i := 1; i <= last; i++ { | |
m := indexedMonth[i] | |
if i < last { | |
daysInMonth := daysInMonth[m] | |
days = days + daysInMonth | |
} | |
if i == last { | |
days = days + sDate | |
} | |
} | |
return days | |
} | |
func splitDate(str string) (month string, date, year int) { | |
slice := strings.Split(str, ",") | |
monthDate := strings.Split(slice[0], " ") | |
m := monthDate[0] | |
d := monthDate[1] | |
y := strings.Trim(slice[1], " ") | |
dInt, err := strconv.Atoi(d) | |
if err != nil { | |
log.Fatal("err convering date to number", err) | |
} | |
yInt, err := strconv.Atoi(y) | |
if err != nil { | |
log.Fatal("err convering year to number", err) | |
} | |
return m, dInt, yInt | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment