date.go 5.7 KB

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