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

Commit 9daa62b

Browse files
committed
make conversion easier
1 parent a90928f commit 9daa62b

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
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
}

fml/time/time_point_unittest.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ TEST(TimePoint, DartClockIsMonotonic) {
2626
std::this_thread::sleep_for(1us);
2727
const auto t3 = DartTimelineTicksSinceEpoch();
2828
EXPECT_LT(TimePoint::Min(), t1);
29-
EXPECT_LT(t1, t2);
30-
EXPECT_LT(t2, t3);
29+
EXPECT_LE(t1, t2);
30+
EXPECT_LE(t2, t3);
3131
EXPECT_LT(t3, TimePoint::Max());
3232
}
3333

0 commit comments

Comments
 (0)