1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
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 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 package software.amazon.awssdk.core.traits;
17
18 import software.amazon.awssdk.annotations.SdkProtectedApi;
19 import software.amazon.awssdk.utils.DateUtils;
20
21 /**
22 * Trait that indicates a different format should be used for marshalling/unmarshalling timestamps. If not present
23 * the protocol will determine the default format to use based on the location (i.e. for JSON protocol headers are ISO8601
24 * but timestamps in the payload are epoch seconds with millisecond decimal precision).
25 */
26 @SdkProtectedApi
27 public final class TimestampFormatTrait implements Trait {
28
29 private final Format format;
30
31 private TimestampFormatTrait(Format timestampFormat) {
32 this.format = timestampFormat;
33 }
34
35 /**
36 * @return Format to use.
37 */
38 public Format format() {
39 return format;
40 }
41
42 public static TimestampFormatTrait create(Format timestampFormat) {
43 return new TimestampFormatTrait(timestampFormat);
44 }
45
46 /**
47 * Enum of the timestamp formats we currently support.
48 */
49 public enum Format {
50
51 /**
52 * See {@link DateUtils#parseIso8601Date(String)}
53 */
54 ISO_8601,
55
56 /**
57 * See {@link DateUtils#parseRfc1123Date(String)}
58 */
59 RFC_822,
60
61 /**
62 * See {@link DateUtils#parseUnixTimestampInstant(String)}
63 */
64 UNIX_TIMESTAMP,
65
66 /**
67 * See {@link DateUtils#parseUnixTimestampMillisInstant(String)}. This is only used by the CBOR protocol currently.
68 */
69 UNIX_TIMESTAMP_MILLIS;
70
71 /**
72 * Creates a timestamp format enum from the string defined in the model.
73 *
74 * @param strFormat String format.
75 * @return Format enum.
76 */
77 public static Format fromString(String strFormat) {
78 switch (strFormat) {
79 case "iso8601":
80 return ISO_8601;
81 case "rfc822":
82 return RFC_822;
83 case "unixTimestamp":
84 return UNIX_TIMESTAMP;
85 // UNIX_TIMESTAMP_MILLIS does not have a defined string format so intentionally omitted here.
86 default:
87 throw new RuntimeException("Unknown timestamp format - " + strFormat);
88 }
89 }
90 }
91
92 }
93