-
Notifications
You must be signed in to change notification settings - Fork 261
Support Timestamp and Duration types #434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bce5957
Support Timestamp and Duration types
npars 1f989c6
Add Timestamp and Duration Swift support
npars 0ddc579
Add Timestamp and Duration to docs
npars 0add4d0
Fix cargo format errors
npars a2a1565
Move time tests to fixtures directory
npars 8349031
Use canonical name for Swift Duration bindings
npars 940d931
Support pre-epoch timestamps
npars 833aab5
Bump fixture version
npars 9f2453e
Add Timestamp calculation documentation
npars File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -361,6 +361,17 @@ unsafe impl ViaFfi for String { | |
/// Support for passing timestamp values via the FFI. | ||
/// | ||
/// Timestamps values are currently always passed by serializing to a buffer. | ||
/// | ||
/// Timestamps are represented on the buffer by an i64 that indicates the | ||
/// direction and the magnitude in seconds of the offset from epoch, and a | ||
/// u32 that indicates the nanosecond portion of the offset magnitude. The | ||
/// nanosecond portion is expected to be between 0 and 999,999,999. | ||
/// | ||
/// To build an epoch offset the absolute value of the seconds portion of the | ||
/// offset should be combined with the nanosecond portion. This is because | ||
/// the sign of the seconds portion represents the direction of the offset | ||
/// overall. The sign of the seconds portion can then be used to determine | ||
/// if the total offset should be added to or subtracted from the unix epoch. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perfect, thanks so much for adding these! |
||
unsafe impl ViaFfi for SystemTime { | ||
type FfiType = RustBuffer; | ||
|
||
|
@@ -406,6 +417,11 @@ unsafe impl ViaFfi for SystemTime { | |
/// Support for passing duration values via the FFI. | ||
/// | ||
/// Duration values are currently always passed by serializing to a buffer. | ||
/// | ||
/// Durations are represented on the buffer by a u64 that indicates the | ||
/// magnitude in seconds, and a u32 that indicates the nanosecond portion | ||
/// of the magnitude. The nanosecond portion is expected to be between 0 | ||
/// and 999,999,999. | ||
unsafe impl ViaFfi for Duration { | ||
type FfiType = RustBuffer; | ||
|
||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -228,30 +228,33 @@ extension Bool: ViaFfi { | |
extension Date: ViaFfiUsingByteBuffer, ViaFfi { | ||
fileprivate static func read(from buf: Reader) throws -> Self { | ||
let seconds: Int64 = try buf.readInt() | ||
let microseconds: UInt32 = try buf.readInt() | ||
let nanoseconds: UInt32 = try buf.readInt() | ||
if seconds >= 0 { | ||
let delta = Double(seconds) + (Double(microseconds) / 1.0e9) | ||
let delta = Double(seconds) + (Double(nanoseconds) / 1.0e9) | ||
return Date.init(timeIntervalSince1970: delta) | ||
} else { | ||
let delta = Double(-seconds) + (Double(microseconds) / 1.0e9) | ||
return Date.init(timeIntervalSince1970: -delta) | ||
let delta = Double(seconds) - (Double(nanoseconds) / 1.0e9) | ||
return Date.init(timeIntervalSince1970: delta) | ||
} | ||
} | ||
|
||
fileprivate func write(into buf: Writer) { | ||
var delta = self.timeIntervalSince1970 | ||
var sign: Int64 = 1 | ||
if delta < 0 { | ||
// The nanoseconds portion of the epoch offset must always be | ||
// positive, to simplify the calculation we will use the absolute | ||
// value of the offset. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏻 |
||
sign = -1 | ||
delta = -delta | ||
} | ||
if delta.rounded(.down) > Double(Int64.max) { | ||
fatalError("Timestamp overflow, exceeds max bounds supported by Uniffi") | ||
} | ||
let seconds = Int64(delta) | ||
let microseconds = UInt32((delta - Double(seconds)) * 1.0e9) | ||
let nanoseconds = UInt32((delta - Double(seconds)) * 1.0e9) | ||
buf.writeInt(sign * seconds) | ||
npars marked this conversation as resolved.
Show resolved
Hide resolved
|
||
buf.writeInt(microseconds) | ||
buf.writeInt(nanoseconds) | ||
} | ||
} | ||
|
||
|
@@ -274,8 +277,8 @@ extension TimeInterval { | |
|
||
fileprivate static func readDuration(from buf: Reader) throws -> Self { | ||
let seconds: UInt64 = try buf.readInt() | ||
let microseconds: UInt32 = try buf.readInt() | ||
return Double(seconds) + (Double(microseconds) / 1.0e9) | ||
let nanoseconds: UInt32 = try buf.readInt() | ||
return Double(seconds) + (Double(nanoseconds) / 1.0e9) | ||
} | ||
|
||
fileprivate func writeDuration(into buf: Writer) { | ||
|
@@ -288,9 +291,9 @@ extension TimeInterval { | |
} | ||
|
||
let seconds = UInt64(self) | ||
let microseconds = UInt32((self - Double(seconds)) * 1.0e9) | ||
let nanoseconds = UInt32((self - Double(seconds)) * 1.0e9) | ||
buf.writeInt(seconds) | ||
buf.writeInt(microseconds) | ||
buf.writeInt(nanoseconds) | ||
} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.