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.
5 #include "ppapi/shared_impl/time_conversion.h"
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.
14 // This should only be used for WebKit times which we can't change the
16 double 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
;
23 return time_to_ticks_delta_seconds
;
28 PP_Time
TimeToPPTime(base::Time t
) {
32 base::Time
PPTimeToTime(PP_Time t
) {
33 // The time code handles exact "0" values as special, and produces
34 // a "null" Time object. But calling code would expect t==0 to represent the
35 // epoch (according to the description of PP_Time). Hence we just return the
36 // epoch in this case.
38 return base::Time::UnixEpoch();
39 return base::Time::FromDoubleT(t
);
42 PP_TimeTicks
TimeTicksToPPTimeTicks(base::TimeTicks t
) {
43 return static_cast<double>(t
.ToInternalValue()) /
44 base::Time::kMicrosecondsPerSecond
;
47 PP_TimeTicks
EventTimeToPPTimeTicks(double event_time
) {
48 return event_time
+ GetTimeToTimeTicksDeltaInSeconds();
51 double PPTimeTicksToEventTime(PP_TimeTicks t
) {
52 return t
- GetTimeToTimeTicksDeltaInSeconds();
55 double PPGetLocalTimeZoneOffset(const base::Time
& time
) {
56 // Explode it to local time and then unexplode it as if it were UTC. Also
57 // explode it to UTC and unexplode it (this avoids mismatching rounding or
58 // lack thereof). The time zone offset is their difference.
59 base::Time::Exploded exploded
= { 0 };
60 base::Time::Exploded utc_exploded
= { 0 };
61 time
.LocalExplode(&exploded
);
62 time
.UTCExplode(&utc_exploded
);
63 if (exploded
.HasValidValues() && utc_exploded
.HasValidValues()) {
64 base::Time adj_time
= base::Time::FromUTCExploded(exploded
);
65 base::Time cur
= base::Time::FromUTCExploded(utc_exploded
);
66 return (adj_time
- cur
).InSecondsF();