TimeUtils.swift 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Sources/SwiftProtobuf/TimeUtils.swift - Generally useful time/calendar functions
  2. //
  3. // Copyright (c) 2014 - 2017 Apple Inc. and the project authors
  4. // Licensed under Apache License v2.0 with Runtime Library Exception
  5. //
  6. // See LICENSE.txt for license information:
  7. // https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
  8. //
  9. // -----------------------------------------------------------------------------
  10. ///
  11. /// Generally useful time/calendar functions and constants
  12. ///
  13. // -----------------------------------------------------------------------------
  14. let minutesPerDay: Int32 = 1440
  15. let minutesPerHour: Int32 = 60
  16. let secondsPerDay: Int32 = 86400
  17. let secondsPerHour: Int32 = 3600
  18. let secondsPerMinute: Int32 = 60
  19. let nanosPerSecond: Int32 = 1_000_000_000
  20. internal func timeOfDayFromSecondsSince1970(seconds: Int64) -> (hh: Int32, mm: Int32, ss: Int32) {
  21. let secondsSinceMidnight = Int32(mod(seconds, Int64(secondsPerDay)))
  22. let ss = mod(secondsSinceMidnight, secondsPerMinute)
  23. let mm = mod(div(secondsSinceMidnight, secondsPerMinute), minutesPerHour)
  24. let hh = Int32(div(secondsSinceMidnight, secondsPerHour))
  25. return (hh: hh, mm: mm, ss: ss)
  26. }
  27. internal func julianDayNumberFromSecondsSince1970(seconds: Int64) -> Int64 {
  28. // January 1, 1970 is Julian Day Number 2440588.
  29. // See http://aa.usno.navy.mil/faq/docs/JD_Formula.php
  30. div(seconds + 2_440_588 * Int64(secondsPerDay), Int64(secondsPerDay))
  31. }
  32. internal func gregorianDateFromSecondsSince1970(seconds: Int64) -> (YY: Int32, MM: Int32, DD: Int32) {
  33. // The following implements Richards' algorithm (see the Wikipedia article
  34. // for "Julian day").
  35. // If you touch this code, please test it exhaustively by playing with
  36. // Test_Timestamp.testJSON_range.
  37. let JJ = julianDayNumberFromSecondsSince1970(seconds: seconds)
  38. let f = JJ + 1401 + div(div(4 * JJ + 274277, 146097) * 3, 4) - 38
  39. let e = 4 * f + 3
  40. let g = Int64(div(mod(e, 1461), 4))
  41. let h = 5 * g + 2
  42. let DD = div(mod(h, 153), 5) + 1
  43. let MM = mod(div(h, 153) + 2, 12) + 1
  44. let YY = div(e, 1461) - 4716 + div(12 + 2 - MM, 12)
  45. return (YY: Int32(YY), MM: Int32(MM), DD: Int32(DD))
  46. }
  47. internal func nanosToString(nanos: Int32) -> String {
  48. if nanos == 0 {
  49. return ""
  50. } else if nanos % 1_000_000 == 0 {
  51. return ".\(threeDigit(abs(nanos) / 1_000_000))"
  52. } else if nanos % 1000 == 0 {
  53. return ".\(sixDigit(abs(nanos) / 1000))"
  54. } else {
  55. return ".\(nineDigit(abs(nanos)))"
  56. }
  57. }