1 // Copyright 2021 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_TIME_TIME_DELTA_FROM_STRING_H_ 6 #define BASE_TIME_TIME_DELTA_FROM_STRING_H_ 7 8 #include "base/base_export.h" 9 #include "base/strings/string_piece.h" 10 #include "third_party/abseil-cpp/absl/types/optional.h" 11 12 namespace base { 13 14 class TimeDelta; 15 16 // Helper function for TimeDelta. 17 // This is not part of TimeDelta to avoid dragging the includes above into 18 // base/time/time.h. 19 // 20 // Adapted from Go's doc at https://golang.org/pkg/time/#ParseDuration 21 // [ParseDuration] parses a duration string. A duration string is 22 // a possibly signed sequence of decimal numbers, each with optional 23 // fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". 24 // Valid time units are "ns", "us" "ms", "s", "m", "h", "d". 25 // 26 // Special values that are allowed without specifying units: 27 // "0", "+0", "-0" -> TimeDelta() 28 // "inf", "+inf" -> TimeDelta::Max() 29 // "-inf" -> TimeDelta::Min() 30 // Returns `absl::nullopt` when parsing fails. Numbers larger than 2^63-1 31 // will fail parsing. Overflowing `number * unit` will return +/-inf, as 32 // appropriate. 33 BASE_EXPORT absl::optional<TimeDelta> TimeDeltaFromString( 34 StringPiece duration_string); 35 36 } // namespace base 37 38 #endif // BASE_TIME_TIME_DELTA_FROM_STRING_H_ 39