Skip to content

Commit 29d558e

Browse files
ref: Add nanosecondsToTimeInterval (#3483)
SentryTime has timeIntervalToNanoseconds but not nanosecondsToTimeInterval. This PR adds nanosecondsToTimeInterval. Co-authored-by: Andrew McKnight <[email protected]>
1 parent 20f4f5d commit 29d558e

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

Sentry.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
62A456E52B0370E0003F19A1 /* SentryUIEventTrackerTransactionMode.m in Sources */ = {isa = PBXBuildFile; fileRef = 62A456E42B0370E0003F19A1 /* SentryUIEventTrackerTransactionMode.m */; };
9191
62B86CFC29F052BB008F3947 /* SentryTestLogConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = 62B86CFB29F052BB008F3947 /* SentryTestLogConfig.m */; };
9292
62C25C862B075F4900C68CBD /* TestOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62C25C852B075F4900C68CBD /* TestOptions.swift */; };
93+
62C3168B2B1F865A000D7031 /* SentryTimeSwiftTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62C3168A2B1F865A000D7031 /* SentryTimeSwiftTests.swift */; };
9394
62E081A929ED4260000F69FC /* SentryBreadcrumbDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 62E081A829ED4260000F69FC /* SentryBreadcrumbDelegate.h */; };
9495
62E081AB29ED4322000F69FC /* SentryBreadcrumbTestDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E081AA29ED4322000F69FC /* SentryBreadcrumbTestDelegate.swift */; };
9596
62F226B729A37C120038080D /* SentryBooleanSerialization.m in Sources */ = {isa = PBXBuildFile; fileRef = 62F226B629A37C120038080D /* SentryBooleanSerialization.m */; };
@@ -1002,6 +1003,7 @@
10021003
62A456E42B0370E0003F19A1 /* SentryUIEventTrackerTransactionMode.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryUIEventTrackerTransactionMode.m; sourceTree = "<group>"; };
10031004
62B86CFB29F052BB008F3947 /* SentryTestLogConfig.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryTestLogConfig.m; sourceTree = "<group>"; };
10041005
62C25C852B075F4900C68CBD /* TestOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestOptions.swift; sourceTree = "<group>"; };
1006+
62C3168A2B1F865A000D7031 /* SentryTimeSwiftTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryTimeSwiftTests.swift; sourceTree = "<group>"; };
10051007
62E081A829ED4260000F69FC /* SentryBreadcrumbDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryBreadcrumbDelegate.h; path = include/SentryBreadcrumbDelegate.h; sourceTree = "<group>"; };
10061008
62E081AA29ED4322000F69FC /* SentryBreadcrumbTestDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryBreadcrumbTestDelegate.swift; sourceTree = "<group>"; };
10071009
62F226B629A37C120038080D /* SentryBooleanSerialization.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryBooleanSerialization.m; sourceTree = "<group>"; };
@@ -2854,6 +2856,7 @@
28542856
84A8892028DBD8D600C51DFD /* SentryDeviceTests.mm */,
28552857
D85790282976A69F00C6AC1F /* TestDebugImageProvider.swift */,
28562858
8431EE5A29ADB8EA00D8DC56 /* SentryTimeTests.m */,
2859+
62C3168A2B1F865A000D7031 /* SentryTimeSwiftTests.swift */,
28572860
33042A1629DC2C4300C60085 /* SentryExtraContextProviderTests.swift */,
28582861
);
28592862
path = Helper;
@@ -4403,6 +4406,7 @@
44034406
63EED6C32237989300E02400 /* SentryOptionsTest.m in Sources */,
44044407
7BBD18B22451804C00427C76 /* SentryRetryAfterHeaderParserTests.swift in Sources */,
44054408
7BD337E424A356180050DB6E /* SentryCrashIntegrationTests.swift in Sources */,
4409+
62C3168B2B1F865A000D7031 /* SentryTimeSwiftTests.swift in Sources */,
44064410
7BD4E8E827FD95900086C410 /* SentryMigrateSessionInitTests.m in Sources */,
44074411
7B6CC50224EE5A42001816D7 /* SentryHubTests.swift in Sources */,
44084412
7BF9EF882722D13000B5BBEF /* SentryTestObjCRuntimeWrapper.m in Sources */,

Sources/Sentry/SentryTime.mm

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@
1010
timeIntervalToNanoseconds(double seconds)
1111
{
1212
NSCAssert(seconds >= 0, @"Seconds must be a positive value");
13-
NSCAssert(seconds <= UINT64_MAX / 1e9,
13+
NSCAssert(seconds <= (double)UINT64_MAX / (double)NSEC_PER_SEC,
1414
@"Value of seconds is too great; will overflow if casted to a uint64_t");
15-
return (uint64_t)(seconds * 1e9);
15+
return (uint64_t)(seconds * NSEC_PER_SEC);
16+
}
17+
18+
double
19+
nanosecondsToTimeInterval(uint64_t nanoseconds)
20+
{
21+
return (double)nanoseconds / NSEC_PER_SEC;
1622
}
1723
1824
uint64_t

Sources/Sentry/include/SentryTime.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ SENTRY_EXTERN_C_BEGIN
1111
*/
1212
uint64_t timeIntervalToNanoseconds(double seconds);
1313

14+
/** Converts integer nanoseconds to a @c NSTimeInterval. */
15+
double nanosecondsToTimeInterval(uint64_t nanoseconds);
16+
1417
/**
1518
* Returns the absolute timestamp, which has no defined reference point or unit
1619
* as it is platform dependent.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Nimble
2+
import XCTest
3+
4+
final class SentryTimeSwiftTests: XCTestCase {
5+
6+
func testTimeIntervalToNanoseconds() {
7+
expect(timeIntervalToNanoseconds(0.0)) == UInt64(0)
8+
expect(timeIntervalToNanoseconds(0.5)) == UInt64(500_000_000)
9+
expect(timeIntervalToNanoseconds(1.0)) == UInt64(1_000_000_000)
10+
expect(timeIntervalToNanoseconds(1.123456789)) == UInt64(1_123_456_789)
11+
expect(timeIntervalToNanoseconds(123_456_789.123456)) == UInt64(123_456_789_123_456_000)
12+
}
13+
14+
func testNanosecondsToTimeInterval() {
15+
expect(nanosecondsToTimeInterval(0)).to(beCloseTo(0.0, within: 1e-9))
16+
expect(nanosecondsToTimeInterval(500_000_000)).to(beCloseTo(0.5, within: 1e-9))
17+
expect(nanosecondsToTimeInterval(1_000_000_000)).to(beCloseTo(1.0, within: 1e-9))
18+
expect(nanosecondsToTimeInterval(1_123_456_789)).to(beCloseTo(1.123456789, within: 1e-9))
19+
expect(nanosecondsToTimeInterval(123_456_789_123_456_000)).to(beCloseTo(123_456_789.123456, within: 1e-9))
20+
}
21+
}

0 commit comments

Comments
 (0)