Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: TimeFromExcelTime #795

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions date.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (

secondsInADay = float64((24 * time.Hour) / time.Second)
nanosInADay = float64((24 * time.Hour) / time.Nanosecond)
roundEpsilon = 1e-9
)

var (
Expand Down Expand Up @@ -103,28 +104,33 @@ func doTheFliegelAndVanFlandernAlgorithm(jd int) (day, month, year int) {
// Convert an excelTime representation (stored as a floating point number) to a time.Time.
func TimeFromExcelTime(excelTime float64, date1904 bool) time.Time {
var date time.Time
var wholeDaysPart = int(excelTime)
// Excel uses Julian dates prior to March 1st 1900, and
// Gregorian thereafter.
wholeDaysPart := int(excelTime)
// Excel uses Julian dates prior to March 1st 1900, and Gregorian
// thereafter.
if wholeDaysPart <= 61 {
const OFFSET1900 = 15018.0
const OFFSET1904 = 16480.0
const MJD0 float64 = 2400000.5
var date time.Time
if date1904 {
date = julianDateToGregorianTime(MJD_0, excelTime+OFFSET1904)
date = julianDateToGregorianTime(MJD0, excelTime+OFFSET1904)
} else {
date = julianDateToGregorianTime(MJD_0, excelTime+OFFSET1900)
date = julianDateToGregorianTime(MJD0, excelTime+OFFSET1900)
}
return date
}
var floatPart = excelTime - float64(wholeDaysPart)
floatPart := excelTime - float64(wholeDaysPart) + roundEpsilon
if date1904 {
date = excel1904Epoc
} else {
date = excel1900Epoc
}
durationPart := time.Duration(nanosInADay * floatPart)
return date.AddDate(0, 0, wholeDaysPart).Add(durationPart)
date = date.AddDate(0, 0, wholeDaysPart).Add(durationPart)
if date.Nanosecond()/1e6 > 500 {
return date.Round(time.Second)
}
return date.Truncate(time.Second)
}

// TimeToExcelTime will convert a time.Time into Excel's float representation, in either 1900 or 1904
Expand Down
Loading