Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 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 | package org.onosproject.drivers.microsemi.yang.utils; |
| 17 | |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame^] | 18 | import java.time.Instant; |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 19 | import java.time.LocalDateTime; |
| 20 | import java.time.OffsetDateTime; |
| 21 | import java.time.ZoneId; |
| 22 | import java.time.ZonedDateTime; |
| 23 | import java.time.format.DateTimeFormatter; |
| 24 | |
Sean Condon | 06613e9 | 2017-06-09 15:14:01 +0100 | [diff] [blame] | 25 | import org.onosproject.yang.gen.v1.ietfyangtypes.rev20130715.ietfyangtypes.DateAndTime; |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * A utility class to change various YANG types to general purpose classes. |
| 29 | */ |
| 30 | public 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 Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame^] | 65 | |
| 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 Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 74 | } |