OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/value_conversions.h" | 5 #include "base/value_conversions.h" |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/string_number_conversions.h" |
| 9 #include "base/time.h" |
8 #include "base/values.h" | 10 #include "base/values.h" |
9 | 11 |
10 namespace base { | 12 namespace base { |
11 | 13 |
12 // |Value| internally stores strings in UTF-8, so we have to convert from the | 14 // |Value| internally stores strings in UTF-8, so we have to convert from the |
13 // system native code to UTF-8 and back. | 15 // system native code to UTF-8 and back. |
14 StringValue* CreateFilePathValue(const FilePath& in_value) { | 16 StringValue* CreateFilePathValue(const FilePath& in_value) { |
15 return new StringValue(in_value.AsUTF8Unsafe()); | 17 return new StringValue(in_value.AsUTF8Unsafe()); |
16 } | 18 } |
17 | 19 |
18 bool GetValueAsFilePath(const Value& value, FilePath* file_path) { | 20 bool GetValueAsFilePath(const Value& value, FilePath* file_path) { |
19 std::string str; | 21 std::string str; |
20 if (!value.GetAsString(&str)) | 22 if (!value.GetAsString(&str)) |
21 return false; | 23 return false; |
22 if (file_path) | 24 if (file_path) |
23 *file_path = FilePath::FromUTF8Unsafe(str); | 25 *file_path = FilePath::FromUTF8Unsafe(str); |
24 return true; | 26 return true; |
25 } | 27 } |
26 | 28 |
| 29 // |Value| does not support 64-bit integers, and doubles do not have enough |
| 30 // precision, so we store the 64-bit time value as a string instead. |
| 31 StringValue* CreateTimeValue(const Time& time) { |
| 32 std::string string_value = base::Int64ToString(time.ToInternalValue()); |
| 33 return new StringValue(string_value); |
| 34 } |
| 35 |
| 36 bool GetValueAsTime(const Value& value, Time* time) { |
| 37 std::string str; |
| 38 int64 int_value; |
| 39 if (!value.GetAsString(&str) || !base::StringToInt64(str, &int_value)) |
| 40 return false; |
| 41 if (time) |
| 42 *time = base::Time::FromInternalValue(int_value); |
| 43 return true; |
| 44 } |
| 45 |
27 } // namespace base | 46 } // namespace base |
OLD | NEW |