/src/swift-protobuf/Sources/SwiftProtobuf/TimeUtils.swift
Line | Count | Source |
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 | | |
15 | | let minutesPerDay: Int32 = 1440 |
16 | | let minutesPerHour: Int32 = 60 |
17 | | let secondsPerDay: Int32 = 86400 |
18 | | let secondsPerHour: Int32 = 3600 |
19 | | let secondsPerMinute: Int32 = 60 |
20 | | let nanosPerSecond: Int32 = 1_000_000_000 |
21 | | let attosPerNanosecond: Int64 = 1_000_000_000 |
22 | | |
23 | 0 | internal func timeOfDayFromSecondsSince1970(seconds: Int64) -> (hh: Int32, mm: Int32, ss: Int32) { |
24 | 0 | let secondsSinceMidnight = Int32(mod(seconds, Int64(secondsPerDay))) |
25 | 0 | let ss = mod(secondsSinceMidnight, secondsPerMinute) |
26 | 0 | let mm = mod(div(secondsSinceMidnight, secondsPerMinute), minutesPerHour) |
27 | 0 | let hh = Int32(div(secondsSinceMidnight, secondsPerHour)) |
28 | 0 |
|
29 | 0 | return (hh: hh, mm: mm, ss: ss) |
30 | 0 | } |
31 | | |
32 | 0 | internal func julianDayNumberFromSecondsSince1970(seconds: Int64) -> Int64 { |
33 | 0 | // January 1, 1970 is Julian Day Number 2440588. |
34 | 0 | // See http://aa.usno.navy.mil/faq/docs/JD_Formula.php |
35 | 0 | div(seconds + 2_440_588 * Int64(secondsPerDay), Int64(secondsPerDay)) |
36 | 0 | } |
37 | | |
38 | 0 | internal func gregorianDateFromSecondsSince1970(seconds: Int64) -> (YY: Int32, MM: Int32, DD: Int32) { |
39 | 0 | // The following implements Richards' algorithm (see the Wikipedia article |
40 | 0 | // for "Julian day"). |
41 | 0 | // If you touch this code, please test it exhaustively by playing with |
42 | 0 | // Test_Timestamp.testJSON_range. |
43 | 0 |
|
44 | 0 | let JJ = julianDayNumberFromSecondsSince1970(seconds: seconds) |
45 | 0 | let f = JJ + 1401 + div(div(4 * JJ + 274277, 146097) * 3, 4) - 38 |
46 | 0 | let e = 4 * f + 3 |
47 | 0 | let g = Int64(div(mod(e, 1461), 4)) |
48 | 0 | let h = 5 * g + 2 |
49 | 0 | let DD = div(mod(h, 153), 5) + 1 |
50 | 0 | let MM = mod(div(h, 153) + 2, 12) + 1 |
51 | 0 | let YY = div(e, 1461) - 4716 + div(12 + 2 - MM, 12) |
52 | 0 |
|
53 | 0 | return (YY: Int32(YY), MM: Int32(MM), DD: Int32(DD)) |
54 | 0 | } |
55 | | |
56 | 308 | internal func nanosToString(nanos: Int32) -> String { |
57 | 308 | if nanos == 0 { |
58 | 116 | return "" |
59 | 192 | } else if nanos % 1_000_000 == 0 { |
60 | 109 | return ".\(threeDigit(abs(nanos) / 1_000_000))" |
61 | 109 | } else if nanos % 1000 == 0 { |
62 | 51 | return ".\(sixDigit(abs(nanos) / 1000))" |
63 | 51 | } else { |
64 | 32 | return ".\(nineDigit(abs(nanos)))" |
65 | 32 | } |
66 | 308 | } |