xref: /aosp_15_r20/external/federated-compute/fcp/base/time_util.h (revision 14675a029014e728ec732f129a32e299b2da0601)
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef FCP_BASE_TIME_UTIL_H_
17 #define FCP_BASE_TIME_UTIL_H_
18 
19 #include "google/protobuf/duration.pb.h"
20 #include "google/protobuf/timestamp.pb.h"
21 #include "absl/time/time.h"
22 
23 namespace fcp {
24 
25 class TimeUtil {
26  public:
27   // Converts an absl::Time to a google::protobuf::Timestamp.
28   // Note that we assume the timestamps we deal with here are representable by
29   // both formats. If the resulted google::protobuf::Timestamp is invalid, it
30   // will lead to undefined behavior.
31   static google::protobuf::Timestamp ConvertAbslToProtoTimestamp(absl::Time t);
32 
33   // Converts a google::protobuf::Timestamp to an absl::Time.
34   // Note that we assume the timestamps we deal with here are representable by
35   // both formats. If the resulted absl::Time is invalid, it will lead to
36   // undefined behavior.
37   static absl::Time ConvertProtoToAbslTime(google::protobuf::Timestamp proto);
38 
39   // Converts an absl::Duration to a google::protobuf::Duration.
40   // Note that we assume the durations we deal with here are representable by
41   // both formats. If the resulted google::protobuf::Duration is invalid, it
42   // will lead to undefined behavior.
43   static google::protobuf::Duration ConvertAbslToProtoDuration(
44       absl::Duration absl_duration);
45 
46   // Converts a google::protobuf::Duration to an absl::Duration.
47   // Note that we assume the timestamps we deal with here are representable by
48   // both formats. If the resulted google::protobuf::Duration is invalid, it
49   // will lead to undefined behavior.
50   static absl::Duration ConvertProtoToAbslDuration(
51       google::protobuf::Duration proto);
52 };
53 
54 }  // namespace fcp
55 
56 #endif  // FCP_BASE_TIME_UTIL_H_
57