Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit f9aea8b

Browse files
committed
make conversion easier
1 parent a90928f commit f9aea8b

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

fml/time/dart_timestamp_provider.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@ DartTimestampProvider::DartTimestampProvider() = default;
1212

1313
DartTimestampProvider::~DartTimestampProvider() = default;
1414

15+
int64_t DartTimestampProvider::ConvertToNanos(int64_t ticks,
16+
int64_t frequency) {
17+
int64_t nano_seconds = (ticks / frequency) * kNanosPerSecond;
18+
int64_t leftover_ticks = ticks % frequency;
19+
int64_t leftover_nanos = (leftover_ticks * kNanosPerSecond) / frequency;
20+
return nano_seconds + leftover_nanos;
21+
}
22+
1523
fml::TimePoint DartTimestampProvider::Now() {
1624
const int64_t ticks = Dart_TimelineGetTicks();
1725
const int64_t frequency = Dart_TimelineGetTicksFrequency();
1826
// optimization for the most common case.
1927
if (frequency != kNanosPerSecond) {
20-
// convert from frequency base to nanos.
21-
int64_t seconds = ticks / frequency;
22-
int64_t leftover_ticks = ticks - (seconds * frequency);
23-
int64_t result = seconds * kNanosPerSecond;
24-
result += ((leftover_ticks * kNanosPerSecond) / frequency);
25-
return fml::TimePoint::FromTicks(result);
28+
return fml::TimePoint::FromTicks(ConvertToNanos(ticks, frequency));
2629
} else {
2730
return fml::TimePoint::FromTicks(ticks);
2831
}

fml/time/dart_timestamp_provider.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class DartTimestampProvider : TimestampProvider {
2929
private:
3030
static constexpr int64_t kNanosPerSecond = 1000000000;
3131

32+
int64_t ConvertToNanos(int64_t ticks, int64_t frequency);
33+
3234
DartTimestampProvider();
3335

3436
FML_DISALLOW_COPY_AND_ASSIGN(DartTimestampProvider);

fml/time/time_point.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class TimePoint {
3939
return TimePoint(ticks.ToNanoseconds());
4040
}
4141

42+
// Expects ticks in nanos.
4243
static constexpr TimePoint FromTicks(int64_t ticks) {
4344
return TimePoint(ticks);
4445
}

0 commit comments

Comments
 (0)