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