Skip to content

Commit

Permalink
fix fraction of day calculation, round nearest to 1us
Browse files Browse the repository at this point in the history
  • Loading branch information
santsai committed Aug 11, 2016
1 parent 4347854 commit 5146baa
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions date.go
Expand Up @@ -24,16 +24,23 @@ func shiftJulianToNoon(julianDays, julianFraction float64) (float64, float64) {

// Return the integer values for hour, minutes, seconds and
// nanoseconds that comprised a given fraction of a day.
// values would round to 1 us.
func fractionOfADay(fraction float64) (hours, minutes, seconds, nanoseconds int) {
f := 5184000000000000 * fraction
nanoseconds = int(math.Mod(f, 1000000000))
f = f / 1000000000
seconds = int(math.Mod(f, 3600))
f = f / 3600
minutes = int(math.Mod(f, 60))
f = f / 60
hours = int(f)
return hours, minutes, seconds, nanoseconds

const (
c1us = 1e3
c1s = 1e9
c1day = 24 * 60 * 60 * c1s
)

frac := int64(c1day*fraction + c1us/2)
nanoseconds = int((frac%c1s)/c1us) * c1us
frac /= c1s
seconds = int(frac % 60)
frac /= 60
minutes = int(frac % 60)
hours = int(frac / 60)
return
}

func julianDateToGregorianTime(part1, part2 float64) time.Time {
Expand Down

1 comment on commit 5146baa

@itcuihao
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you tell me why this change? Thank you.

Please sign in to comment.