Skip to content

Commit

Permalink
Fix time values returned by Cell.dateValue (#115)
Browse files Browse the repository at this point in the history
Resolve #114
  • Loading branch information
MaxDesiatov authored Jun 12, 2020
1 parent 4fa117d commit 90f00c1
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CoreXLSX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
D1EB9C6C21A98FFE002F2254 /* Workbook.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1C96B7F21A806A500303975 /* Workbook.swift */; };
D1EB9C6D21A98FFE002F2254 /* Workbook.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1C96B7F21A806A500303975 /* Workbook.swift */; };
D1EB9C6E21A98FFE002F2254 /* Workbook.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1C96B7F21A806A500303975 /* Workbook.swift */; };
D1EF5CB72493C62000B30B12 /* CellQueries.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1EF5CB62493C62000B30B12 /* CellQueries.swift */; };
D1FD863E21A1F09E00B7F8D6 /* SharedStrings.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1FD863D21A1F09E00B7F8D6 /* SharedStrings.swift */; };
D1FD863F21A1F09E00B7F8D6 /* SharedStrings.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1FD863D21A1F09E00B7F8D6 /* SharedStrings.swift */; };
D1FD864021A1F09E00B7F8D6 /* SharedStrings.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1FD863D21A1F09E00B7F8D6 /* SharedStrings.swift */; };
Expand Down Expand Up @@ -139,6 +140,7 @@
D1C96B7F21A806A500303975 /* Workbook.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Workbook.swift; sourceTree = "<group>"; };
D1C96B8121A80E5900303975 /* Workbook.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Workbook.swift; sourceTree = "<group>"; };
D1EB1B3321B151440043CD1E /* SharedStrings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SharedStrings.swift; sourceTree = "<group>"; };
D1EF5CB62493C62000B30B12 /* CellQueries.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CellQueries.swift; sourceTree = "<group>"; };
D1FD863D21A1F09E00B7F8D6 /* SharedStrings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SharedStrings.swift; sourceTree = "<group>"; };
OBJ_11 /* CellReference.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CellReference.swift; sourceTree = "<group>"; };
OBJ_12 /* ColumnReference.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColumnReference.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -300,6 +302,7 @@
D1A8190821A9CB89004FCA33 /* Worksheet.swift */,
OBJ_21 /* XCTestManifests.swift */,
82AAB1C922A19EA700C30F99 /* Comments.swift */,
D1EF5CB62493C62000B30B12 /* CellQueries.swift */,
);
name = CoreXLSXTests;
path = Tests/CoreXLSXTests;
Expand Down Expand Up @@ -641,6 +644,7 @@
OBJ_90 /* CellReference.swift in Sources */,
D1EB1B3421B151440043CD1E /* SharedStrings.swift in Sources */,
D1BBD9D8223D3E0E00B28C7B /* Styles.swift in Sources */,
D1EF5CB72493C62000B30B12 /* CellQueries.swift in Sources */,
D1A8190921A9CB89004FCA33 /* Worksheet.swift in Sources */,
OBJ_91 /* XLSXFile.swift in Sources */,
OBJ_92 /* Relationships.swift in Sources */,
Expand Down
1 change: 1 addition & 0 deletions Sources/CoreXLSX/Worksheet/Cell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
/// [docs](http://www.datypic.com/sc/ooxml/t-ssml_ST_CellType.html)
public enum CellType: String, Codable {
case bool = "b"
case date = "d"
case number = "n"
case error = "e"
case sharedString = "s"
Expand Down
4 changes: 2 additions & 2 deletions Sources/CoreXLSX/Worksheet/CellQueries.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private let referenceDate = DateComponents(
second: 0,
nanosecond: 0
).date
private let secondsInADay: Double = 86_400_000
private let secondsInADay: Double = 86400

public extension Cell {
/// Returns a string value for this cell, potentially loading a shared string value from a
Expand Down Expand Up @@ -94,7 +94,7 @@ public extension Cell {

let days = Int(floor(intervalSinceReference))
let seconds = Int(
floor(intervalSinceReference.truncatingRemainder(dividingBy: 1) * secondsInADay)
round(intervalSinceReference.truncatingRemainder(dividingBy: 1) * secondsInADay)
)

guard let addedDays = referenceCalendar.date(byAdding: .day, value: days, to: referenceDate)
Expand Down
113 changes: 113 additions & 0 deletions Tests/CoreXLSXTests/CellQueries.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright 2019-2020 CoreOffice contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Created by Max Desiatov on 12/06/2020.
//

@testable import CoreXLSX
import XCTest

final class CellQueriesTests: XCTestCase {
// swiftlint:disable:next function_body_length
func testDates() throws {
let cells = [
Cell(
reference: CellReference(ColumnReference("I")!, 2),
type: nil,
s: Optional("7"),
inlineString: nil,
formula: nil,
value: "0.39583333333212067"
),
Cell(
reference: CellReference(ColumnReference("J")!, 2),
type: nil,
s: Optional("7"),
inlineString: nil,
formula: nil,
value: "0.54166666666787933"
),
Cell(
reference: CellReference(ColumnReference("K")!, 2),
type: nil,
s: Optional("7"),
inlineString: nil,
formula: nil,
value: "0.625"
),
Cell(
reference: CellReference(ColumnReference("L")!, 2),
type: nil,
s: Optional("7"),
inlineString: nil,
formula: nil,
value: "0.8125"
),
Cell(
reference: CellReference(ColumnReference("O")!, 2),
type: nil,
s: Optional("7"),
inlineString: nil,
formula: nil,
value: "0.625"
),
Cell(
reference: CellReference(ColumnReference("P")!, 2),
type: nil,
s: Optional("7"),
inlineString: nil,
formula: nil,
value: "0.75"
),
Cell(
reference: CellReference(ColumnReference("U")!, 2),
type: nil,
s: Optional("7"),
inlineString: nil,
formula: nil,
value: "0.33333333333212067"
),
Cell(
reference: CellReference(ColumnReference("V")!, 2),
type: nil,
s: Optional("7"),
inlineString: nil,
formula: nil,
value: "0.45833333333212067"
),
Cell(
reference: CellReference(ColumnReference("Y")!, 2),
type: nil,
s: Optional("7"),
inlineString: nil,
formula: nil,
value: "0.33333333333212067"
),
Cell(
reference: CellReference(ColumnReference("Z")!, 2),
type: nil,
s: Optional("7"),
inlineString: nil,
formula: nil,
value: "0.45833333333212067"
),
]

XCTAssertEqual(
cells.compactMap { $0.dateValue?.timeIntervalSince1970 },
[-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,
-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]
)
}
}
4 changes: 0 additions & 4 deletions Tests/CoreXLSXTests/Comments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,4 @@ final class CommentsTests: XCTestCase {
XCTAssertEqual(comments.commentList.itemsByReference["A1"]?.text.plain,
"my note")
}

static let allTests = [
("testComments", testComments),
]
}
6 changes: 0 additions & 6 deletions Tests/CoreXLSXTests/SharedStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,4 @@ final class SharedStringsTests: XCTestCase {
}
XCTAssertEqual(text, "CellA2")
}

static let allTests = [
("testSharedStrings", testSharedStrings),
("testSharedStringsOrder", testSharedStringsOrder),
("testSharedStringsRichText", testRichTextXML),
]
}
4 changes: 0 additions & 4 deletions Tests/CoreXLSXTests/Styles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,4 @@ final class StylesTests: XCTestCase {
XCTAssertEqual(styles.tableStyles!.count, 0)
XCTAssertEqual(styles.colors!.indexed.rgbColors.count, 14)
}

static let allTests = [
("testStyles", testStyles),
]
}

0 comments on commit 90f00c1

Please sign in to comment.