blob: 7c67992fa8e452c6440b197a3b46fdab8283b7ea [file] [log] [blame]
Sean Condonfae8e662016-12-15 10:25:13 +00001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Sean Condonfae8e662016-12-15 10:25:13 +00003 *
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
Sean Condon0e89bda2017-03-21 14:23:19 +000018import java.time.Instant;
Sean Condonfae8e662016-12-15 10:25:13 +000019import java.time.LocalDateTime;
20import java.time.OffsetDateTime;
21import java.time.ZoneId;
22import java.time.ZonedDateTime;
23import java.time.format.DateTimeFormatter;
24
Sean Condon06613e92017-06-09 15:14:01 +010025import org.onosproject.yang.gen.v1.ietfyangtypes.rev20130715.ietfyangtypes.DateAndTime;
Sean Condonfae8e662016-12-15 10:25:13 +000026
27/**
28 * A utility class to change various YANG types to general purpose classes.
29 */
30public final class IetfYangTypesUtils {
31 private IetfYangTypesUtils() {
32 //Hiding the public constructor for this utility class
33 }
34
35 /**
36 * Convert from Date and Time in a ietf-yang-types format to the Java Time API.
37 * @param dateAndTime A date and time from a YANG object
38 * @return A Date and Time with a Time Zone offset
39 */
40 public static OffsetDateTime fromYangDateTime(DateAndTime dateAndTime) {
41 return OffsetDateTime.parse(dateAndTime.toString(), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
42 }
43
44 /**
45 * Convert a from Date and Time in a ietf-yang-types format to the Java Time API and rezone to a given Time Zone.
46 * @param dateAndTime A date and time from a YANG object
47 * @param zoneId The time zone to rezone the time and date to
48 * @return The rezoned time and date
49 */
50 public static ZonedDateTime fromYangDateTimeZoned(DateAndTime dateAndTime, ZoneId zoneId) {
51 return OffsetDateTime.parse(dateAndTime.toString(),
52 DateTimeFormatter.ISO_OFFSET_DATE_TIME).atZoneSameInstant(zoneId);
53 }
54
55 /**
56 * Convert a from Date and Time in a ietf-yang-types format to the Java Time API rezoned to the local Time Zone.
57 * @param dateAndTime A date and time from a YANG object
58 * @return The date and time in the zone of this local machine
59 */
60 public static LocalDateTime fromYangDateTimeToLocal(DateAndTime dateAndTime) {
61 OffsetDateTime odt = OffsetDateTime.parse(dateAndTime.toString(), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
62
63 return LocalDateTime.ofInstant(odt.toInstant(), ZoneId.systemDefault());
64 }
Sean Condon0e89bda2017-03-21 14:23:19 +000065
66 /**
67 * Convert a from Date and Time in a ietf-yang-types format to the Java Time API as an Instant.
68 * @param dateAndTime A date and time from a YANG object
69 * @return The date and time as an Instant
70 */
71 public static Instant fromYangDateTimeToInstant(DateAndTime dateAndTime) {
72 return Instant.from(OffsetDateTime.parse(dateAndTime.toString(), DateTimeFormatter.ISO_OFFSET_DATE_TIME));
73 }
Sean Condonfae8e662016-12-15 10:25:13 +000074}