| 6 |
| 7 | #include "base/strings/string_number_conversions.h" |
| 8 |
| 9 | namespace util { |
| 10 |
| 11 | base::Value Int64ToValue(int64_t integer) { |
| 12 | return base::Value(base::NumberToString(integer)); |
| 13 | } |
| 14 |
| 15 | base::Optional<int64_t> ValueToInt64(const base::Value* value) { |
| 16 | return value ? ValueToInt64(*value) : base::nullopt; |
| 17 | } |
| 18 |
| 19 | base::Optional<int64_t> ValueToInt64(const base::Value& value) { |
| 20 | if (!value.is_string()) |
| 21 | return base::nullopt; |
| 22 |
| 23 | int64_t integer; |
| 24 | if (!base::StringToInt64(value.GetString(), &integer)) |
| 25 | return base::nullopt; |
| 26 |
| 27 | return integer; |
| 28 | } |
| 29 |
| 30 | base::Value TimeDeltaToValue(base::TimeDelta time_delta) { |
| 31 | return Int64ToValue(time_delta.InMicroseconds()); |
| 32 | } |
| 33 |
| 34 | base::Optional<base::TimeDelta> ValueToTimeDelta(const base::Value* value) { |
| 35 | return value ? ValueToTimeDelta(*value) : base::nullopt; |
| 36 | } |
| 37 |
| 38 | base::Optional<base::TimeDelta> ValueToTimeDelta(const base::Value& value) { |
| 39 | base::Optional<int64_t> integer = ValueToInt64(value); |
| 40 | if (!integer) |
| 41 | return base::nullopt; |
| 42 | return base::TimeDelta::FromMicroseconds(*integer); |
| 43 | } |
| 44 |
| 45 | base::Value TimeToValue(base::Time time) { |
| 46 | return TimeDeltaToValue(time.ToDeltaSinceWindowsEpoch()); |
| 47 | } |
| 48 |
| 49 | base::Optional<base::Time> ValueToTime(const base::Value* value) { |
| 50 | return value ? ValueToTime(*value) : base::nullopt; |
| 51 | } |
| 52 |
| 53 | base::Optional<base::Time> ValueToTime(const base::Value& value) { |
| 54 | base::Optional<base::TimeDelta> time_delta = ValueToTimeDelta(value); |
| 55 | if (!time_delta) |
| 56 | return base::nullopt; |
| 57 | return base::Time::FromDeltaSinceWindowsEpoch(*time_delta); |
| 58 | } |
| 59 |
| 60 | } // namespace util |
| 61 |