date.go 6.0 KB

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