time_conversion.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ppapi/shared_impl/time_conversion.h"
6
7namespace ppapi {
8
9namespace {
10
11// Since WebKit doesn't use ticks for event times, we have to compute what
12// the time ticks would be assuming the wall clock time doesn't change.
13//
14// This should only be used for WebKit times which we can't change the
15// definition of.
16double GetTimeToTimeTicksDeltaInSeconds() {
17  static double time_to_ticks_delta_seconds = 0.0;
18  if (time_to_ticks_delta_seconds == 0.0) {
19    double wall_clock = TimeToPPTime(base::Time::Now());
20    double ticks = TimeTicksToPPTimeTicks(base::TimeTicks::Now());
21    time_to_ticks_delta_seconds = ticks - wall_clock;
22  }
23  return time_to_ticks_delta_seconds;
24}
25
26}  // namespace
27
28PP_Time TimeToPPTime(base::Time t) {
29  return t.ToDoubleT();
30}
31
32base::Time PPTimeToTime(PP_Time t) {
33  return base::Time::FromDoubleT(t);
34}
35
36PP_TimeTicks TimeTicksToPPTimeTicks(base::TimeTicks t) {
37  return static_cast<double>(t.ToInternalValue()) /
38      base::Time::kMicrosecondsPerSecond;
39}
40
41PP_TimeTicks EventTimeToPPTimeTicks(double event_time) {
42  return event_time + GetTimeToTimeTicksDeltaInSeconds();
43}
44
45double PPTimeTicksToEventTime(PP_TimeTicks t) {
46  return t - GetTimeToTimeTicksDeltaInSeconds();
47}
48
49}  // namespace ppapi
50