Skip to content

Commit

Permalink
ref: Add nanosecondsToTimeInterval (#3483)
Browse files Browse the repository at this point in the history
SentryTime has timeIntervalToNanoseconds but not
nanosecondsToTimeInterval. This PR adds nanosecondsToTimeInterval.

Co-authored-by: Andrew McKnight <andrew.mcknight@sentry.io>
  • Loading branch information
philipphofmann and armcknight authored Dec 6, 2023
1 parent 20f4f5d commit 29d558e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Sentry.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
62A456E52B0370E0003F19A1 /* SentryUIEventTrackerTransactionMode.m in Sources */ = {isa = PBXBuildFile; fileRef = 62A456E42B0370E0003F19A1 /* SentryUIEventTrackerTransactionMode.m */; };
62B86CFC29F052BB008F3947 /* SentryTestLogConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = 62B86CFB29F052BB008F3947 /* SentryTestLogConfig.m */; };
62C25C862B075F4900C68CBD /* TestOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62C25C852B075F4900C68CBD /* TestOptions.swift */; };
62C3168B2B1F865A000D7031 /* SentryTimeSwiftTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62C3168A2B1F865A000D7031 /* SentryTimeSwiftTests.swift */; };
62E081A929ED4260000F69FC /* SentryBreadcrumbDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 62E081A829ED4260000F69FC /* SentryBreadcrumbDelegate.h */; };
62E081AB29ED4322000F69FC /* SentryBreadcrumbTestDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E081AA29ED4322000F69FC /* SentryBreadcrumbTestDelegate.swift */; };
62F226B729A37C120038080D /* SentryBooleanSerialization.m in Sources */ = {isa = PBXBuildFile; fileRef = 62F226B629A37C120038080D /* SentryBooleanSerialization.m */; };
Expand Down Expand Up @@ -1002,6 +1003,7 @@
62A456E42B0370E0003F19A1 /* SentryUIEventTrackerTransactionMode.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryUIEventTrackerTransactionMode.m; sourceTree = "<group>"; };
62B86CFB29F052BB008F3947 /* SentryTestLogConfig.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryTestLogConfig.m; sourceTree = "<group>"; };
62C25C852B075F4900C68CBD /* TestOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestOptions.swift; sourceTree = "<group>"; };
62C3168A2B1F865A000D7031 /* SentryTimeSwiftTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryTimeSwiftTests.swift; sourceTree = "<group>"; };
62E081A829ED4260000F69FC /* SentryBreadcrumbDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryBreadcrumbDelegate.h; path = include/SentryBreadcrumbDelegate.h; sourceTree = "<group>"; };
62E081AA29ED4322000F69FC /* SentryBreadcrumbTestDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryBreadcrumbTestDelegate.swift; sourceTree = "<group>"; };
62F226B629A37C120038080D /* SentryBooleanSerialization.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryBooleanSerialization.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2854,6 +2856,7 @@
84A8892028DBD8D600C51DFD /* SentryDeviceTests.mm */,
D85790282976A69F00C6AC1F /* TestDebugImageProvider.swift */,
8431EE5A29ADB8EA00D8DC56 /* SentryTimeTests.m */,
62C3168A2B1F865A000D7031 /* SentryTimeSwiftTests.swift */,
33042A1629DC2C4300C60085 /* SentryExtraContextProviderTests.swift */,
);
path = Helper;
Expand Down Expand Up @@ -4403,6 +4406,7 @@
63EED6C32237989300E02400 /* SentryOptionsTest.m in Sources */,
7BBD18B22451804C00427C76 /* SentryRetryAfterHeaderParserTests.swift in Sources */,
7BD337E424A356180050DB6E /* SentryCrashIntegrationTests.swift in Sources */,
62C3168B2B1F865A000D7031 /* SentryTimeSwiftTests.swift in Sources */,
7BD4E8E827FD95900086C410 /* SentryMigrateSessionInitTests.m in Sources */,
7B6CC50224EE5A42001816D7 /* SentryHubTests.swift in Sources */,
7BF9EF882722D13000B5BBEF /* SentryTestObjCRuntimeWrapper.m in Sources */,
Expand Down
10 changes: 8 additions & 2 deletions Sources/Sentry/SentryTime.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@
timeIntervalToNanoseconds(double seconds)
{
NSCAssert(seconds >= 0, @"Seconds must be a positive value");
NSCAssert(seconds <= UINT64_MAX / 1e9,
NSCAssert(seconds <= (double)UINT64_MAX / (double)NSEC_PER_SEC,
@"Value of seconds is too great; will overflow if casted to a uint64_t");
return (uint64_t)(seconds * 1e9);
return (uint64_t)(seconds * NSEC_PER_SEC);
}
double
nanosecondsToTimeInterval(uint64_t nanoseconds)
{
return (double)nanoseconds / NSEC_PER_SEC;
}
uint64_t
Expand Down
3 changes: 3 additions & 0 deletions Sources/Sentry/include/SentryTime.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ SENTRY_EXTERN_C_BEGIN
*/
uint64_t timeIntervalToNanoseconds(double seconds);

/** Converts integer nanoseconds to a @c NSTimeInterval. */
double nanosecondsToTimeInterval(uint64_t nanoseconds);

/**
* Returns the absolute timestamp, which has no defined reference point or unit
* as it is platform dependent.
Expand Down
21 changes: 21 additions & 0 deletions Tests/SentryTests/Helper/SentryTimeSwiftTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Nimble
import XCTest

final class SentryTimeSwiftTests: XCTestCase {

func testTimeIntervalToNanoseconds() {
expect(timeIntervalToNanoseconds(0.0)) == UInt64(0)
expect(timeIntervalToNanoseconds(0.5)) == UInt64(500_000_000)
expect(timeIntervalToNanoseconds(1.0)) == UInt64(1_000_000_000)
expect(timeIntervalToNanoseconds(1.123456789)) == UInt64(1_123_456_789)
expect(timeIntervalToNanoseconds(123_456_789.123456)) == UInt64(123_456_789_123_456_000)
}

func testNanosecondsToTimeInterval() {
expect(nanosecondsToTimeInterval(0)).to(beCloseTo(0.0, within: 1e-9))
expect(nanosecondsToTimeInterval(500_000_000)).to(beCloseTo(0.5, within: 1e-9))
expect(nanosecondsToTimeInterval(1_000_000_000)).to(beCloseTo(1.0, within: 1e-9))
expect(nanosecondsToTimeInterval(1_123_456_789)).to(beCloseTo(1.123456789, within: 1e-9))
expect(nanosecondsToTimeInterval(123_456_789_123_456_000)).to(beCloseTo(123_456_789.123456, within: 1e-9))
}
}

0 comments on commit 29d558e

Please sign in to comment.