This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ios] Fixed conversion between Duration and NSTimeInerval (#8276)
- Loading branch information
1 parent
8f89275
commit 1f1cc13
Showing
10 changed files
with
126 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
#import <Foundation/Foundation.h> | ||
|
||
#import "MGLFoundation.h" | ||
#include <mbgl/util/chrono.hpp> | ||
|
||
@interface NSDate (MGLAdditions) | ||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
|
||
/// Converts from a duration in seconds to a duration object usable in mbgl. | ||
mbgl::Duration MGLDurationInSecondsFromTimeInterval(NSTimeInterval duration); | ||
MGL_EXPORT | ||
mbgl::Duration MGLDurationFromTimeInterval(NSTimeInterval duration); | ||
|
||
/// Converts from an mbgl duration object to a duration in seconds. | ||
NSTimeInterval MGLTimeIntervalFromDurationInSeconds(mbgl::Duration duration); | ||
MGL_EXPORT | ||
NSTimeInterval MGLTimeIntervalFromDuration(mbgl::Duration duration); | ||
|
||
@end | ||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
#import "NSDate+MGLAdditions.h" | ||
|
||
@implementation NSDate (MGLAdditions) | ||
|
||
mbgl::Duration MGLDurationInSecondsFromTimeInterval(NSTimeInterval duration) | ||
mbgl::Duration MGLDurationFromTimeInterval(NSTimeInterval duration) | ||
{ | ||
return std::chrono::duration_cast<mbgl::Duration>(std::chrono::duration<NSTimeInterval>(duration)); | ||
} | ||
|
||
NSTimeInterval MGLTimeIntervalFromDurationInSeconds(mbgl::Duration duration) | ||
NSTimeInterval MGLTimeIntervalFromDuration(mbgl::Duration duration) | ||
{ | ||
return duration.count(); | ||
std::chrono::nanoseconds nano(duration.count()); | ||
std::chrono::seconds sec = std::chrono::duration_cast<std::chrono::seconds>(nano); | ||
|
||
return sec.count(); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#import <XCTest/XCTest.h> | ||
|
||
#include <mbgl/util/chrono.hpp> | ||
#import "../../darwin/src/NSDate+MGLAdditions.h" | ||
|
||
using namespace std::chrono_literals; | ||
|
||
@interface MGLNSDateAdditionsTests : XCTestCase | ||
@end | ||
|
||
@implementation MGLNSDateAdditionsTests | ||
|
||
- (void)testDurationToNSTimeInterval { | ||
|
||
NSTimeInterval timeInterval = 5; | ||
mbgl::Duration duration = MGLDurationFromTimeInterval(timeInterval); | ||
NSTimeInterval durationTimeInterval = MGLTimeIntervalFromDuration(duration); | ||
|
||
mbgl::Duration expectedDuration = 5s; | ||
|
||
XCTAssertEqual(timeInterval, durationTimeInterval); | ||
XCTAssertEqual(timeInterval, MGLTimeIntervalFromDuration(expectedDuration)); | ||
|
||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#import <XCTest/XCTest.h> | ||
|
||
#include <mbgl/util/chrono.hpp> | ||
#import "../../darwin/src/NSDate+MGLAdditions.h" | ||
|
||
using namespace std::chrono_literals; | ||
|
||
@interface MGLNSDateAdditionsTests : XCTestCase | ||
@end | ||
|
||
@implementation MGLNSDateAdditionsTests | ||
|
||
- (void)testDurationToNSTimeInterval { | ||
|
||
NSTimeInterval timeInterval = 5; | ||
mbgl::Duration duration = MGLDurationFromTimeInterval(timeInterval); | ||
NSTimeInterval durationTimeInterval = MGLTimeIntervalFromDuration(duration); | ||
|
||
mbgl::Duration expectedDuration = 5s; | ||
|
||
XCTAssertEqual(timeInterval, durationTimeInterval); | ||
XCTAssertEqual(timeInterval, MGLTimeIntervalFromDuration(expectedDuration)); | ||
|
||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.