date.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package xlsx
  2. import (
  3. "math"
  4. "time"
  5. )
  6. const MJD_0 float64 = 2400000.5
  7. const MJD_JD2000 float64 = 51544.5
  8. func shiftJulianToNoon(julianDays, julianFraction float64) (float64, float64) {
  9. switch {
  10. case -0.5 < julianFraction && julianFraction < 0.5:
  11. julianFraction += 0.5
  12. case julianFraction >= 0.5:
  13. julianDays += 1
  14. julianFraction -= 0.5
  15. case julianFraction <= -0.5:
  16. julianDays -= 1
  17. julianFraction += 1.5
  18. }
  19. return julianDays, julianFraction
  20. }
  21. // Return the integer values for hour, minutes, seconds and
  22. // nanoseconds that comprised a given fraction of a day.
  23. // values would round to 1 us.
  24. func fractionOfADay(fraction float64) (hours, minutes, seconds, nanoseconds int) {
  25. const (
  26. c1us = 1e3
  27. c1s = 1e9
  28. c1day = 24 * 60 * 60 * c1s
  29. )
  30. frac := int64(c1day*fraction + c1us/2)
  31. nanoseconds = int((frac%c1s)/c1us) * c1us
  32. frac /= c1s
  33. seconds = int(frac % 60)
  34. frac /= 60
  35. minutes = int(frac % 60)
  36. hours = int(frac / 60)
  37. return
  38. }
  39. func julianDateToGregorianTime(part1, part2 float64) time.Time {
  40. part1I, part1F := math.Modf(part1)
  41. part2I, part2F := math.Modf(part2)
  42. julianDays := part1I + part2I
  43. julianFraction := part1F + part2F
  44. julianDays, julianFraction = shiftJulianToNoon(julianDays, julianFraction)
  45. day, month, year := doTheFliegelAndVanFlandernAlgorithm(int(julianDays))
  46. hours, minutes, seconds, nanoseconds := fractionOfADay(julianFraction)
  47. return time.Date(year, time.Month(month), day, hours, minutes, seconds, nanoseconds, time.UTC)
  48. }
  49. // By this point generations of programmers have repeated the
  50. // algorithm sent to the editor of "Communications of the ACM" in 1968
  51. // (published in CACM, volume 11, number 10, October 1968, p.657).
  52. // None of those programmers seems to have found it necessary to
  53. // explain the constants or variable names set out by Henry F. Fliegel
  54. // and Thomas C. Van Flandern. Maybe one day I'll buy that jounal and
  55. // expand an explanation here - that day is not today.
  56. func doTheFliegelAndVanFlandernAlgorithm(jd int) (day, month, year int) {
  57. l := jd + 68569
  58. n := (4 * l) / 146097
  59. l = l - (146097*n+3)/4
  60. i := (4000 * (l + 1)) / 1461001
  61. l = l - (1461*i)/4 + 31
  62. j := (80 * l) / 2447
  63. d := l - (2447*j)/80
  64. l = j / 11
  65. m := j + 2 - (12 * l)
  66. y := 100*(n-49) + i + l
  67. return d, m, y
  68. }
  69. // Convert an excelTime representation (stored as a floating point number) to a time.Time.
  70. func TimeFromExcelTime(excelTime float64, date1904 bool) time.Time {
  71. var date time.Time
  72. var intPart int64 = int64(excelTime)
  73. // Excel uses Julian dates prior to March 1st 1900, and
  74. // Gregorian thereafter.
  75. if intPart <= 61 {
  76. const OFFSET1900 = 15018.0
  77. const OFFSET1904 = 16480.0
  78. var date time.Time
  79. if date1904 {
  80. date = julianDateToGregorianTime(MJD_0, excelTime+OFFSET1904)
  81. } else {
  82. date = julianDateToGregorianTime(MJD_0, excelTime+OFFSET1900)
  83. }
  84. return date
  85. }
  86. var floatPart float64 = excelTime - float64(intPart)
  87. var dayNanoSeconds float64 = 24 * 60 * 60 * 1000 * 1000 * 1000
  88. if date1904 {
  89. date = time.Date(1904, 1, 1, 0, 0, 0, 0, time.UTC)
  90. } else {
  91. date = time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC)
  92. }
  93. durationDays := time.Duration(intPart) * time.Hour * 24
  94. durationPart := time.Duration(dayNanoSeconds * floatPart)
  95. return date.Add(durationDays).Add(durationPart)
  96. }