date.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
  2. // this source code is governed by a BSD-style license that can be found in
  3. // the LICENSE file.
  4. //
  5. // Package excelize providing a set of functions that allow you to write to
  6. // and read from XLSX files. Support reads and writes XLSX file generated by
  7. // Microsoft Excel™ 2007 and later. Support save file without losing original
  8. // charts of XLSX. This library needs Go version 1.8 or later.
  9. package excelize
  10. import (
  11. "math"
  12. "time"
  13. )
  14. const (
  15. dayNanoseconds = 24 * time.Hour
  16. maxDuration = 290 * 364 * dayNanoseconds
  17. )
  18. var (
  19. excelMinTime1900 = time.Date(1899, time.December, 31, 0, 0, 0, 0, time.UTC)
  20. excelBuggyPeriodStart = time.Date(1900, time.March, 1, 0, 0, 0, 0, time.UTC).Add(-time.Nanosecond)
  21. )
  22. // timeToExcelTime provides a function to convert time to Excel time.
  23. func timeToExcelTime(t time.Time) float64 {
  24. // TODO in future this should probably also handle date1904 and like TimeFromExcelTime
  25. // Force user to explicit convet passed value to UTC time.
  26. // Because for example 1900-01-01 00:00:00 +0300 MSK converts to 1900-01-01 00:00:00 +0230 LMT
  27. // probably due to daylight saving.
  28. if t.Location() != time.UTC {
  29. panic("only UTC time expected")
  30. }
  31. if t.Before(excelMinTime1900) {
  32. return 0.0
  33. }
  34. tt := t
  35. diff := t.Sub(excelMinTime1900)
  36. result := float64(0)
  37. for diff >= maxDuration {
  38. result += float64(maxDuration / dayNanoseconds)
  39. tt = tt.Add(-maxDuration)
  40. diff = tt.Sub(excelMinTime1900)
  41. }
  42. rem := diff % dayNanoseconds
  43. result += float64(diff-rem)/float64(dayNanoseconds) + float64(rem)/float64(dayNanoseconds)
  44. // Excel dates after 28th February 1900 are actually one day out.
  45. // Excel behaves as though the date 29th February 1900 existed, which it didn't.
  46. // Microsoft intentionally included this bug in Excel so that it would remain compatible with the spreadsheet
  47. // program that had the majority market share at the time; Lotus 1-2-3.
  48. // https://www.myonlinetraininghub.com/excel-date-and-time
  49. if t.After(excelBuggyPeriodStart) {
  50. result += 1.0
  51. }
  52. return result
  53. }
  54. // shiftJulianToNoon provides a function to process julian date to noon.
  55. func shiftJulianToNoon(julianDays, julianFraction float64) (float64, float64) {
  56. switch {
  57. case -0.5 < julianFraction && julianFraction < 0.5:
  58. julianFraction += 0.5
  59. case julianFraction >= 0.5:
  60. julianDays++
  61. julianFraction -= 0.5
  62. case julianFraction <= -0.5:
  63. julianDays--
  64. julianFraction += 1.5
  65. }
  66. return julianDays, julianFraction
  67. }
  68. // fractionOfADay provides a function to return the integer values for hour,
  69. // minutes, seconds and nanoseconds that comprised a given fraction of a day.
  70. // values would round to 1 us.
  71. func fractionOfADay(fraction float64) (hours, minutes, seconds, nanoseconds int) {
  72. const (
  73. c1us = 1e3
  74. c1s = 1e9
  75. c1day = 24 * 60 * 60 * c1s
  76. )
  77. frac := int64(c1day*fraction + c1us/2)
  78. nanoseconds = int((frac%c1s)/c1us) * c1us
  79. frac /= c1s
  80. seconds = int(frac % 60)
  81. frac /= 60
  82. minutes = int(frac % 60)
  83. hours = int(frac / 60)
  84. return
  85. }
  86. // julianDateToGregorianTime provides a function to convert julian date to
  87. // gregorian time.
  88. func julianDateToGregorianTime(part1, part2 float64) time.Time {
  89. part1I, part1F := math.Modf(part1)
  90. part2I, part2F := math.Modf(part2)
  91. julianDays := part1I + part2I
  92. julianFraction := part1F + part2F
  93. julianDays, julianFraction = shiftJulianToNoon(julianDays, julianFraction)
  94. day, month, year := doTheFliegelAndVanFlandernAlgorithm(int(julianDays))
  95. hours, minutes, seconds, nanoseconds := fractionOfADay(julianFraction)
  96. return time.Date(year, time.Month(month), day, hours, minutes, seconds, nanoseconds, time.UTC)
  97. }
  98. // doTheFliegelAndVanFlandernAlgorithm; By this point generations of
  99. // programmers have repeated the algorithm sent to the editor of
  100. // "Communications of the ACM" in 1968 (published in CACM, volume 11, number
  101. // 10, October 1968, p.657). None of those programmers seems to have found it
  102. // necessary to explain the constants or variable names set out by Henry F.
  103. // Fliegel and Thomas C. Van Flandern. Maybe one day I'll buy that jounal and
  104. // expand an explanation here - that day is not today.
  105. func doTheFliegelAndVanFlandernAlgorithm(jd int) (day, month, year int) {
  106. l := jd + 68569
  107. n := (4 * l) / 146097
  108. l = l - (146097*n+3)/4
  109. i := (4000 * (l + 1)) / 1461001
  110. l = l - (1461*i)/4 + 31
  111. j := (80 * l) / 2447
  112. d := l - (2447*j)/80
  113. l = j / 11
  114. m := j + 2 - (12 * l)
  115. y := 100*(n-49) + i + l
  116. return d, m, y
  117. }
  118. // timeFromExcelTime provides a function to convert an excelTime
  119. // representation (stored as a floating point number) to a time.Time.
  120. func timeFromExcelTime(excelTime float64, date1904 bool) time.Time {
  121. const MDD int64 = 106750 // Max time.Duration Days, aprox. 290 years
  122. var date time.Time
  123. var intPart = int64(excelTime)
  124. // Excel uses Julian dates prior to March 1st 1900, and Gregorian
  125. // thereafter.
  126. if intPart <= 61 {
  127. const OFFSET1900 = 15018.0
  128. const OFFSET1904 = 16480.0
  129. const MJD0 float64 = 2400000.5
  130. var date time.Time
  131. if date1904 {
  132. date = julianDateToGregorianTime(MJD0, excelTime+OFFSET1904)
  133. } else {
  134. date = julianDateToGregorianTime(MJD0, excelTime+OFFSET1900)
  135. }
  136. return date
  137. }
  138. var floatPart = excelTime - float64(intPart)
  139. var dayNanoSeconds float64 = 24 * 60 * 60 * 1000 * 1000 * 1000
  140. if date1904 {
  141. date = time.Date(1904, 1, 1, 0, 0, 0, 0, time.UTC)
  142. } else {
  143. date = time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC)
  144. }
  145. // Duration is limited to aprox. 290 years
  146. for intPart > MDD {
  147. durationDays := time.Duration(MDD) * time.Hour * 24
  148. date = date.Add(durationDays)
  149. intPart = intPart - MDD
  150. }
  151. durationDays := time.Duration(intPart) * time.Hour * 24
  152. durationPart := time.Duration(dayNanoSeconds * floatPart)
  153. return date.Add(durationDays).Add(durationPart)
  154. }