001/*
002 * Copyright (c) 2023 Chris K Wensel <[email protected]>. All Rights Reserved.
003 *
004 * This Source Code Form is subject to the terms of the Mozilla Public
005 * License, v. 2.0. If a copy of the MPL was not distributed with this
006 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
007 */
008
009package clusterless.commons.temporal;
010
011import java.time.ZoneOffset;
012import java.time.format.DateTimeFormatter;
013import java.time.format.DateTimeFormatterBuilder;
014import java.time.temporal.TemporalUnit;
015
016import static clusterless.commons.temporal.IntervalField.*;
017import static java.time.temporal.ChronoField.*;
018
019/**
020 * IntervalDateTimeFormatter provided formatters for the {@link IntervalUnit} units.
021 *
022 * See {@link IntervalUnits#formatter(TemporalUnit)} for looking up an appropriate formatter.
023 */
024public class IntervalDateTimeFormatter {
025    public static final DateTimeFormatter FOURTH_FORMATTER;
026    public static final DateTimeFormatter SIXTH_FORMATTER;
027    public static final DateTimeFormatter TWELFTH_FORMATTER;
028
029    static {
030        FOURTH_FORMATTER = new DateTimeFormatterBuilder()
031                .parseStrict()
032                .appendValue(YEAR, 4)
033                .appendValue(MONTH_OF_YEAR, 2)
034                .appendValue(DAY_OF_MONTH, 2)
035                .appendLiteral(FOURTH_OF_DAY.getBaseUnit().getDuration().toString())
036                .appendValue(FOURTH_OF_DAY, 3)
037                .toFormatter()
038                .withZone(ZoneOffset.UTC);
039    }
040
041    static {
042        SIXTH_FORMATTER = new DateTimeFormatterBuilder()
043                .parseStrict()
044                .appendValue(YEAR, 4)
045                .appendValue(MONTH_OF_YEAR, 2)
046                .appendValue(DAY_OF_MONTH, 2)
047                .appendLiteral(SIXTH_OF_DAY.getBaseUnit().getDuration().toString())
048                .appendValue(SIXTH_OF_DAY, 3)
049                .toFormatter()
050                .withZone(ZoneOffset.UTC);
051    }
052
053    static {
054        TWELFTH_FORMATTER = new DateTimeFormatterBuilder()
055                .parseStrict()
056                .appendValue(YEAR, 4)
057                .appendValue(MONTH_OF_YEAR, 2)
058                .appendValue(DAY_OF_MONTH, 2)
059                .appendLiteral(TWELFTH_OF_DAY.getBaseUnit().getDuration().toString())
060                .appendValue(TWELFTH_OF_DAY, 3)
061                .toFormatter()
062                .withZone(ZoneOffset.UTC);
063    }
064}