summaryrefslogtreecommitdiff
path: root/date.go
diff options
context:
space:
mode:
authorxuri <xuri.me@gmail.com>2018-06-20 15:53:30 +0800
committerGitHub <noreply@github.com>2018-06-20 15:53:30 +0800
commit3a91b28ddbca53d09b46612fa4c2c29a65d42e41 (patch)
tree1e4a44eac7de7295b5de48bab75aea254a1b7ef4 /date.go
parent5db716d849d51b630822571c8a0aa4edd7cc8365 (diff)
parent37c470e8c0df99bd47f7054834e0db93b75ab073 (diff)
Merge pull request #237 from liepumartins/patch-1
Ability to parse dates further in future
Diffstat (limited to 'date.go')
-rw-r--r--date.go23
1 files changed, 22 insertions, 1 deletions
diff --git a/date.go b/date.go
index a493866..e078374 100644
--- a/date.go
+++ b/date.go
@@ -15,7 +15,20 @@ func timeToUTCTime(t time.Time) time.Time {
// timeToExcelTime provides function to convert time to Excel time.
func timeToExcelTime(t time.Time) float64 {
- return float64(t.UnixNano())/8.64e13 + 25569.0
+ // TODO in future this should probably also handle date1904 and like TimeFromExcelTime
+ var excelTime float64
+ excelTime = 0
+ // check if UnixNano would be out of int64 range
+ for t.Unix() > 9223372036 {
+ // reduce by aprox. 290 years, which is max for int64 nanoseconds
+ deltaDays := 290 * 364
+ delta := time.Duration(deltaDays * 8.64e13)
+ excelTime = excelTime + float64(deltaDays)
+ t = t.Add(-delta)
+ }
+ // finally add remainder of UnixNano to keep nano precision
+ // and 25569 which is days between 1900 and 1970
+ return excelTime + float64(t.UnixNano())/8.64e13 + 25569.0
}
// shiftJulianToNoon provides function to process julian date to noon.
@@ -90,6 +103,7 @@ func doTheFliegelAndVanFlandernAlgorithm(jd int) (day, month, year int) {
// timeFromExcelTime provides function to convert an excelTime representation
// (stored as a floating point number) to a time.Time.
func timeFromExcelTime(excelTime float64, date1904 bool) time.Time {
+ const MDD int64 = 106750 // Max time.Duration Days, aprox. 290 years
var date time.Time
var intPart = int64(excelTime)
// Excel uses Julian dates prior to March 1st 1900, and Gregorian
@@ -113,6 +127,13 @@ func timeFromExcelTime(excelTime float64, date1904 bool) time.Time {
} else {
date = time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC)
}
+
+ // Duration is limited to aprox. 290 years
+ for intPart > MDD {
+ durationDays := time.Duration(MDD) * time.Hour * 24
+ date = date.Add(durationDays)
+ intPart = intPart - MDD
+ }
durationDays := time.Duration(intPart) * time.Hour * 24
durationPart := time.Duration(dayNanoSeconds * floatPart)
return date.Add(durationDays).Add(durationPart)