Skip to content

Commit 90f00c1

Browse files
authored
Fix time values returned by Cell.dateValue (#115)
Resolve #114
1 parent 4fa117d commit 90f00c1

7 files changed

Lines changed: 120 additions & 16 deletions

File tree

CoreXLSX.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
D1EB9C6C21A98FFE002F2254 /* Workbook.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1C96B7F21A806A500303975 /* Workbook.swift */; };
6767
D1EB9C6D21A98FFE002F2254 /* Workbook.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1C96B7F21A806A500303975 /* Workbook.swift */; };
6868
D1EB9C6E21A98FFE002F2254 /* Workbook.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1C96B7F21A806A500303975 /* Workbook.swift */; };
69+
D1EF5CB72493C62000B30B12 /* CellQueries.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1EF5CB62493C62000B30B12 /* CellQueries.swift */; };
6970
D1FD863E21A1F09E00B7F8D6 /* SharedStrings.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1FD863D21A1F09E00B7F8D6 /* SharedStrings.swift */; };
7071
D1FD863F21A1F09E00B7F8D6 /* SharedStrings.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1FD863D21A1F09E00B7F8D6 /* SharedStrings.swift */; };
7172
D1FD864021A1F09E00B7F8D6 /* SharedStrings.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1FD863D21A1F09E00B7F8D6 /* SharedStrings.swift */; };
@@ -139,6 +140,7 @@
139140
D1C96B7F21A806A500303975 /* Workbook.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Workbook.swift; sourceTree = "<group>"; };
140141
D1C96B8121A80E5900303975 /* Workbook.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Workbook.swift; sourceTree = "<group>"; };
141142
D1EB1B3321B151440043CD1E /* SharedStrings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SharedStrings.swift; sourceTree = "<group>"; };
143+
D1EF5CB62493C62000B30B12 /* CellQueries.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CellQueries.swift; sourceTree = "<group>"; };
142144
D1FD863D21A1F09E00B7F8D6 /* SharedStrings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SharedStrings.swift; sourceTree = "<group>"; };
143145
OBJ_11 /* CellReference.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CellReference.swift; sourceTree = "<group>"; };
144146
OBJ_12 /* ColumnReference.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColumnReference.swift; sourceTree = "<group>"; };
@@ -300,6 +302,7 @@
300302
D1A8190821A9CB89004FCA33 /* Worksheet.swift */,
301303
OBJ_21 /* XCTestManifests.swift */,
302304
82AAB1C922A19EA700C30F99 /* Comments.swift */,
305+
D1EF5CB62493C62000B30B12 /* CellQueries.swift */,
303306
);
304307
name = CoreXLSXTests;
305308
path = Tests/CoreXLSXTests;
@@ -641,6 +644,7 @@
641644
OBJ_90 /* CellReference.swift in Sources */,
642645
D1EB1B3421B151440043CD1E /* SharedStrings.swift in Sources */,
643646
D1BBD9D8223D3E0E00B28C7B /* Styles.swift in Sources */,
647+
D1EF5CB72493C62000B30B12 /* CellQueries.swift in Sources */,
644648
D1A8190921A9CB89004FCA33 /* Worksheet.swift in Sources */,
645649
OBJ_91 /* XLSXFile.swift in Sources */,
646650
OBJ_92 /* Relationships.swift in Sources */,

