date.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package excelize
  2. import (
  3. "math"
  4. "time"
  5. )
  6. // timeLocationUTC defined the UTC time location.
  7. var timeLocationUTC, _ = time.LoadLocation("UTC")
  8. // timeToUTCTime provides a function to convert time to UTC time.
  9. func timeToUTCTime(t time.Time) time.Time {
  10. return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), timeLocationUTC)
  11. }
  12. // timeToExcelTime provides a function to convert time to Excel time.
  13. func timeToExcelTime(t time.Time) float64 {
  14. // TODO in future this should probably also handle date1904 and like TimeFromExcelTime
  15. var excelTime float64
  16. var deltaDays int64
  17. excelTime = 0
  18. deltaDays = 290 * 364
  19. // check if UnixNano would be out of int64 range
  20. for t.Unix() > deltaDays*24*60*60 {
  21. // reduce by aprox. 290 years, which is max for int64 nanoseconds
  22. delta := time.Duration(deltaDays) * 24 * time.Hour
  23. excelTime = excelTime + float64(deltaDays)
  24. t = t.Add(-delta)
  25. }
  26. // finally add remainder of UnixNano to keep nano precision
  27. // and 25569 which is days between 1900 and 1970
  28. return excelTime + float64(t.UnixNano())/8.64e13 + 25569.0
  29. }
  30. // shiftJulianToNoon provides a function to process julian date to noon.
  31. func shiftJulianToNoon(julianDays, julianFraction float64) (float64, float64) {
  32. switch {
  33. case -0.5 < julianFraction && julianFraction < 0.5:
  34. julianFraction += 0.5
  35. case julianFraction >= 0.5:
  36. julianDays++
  37. julianFraction -= 0.5
  38. case julianFraction <= -0.5:
  39. julianDays--
  40. julianFraction += 1.5
  41. }
  42. return julianDays, julianFraction
  43. }
  44. // fractionOfADay provides a function to return the integer values for hour,
  45. // minutes, seconds and nanoseconds that comprised a given fraction of a day.
  46. // values would round to 1 us.
  47. func fractionOfADay(fraction float64) (hours, minutes, seconds, nanoseconds int) {
  48. const (
  49. c1us = 1e3
  50. c1s = 1e9
  51. c1day = 24 * 60 * 60 * c1s
  52. )
  53. frac := int64(c1day*fraction + c1us/2)
  54. nanoseconds = int((frac%c1s)/c1us) * c1us
  55. frac /= c1s
  56. seconds = int(frac % 60)
  57. frac /= 60
  58. minutes = int(frac % 60)
  59. hours = int(frac / 60)
  60. return
  61. }
  62. // julianDateToGregorianTime provides a function to convert julian date to
  63. // gregorian time.
  64. func julianDateToGregorianTime(part1, part2 float64) time.Time {
  65. part1I, part1F := math.Modf(part1)
  66. part2I, part2F := math.Modf(part2)
  67. julianDays := part1I + part2I
  68. julianFraction := part1F + part2F
  69. julianDays, julianFraction = shiftJulianToNoon(julianDays, julianFraction)
  70. day, month, year := doTheFliegelAndVanFlandernAlgorithm(int(julianDays))
  71. hours, minutes, seconds, nanoseconds := fractionOfADay(julianFraction)
  72. return time.Date(year, time.Month(month), day, hours, minutes, seconds, nanoseconds, time.UTC)
  73. }
  74. // By this point generations of programmers have repeated the algorithm sent
  75. // to the editor of "Communications of the ACM" in 1968 (published in CACM,
  76. // volume 11, number 10, October 1968, p.657). None of those programmers seems
  77. // to have found it necessary to explain the constants or variable names set
  78. // out by Henry F. Fliegel and Thomas C. Van Flandern. Maybe one day I'll buy
  79. // that jounal and expand an explanation here - that day is not today.
  80. func doTheFliegelAndVanFlandernAlgorithm(jd int) (day, month, year int) {
  81. l := jd + 68569
  82. n := (4 * l) / 146097
  83. l = l - (146097*n+3)/4
  84. i := (4000 * (l + 1)) / 1461001
  85. l = l - (1461*i)/4 + 31
  86. j := (80 * l) / 2447
  87. d := l - (2447*j)/80
  88. l = j / 11
  89. m := j + 2 - (12 * l)
  90. y := 100*(n-49) + i + l
  91. return d, m, y
  92. }
  93. // timeFromExcelTime provides a function to convert an excelTime
  94. // representation (stored as a floating point number) to a time.Time.
  95. func timeFromExcelTime(excelTime float64, date1904 bool) time.Time {
  96. const MDD int64 = 106750 // Max time.Duration Days, aprox. 290 years
  97. var date time.Time
  98. var intPart = int64(excelTime)
  99. // Excel uses Julian dates prior to March 1st 1900, and Gregorian
  100. // thereafter.
  101. if intPart <= 61 {
  102. const OFFSET1900 = 15018.0
  103. const OFFSET1904 = 16480.0
  104. const MJD0 float64 = 2400000.5
  105. var date time.Time
  106. if date1904 {
  107. date = julianDateToGregorianTime(MJD0, excelTime+OFFSET1904)
  108. } else {
  109. date = julianDateToGregorianTime(MJD0, excelTime+OFFSET1900)
  110. }
  111. return date
  112. }
  113. var floatPart = excelTime - float64(intPart)
  114. var dayNanoSeconds float64 = 24 * 60 * 60 * 1000 * 1000 * 1000
  115. if date1904 {
  116. date = time.Date(1904, 1, 1, 0, 0, 0, 0, time.UTC)
  117. } else {
  118. date = time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC)
  119. }
  120. // Duration is limited to aprox. 290 years
  121. for intPart > MDD {
  122. durationDays := time.Duration(MDD) * time.Hour * 24
  123. date = date.Add(durationDays)
  124. intPart = intPart - MDD
  125. }
  126. durationDays := time.Duration(intPart) * time.Hour * 24
  127. durationPart := time.Duration(dayNanoSeconds * floatPart)
  128. return date.Add(durationDays).Add(durationPart)
  129. }