summaryrefslogtreecommitdiff
path: root/date.go
diff options
context:
space:
mode:
authorStani <spe.stani.be@gmail.com>2021-08-21 05:50:49 +0200
committerGitHub <noreply@github.com>2021-08-21 11:50:49 +0800
commit935af2e356ff60c88761db1fc9a6be8f8c67a4f5 (patch)
treee87ddf1d5973f5bb33dfffbc4e345e018b138030 /date.go
parentdca03c6230e596560ea58ca1edb27581cdd59aaa (diff)
This closes #1002, new fn: DAY ref #65
Co-authored-by: Stani Michiels <git@rchtct.com> Co-authored-by: xuri <xuri.me@gmail.com>
Diffstat (limited to 'date.go')
-rw-r--r--date.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/date.go b/date.go
index a5edcf8..b8c26e0 100644
--- a/date.go
+++ b/date.go
@@ -23,6 +23,7 @@ const (
)
var (
+ daysInMonth = []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
excel1900Epoc = time.Date(1899, time.December, 30, 0, 0, 0, 0, time.UTC)
excel1904Epoc = time.Date(1904, time.January, 1, 0, 0, 0, 0, time.UTC)
excelMinTime1900 = time.Date(1899, time.December, 31, 0, 0, 0, 0, time.UTC)
@@ -167,3 +168,47 @@ func ExcelDateToTime(excelDate float64, use1904Format bool) (time.Time, error) {
}
return timeFromExcelTime(excelDate, use1904Format), nil
}
+
+// isLeapYear determine if leap year for a given year.
+func isLeapYear(y int) bool {
+ if y == y/400*400 {
+ return true
+ }
+ if y == y/100*100 {
+ return false
+ }
+ return y == y/4*4
+}
+
+// getDaysInMonth provides a function to get the days by a given year and
+// month number.
+func getDaysInMonth(y, m int) int {
+ if m == 2 && isLeapYear(y) {
+ return 29
+ }
+ return daysInMonth[m-1]
+}
+
+// validateDate provides a function to validate if a valid date by a given
+// year, month, and day number.
+func validateDate(y, m, d int) bool {
+ if m < 1 || m > 12 {
+ return false
+ }
+ if d < 1 {
+ return false
+ }
+ return d <= getDaysInMonth(y, m)
+}
+
+// formatYear converts the given year number into a 4-digit format.
+func formatYear(y int) int {
+ if y < 1900 {
+ if y < 30 {
+ y += 2000
+ } else {
+ y += 1900
+ }
+ }
+ return y
+}