Sources/CoreXLSX/Worksheet/Cell.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
/// [docs](http://www.datypic.com/sc/ooxml/t-ssml_ST_CellType.html)
1919
public enum CellType: String, Codable {
2020
case bool = "b"
21+
case date = "d"
2122
case number = "n"
2223
case error = "e"
2324
case sharedString = "s"

Sources/CoreXLSX/Worksheet/CellQueries.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private let referenceDate = DateComponents(
6161
second: 0,
6262
nanosecond: 0
6363
).date
64-
private let secondsInADay: Double = 86_400_000
64+
private let secondsInADay: Double = 86400
6565

6666
public extension Cell {
6767
/// Returns a string value for this cell, potentially loading a shared string value from a
@@ -94,7 +94,7 @@ public extension Cell {
9494

9595
let days = Int(floor(intervalSinceReference))
9696
let seconds = Int(
97-
floor(intervalSinceReference.truncatingRemainder(dividingBy: 1) * secondsInADay)
97+
round(intervalSinceReference.truncatingRemainder(dividingBy: 1) * secondsInADay)
9898
)
9999

100100
guard let addedDays = referenceCalendar.date(byAdding: .day, value: days, to: referenceDate)
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright 2019-2020 CoreOffice contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
// Created by Max Desiatov on 12/06/2020.
16+
//
17+
18+
@testable import CoreXLSX
19+
import XCTest
20+
21+
final class CellQueriesTests: XCTestCase {
22+
// swiftlint:disable:next function_body_length
23+
func testDates() throws {
24+
let cells = [
25+
Cell(
26+
reference: CellReference(ColumnReference("I")!, 2),
27+
type: nil,
28+
s: Optional("7"),
29+
inlineString: nil,
30+
formula: nil,
31+
value: "0.39583333333212067"
32+
),
33+
Cell(
34+
reference: CellReference(ColumnReference("J")!, 2),
35+
type: nil,
36+
s: Optional("7"),
37+
inlineString: nil,
38+
formula: nil,
39+
value: "0.54166666666787933"
40+
),
41+
Cell(
42+
reference: CellReference(ColumnReference("K")!, 2),
43+
type: nil,
44+
s: Optional("7"),
45+
inlineString: nil,
46+
formula: nil,
47+
value: "0.625"
48+
),
49+
Cell(
50+
reference: CellReference(ColumnReference("L")!, 2),
51+
type: nil,
52+
s: Optional("7"),
53+
inlineString: nil,
54+
formula: nil,
55+
value: "0.8125"
56+
),
57+
Cell(
58+
reference: CellReference(ColumnReference("O")!, 2),
59+
type: nil,
60+
s: Optional("7"),
61+
inlineString: nil,
62+
formula: nil,
63+
value: "0.625"
64+
),
65+
Cell(
66+
reference: CellReference(ColumnReference("P")!, 2),
67+
type: nil,
68+
s: Optional("7"),
69+
inlineString: nil,
70+
formula: nil,
71+
value: "0.75"
72+
),
73+
Cell(
74+
reference: CellReference(ColumnReference("U")!, 2),
75+
type: nil,
76+
s: Optional("7"),
77+
inlineString: nil,
78+
formula: nil,
79+
value: "0.33333333333212067"
80+
),
81+
Cell(
82+
reference: CellReference(ColumnReference("V")!, 2),
83+
type: nil,
84+
s: Optional("7"),
85+
inlineString: nil,
86+
formula: nil,
87+
value: "0.45833333333212067"
88+
),
89+
Cell(
90+
reference: CellReference(ColumnReference("Y")!, 2),
91+
type: nil,
92+
s: Optional("7"),
93+
inlineString: nil,
94+
formula: nil,
95+
value: "0.33333333333212067"
96+
),
97+
Cell(
98+
reference: CellReference(ColumnReference("Z")!, 2),
99+
type: nil,
100+
s: Optional("7"),
101+
inlineString: nil,
102+
formula: nil,
103+
value: "0.45833333333212067"
104+
),
105+
]
106+
107+
XCTAssertEqual(
108+
cells.compactMap { $0.dateValue?.timeIntervalSince1970 },
109+
[-2_209_127_400.0, -2_209_114_800.0, -2_209_107_600.0, -2_209_091_400.0, -2_209_107_600.0,
110+
-2_209_096_800.0, -2_209_132_800.0, -2_209_122_000.0, -2_209_132_800.0, -2_209_122_000.0]
111+
)
112+
}
113+
}

Tests/CoreXLSXTests/Comments.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,4 @@ final class CommentsTests: XCTestCase {
2727
XCTAssertEqual(comments.commentList.itemsByReference["A1"]?.text.plain,
2828
"my note")
2929
}
30-
31-
static let allTests = [
32-
("testComments", testComments),
33-
]
3430
}

Tests/CoreXLSXTests/SharedStrings.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,4 @@ final class SharedStringsTests: XCTestCase {
143143
}
144144
XCTAssertEqual(text, "CellA2")
145145
}
146-
147-
static let allTests = [
148-
("testSharedStrings", testSharedStrings),
149-
("testSharedStringsOrder", testSharedStringsOrder),
150-
("testSharedStringsRichText", testRichTextXML),
151-
]
152146
}

Tests/CoreXLSXTests/Styles.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,4 @@ final class StylesTests: XCTestCase {
106106
XCTAssertEqual(styles.tableStyles!.count, 0)
107107
XCTAssertEqual(styles.colors!.indexed.rgbColors.count, 14)
108108
}
109-
110-
static let allTests = [
111-
("testStyles", testStyles),
112-
]
113109
}

0 commit comments

Comments
 (0)