date.go 6.1 KB

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