blob: 275dc25c6f8a5a23ea375e924371c513588ed553 [file] [log] [blame]
Sean Condonfae8e662016-12-15 10:25:13 +00001/*
2 * Copyright 2017-present Open Networking Laboratory
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 */
16package org.onosproject.drivers.microsemi.yang.utils;
17
18import java.time.LocalDateTime;
19import java.time.OffsetDateTime;
20import java.time.ZoneId;
21import java.time.ZonedDateTime;
22import java.time.format.DateTimeFormatter;
23
Sean Condon06613e92017-06-09 15:14:01 +010024import org.onosproject.yang.gen.v1.ietfyangtypes.rev20130715.ietfyangtypes.DateAndTime;
Sean Condonfae8e662016-12-15 10:25:13 +000025
26/**
27 * A utility class to change various YANG types to general purpose classes.
28 */
29public final class IetfYangTypesUtils {
30 private IetfYangTypesUtils() {
31 //Hiding the public constructor for this utility class
32 }
33
34 /**
35 * Convert from Date and Time in a ietf-yang-types format to the Java Time API.
36 * @param dateAndTime A date and time from a YANG object
37 * @return A Date and Time with a Time Zone offset
38 */
39 public static OffsetDateTime fromYangDateTime(DateAndTime dateAndTime) {
40 return OffsetDateTime.parse(dateAndTime.toString(), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
41 }
42
43 /**
44 * Convert a from Date and Time in a ietf-yang-types format to the Java Time API and rezone to a given Time Zone.
45 * @param dateAndTime A date and time from a YANG object
46 * @param zoneId The time zone to rezone the time and date to
47 * @return The rezoned time and date
48 */
49 public static ZonedDateTime fromYangDateTimeZoned(DateAndTime dateAndTime, ZoneId zoneId) {
50 return OffsetDateTime.parse(dateAndTime.toString(),
51 DateTimeFormatter.ISO_OFFSET_DATE_TIME).atZoneSameInstant(zoneId);
52 }
53
54 /**
55 * Convert a from Date and Time in a ietf-yang-types format to the Java Time API rezoned to the local Time Zone.
56 * @param dateAndTime A date and time from a YANG object
57 * @return The date and time in the zone of this local machine
58 */
59 public static LocalDateTime fromYangDateTimeToLocal(DateAndTime dateAndTime) {
60 OffsetDateTime odt = OffsetDateTime.parse(dateAndTime.toString(), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
61
62 return LocalDateTime.ofInstant(odt.toInstant(), ZoneId.systemDefault());
63 }
64}