Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow_addons/text/parse_time_op.py: 91%
11 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
1# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Parse time ops."""
16import platform
18import tensorflow as tf
20from tensorflow_addons.utils.resource_loader import LazySO
22IS_WINDOWS = platform.system() == "Windows"
24_parse_time_so = LazySO("custom_ops/text/_parse_time_op.so")
26tf.no_gradient("Addons>ParseTime")
29def parse_time(time_string: str, time_format: str, output_unit: str) -> tf.Tensor:
30 """Parse an input string according to the provided format string into a
31 Unix time.
33 Parse an input string according to the provided format string into a Unix
34 time, the number of seconds / milliseconds / microseconds / nanoseconds
35 elapsed since January 1, 1970 UTC.
37 Uses strftime()-like formatting options, with the same extensions as
38 FormatTime(), but with the exceptions that %E#S is interpreted as %E*S, and
39 %E#f as %E*f. %Ez and %E*z also accept the same inputs.
41 %Y consumes as many numeric characters as it can, so the matching
42 data should always be terminated with a non-numeric. %E4Y always
43 consumes exactly four characters, including any sign.
45 Unspecified fields are taken from the default date and time of ...
47 "1970-01-01 00:00:00.0 +0000"
49 For example, parsing a string of "15:45" (%H:%M) will return an
50 Unix time that represents "1970-01-01 15:45:00.0 +0000".
52 Note that ParseTime only heeds the fields year, month, day, hour,
53 minute, (fractional) second, and UTC offset. Other fields, like
54 weekday (%a or %A), while parsed for syntactic validity, are
55 ignored in the conversion.
57 Date and time fields that are out-of-range will be treated as
58 errors rather than normalizing them like `absl::CivilSecond` does.
59 For example, it is an error to parse the date "Oct 32, 2013"
60 because 32 is out of range.
62 A leap second of ":60" is normalized to ":00" of the following
63 minute with fractional seconds discarded. The following table
64 shows how the given seconds and subseconds will be parsed:
66 "59.x" -> 59.x // exact
67 "60.x" -> 00.0 // normalized
68 "00.x" -> 00.x // exact
70 Args:
71 time_string: The input time string to be parsed.
72 time_format: The time format.
73 output_unit: The output unit of the parsed unix time. Can only be SECOND,
74 MILLISECOND, MICROSECOND, NANOSECOND.
76 Returns:
77 the number of seconds / milliseconds / microseconds / nanoseconds elapsed
78 since January 1, 1970 UTC.
80 Raises:
81 ValueError: If `output_unit` is not a valid value,
82 if parsing `time_string` according to `time_format` failed.
83 """
84 if IS_WINDOWS:
85 raise NotImplementedError("parse_time is not yet implemented on Windows.")
86 return _parse_time_so.ops.addons_parse_time(time_string, time_format, output_unit)