| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/shared_impl/time_conversion.h" | 5 #include "ppapi/shared_impl/time_conversion.h" |
| 6 | 6 |
| 7 namespace ppapi { | 7 namespace ppapi { |
| 8 | 8 |
| 9 namespace { | 9 namespace { |
| 10 | 10 |
| 11 // Since WebKit doesn't use ticks for event times, we have to compute what | 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. | 12 // the time ticks would be assuming the wall clock time doesn't change. |
| 13 // | 13 // |
| 14 // This should only be used for WebKit times which we can't change the | 14 // This should only be used for WebKit times which we can't change the |
| 15 // definition of. | 15 // definition of. |
| 16 double GetTimeToTimeTicksDeltaInSeconds() { | 16 double GetTimeToTimeTicksDeltaInSeconds() { |
| 17 static double time_to_ticks_delta_seconds = 0.0; | 17 static double time_to_ticks_delta_seconds = 0.0; |
| 18 if (time_to_ticks_delta_seconds == 0.0) { | 18 if (time_to_ticks_delta_seconds == 0.0) { |
| 19 double wall_clock = TimeToPPTime(base::Time::Now()); | 19 double wall_clock = TimeToPPTime(base::Time::Now()); |
| 20 double ticks = TimeTicksToPPTimeTicks(base::TimeTicks::Now()); | 20 double ticks = TimeTicksToPPTimeTicks(base::TimeTicks::Now()); |
| 21 time_to_ticks_delta_seconds = ticks - wall_clock; | 21 time_to_ticks_delta_seconds = ticks - wall_clock; |
| 22 } | 22 } |
| 23 return time_to_ticks_delta_seconds; | 23 return time_to_ticks_delta_seconds; |
| 24 } | 24 } |
| 25 | 25 |
| 26 } // namespace | 26 } // namespace |
| 27 | 27 |
| 28 PP_Time TimeToPPTime(base::Time t) { | 28 PP_Time TimeToPPTime(base::Time t) { return t.ToDoubleT(); } |
| 29 return t.ToDoubleT(); | |
| 30 } | |
| 31 | 29 |
| 32 base::Time PPTimeToTime(PP_Time t) { | 30 base::Time PPTimeToTime(PP_Time t) { |
| 33 // The time code handles exact "0" values as special, and produces | 31 // 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 | 32 // 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 | 33 // epoch (according to the description of PP_Time). Hence we just return the |
| 36 // epoch in this case. | 34 // epoch in this case. |
| 37 if (t == 0.0) | 35 if (t == 0.0) |
| 38 return base::Time::UnixEpoch(); | 36 return base::Time::UnixEpoch(); |
| 39 return base::Time::FromDoubleT(t); | 37 return base::Time::FromDoubleT(t); |
| 40 } | 38 } |
| 41 | 39 |
| 42 PP_TimeTicks TimeTicksToPPTimeTicks(base::TimeTicks t) { | 40 PP_TimeTicks TimeTicksToPPTimeTicks(base::TimeTicks t) { |
| 43 return static_cast<double>(t.ToInternalValue()) / | 41 return static_cast<double>(t.ToInternalValue()) / |
| 44 base::Time::kMicrosecondsPerSecond; | 42 base::Time::kMicrosecondsPerSecond; |
| 45 } | 43 } |
| 46 | 44 |
| 47 PP_TimeTicks EventTimeToPPTimeTicks(double event_time) { | 45 PP_TimeTicks EventTimeToPPTimeTicks(double event_time) { |
| 48 return event_time + GetTimeToTimeTicksDeltaInSeconds(); | 46 return event_time + GetTimeToTimeTicksDeltaInSeconds(); |
| 49 } | 47 } |
| 50 | 48 |
| 51 double PPTimeTicksToEventTime(PP_TimeTicks t) { | 49 double PPTimeTicksToEventTime(PP_TimeTicks t) { |
| 52 return t - GetTimeToTimeTicksDeltaInSeconds(); | 50 return t - GetTimeToTimeTicksDeltaInSeconds(); |
| 53 } | 51 } |
| 54 | 52 |
| 55 double PPGetLocalTimeZoneOffset(const base::Time& time) { | 53 double PPGetLocalTimeZoneOffset(const base::Time& time) { |
| 56 // Explode it to local time and then unexplode it as if it were UTC. Also | 54 // 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 | 55 // explode it to UTC and unexplode it (this avoids mismatching rounding or |
| 58 // lack thereof). The time zone offset is their difference. | 56 // lack thereof). The time zone offset is their difference. |
| 59 base::Time::Exploded exploded = { 0 }; | 57 base::Time::Exploded exploded = {0}; |
| 60 base::Time::Exploded utc_exploded = { 0 }; | 58 base::Time::Exploded utc_exploded = {0}; |
| 61 time.LocalExplode(&exploded); | 59 time.LocalExplode(&exploded); |
| 62 time.UTCExplode(&utc_exploded); | 60 time.UTCExplode(&utc_exploded); |
| 63 if (exploded.HasValidValues() && utc_exploded.HasValidValues()) { | 61 if (exploded.HasValidValues() && utc_exploded.HasValidValues()) { |
| 64 base::Time adj_time = base::Time::FromUTCExploded(exploded); | 62 base::Time adj_time = base::Time::FromUTCExploded(exploded); |
| 65 base::Time cur = base::Time::FromUTCExploded(utc_exploded); | 63 base::Time cur = base::Time::FromUTCExploded(utc_exploded); |
| 66 return (adj_time - cur).InSecondsF(); | 64 return (adj_time - cur).InSecondsF(); |
| 67 } | 65 } |
| 68 return 0.0; | 66 return 0.0; |
| 69 } | 67 } |
| 70 | 68 |
| 71 } // namespace ppapi | 69 } // namespace ppapi |
| OLD | NEW |