//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.
//
package com.cloud.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import java.time.format.DateTimeFormatter;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

import com.cloud.utils.DateUtil.IntervalType;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class DateUtilTest {
    // command line test tool
    public static void main(String[] args) {
        TimeZone localTimezone = Calendar.getInstance().getTimeZone();
        TimeZone gmtTimezone = TimeZone.getTimeZone("GMT");
        TimeZone estTimezone = TimeZone.getTimeZone("EST");

        Date time = new Date();
        System.out.println("local time :" + DateUtil.getDateDisplayString(localTimezone, time));
        System.out.println("GMT time   :" + DateUtil.getDateDisplayString(gmtTimezone, time));
        System.out.println("EST time   :" + DateUtil.getDateDisplayString(estTimezone, time));
        //Test next run time. Expects interval and schedule as arguments
        if (args.length == 2) {
            System.out.println("Next run time: " + DateUtil.getNextRunTime(IntervalType.getIntervalType(args[0]), args[1], "GMT", time).toString());
        }
    }

    @Test
    public void zonedTimeFormatLegacy() throws ParseException {
        Date time = new Date();
        DateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'Z");
        String str = dfDate.format(time);
        Date dtParsed = DateUtil.parseTZDateString(str);

        assertEquals(str, time.toString(), dtParsed.toString());
    }

    @Test
    public void zonedTimeFormat() throws ParseException {
        Date time = new Date();
        DateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        String str = dfDate.format(time);
        Date dtParsed = DateUtil.parseTZDateString(str);

        assertEquals(str, time.toString(), dtParsed.toString());
    }

    @Test
    public void zonedTimeFormatIsoOffsetDateTime() throws ParseException {
        Date time = new Date();
        String str = OffsetDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);

        Date dtParsed = DateUtil.parseTZDateString(str);

        assertEquals(str, time.toString(), dtParsed.toString());
    }

    @Test
    public void zonedTimeFormatIsoInstant() throws ParseException {
        Date time = new Date();
        String str = OffsetDateTime.now().format(DateTimeFormatter.ISO_INSTANT);

        Date dtParsed = DateUtil.parseTZDateString(str);

        assertEquals(str, time.toString(), dtParsed.toString());
    }

    @Test
    public void zonedTimeFormatIsoOffsetDateTimeMs() throws ParseException {
        Date time = new Date();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSX");
        String str = OffsetDateTime.now().format(formatter);

        Date dtParsed = DateUtil.parseTZDateString(str);

        assertEquals(str, time.toString(), dtParsed.toString());
    }

    @Test
    public void zonedTimeFormatIsoInstantMs() throws ParseException {
        Date time = new Date();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'");
        String str = OffsetDateTime.now(ZoneOffset.UTC).format(formatter);

        Date dtParsed = DateUtil.parseTZDateString(str);

        assertEquals(str, time.toString(), dtParsed.toString());
    }

    @Test
    public void zonedTimeFormatIsoNoColonZMs() throws ParseException {
        Date time = new Date();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ");
        String str = OffsetDateTime.now().format(formatter);

        Date dtParsed = DateUtil.parseTZDateString(str);

        assertEquals(str, time.toString(), dtParsed.toString());
    }
}

@@ -915,32 +915,83 @@
 mpleDateFormat;%0A
+import java.time.Instant;%0Aimport java.time.ZoneId;%0A
 import java.util
@@ -1001,16 +1001,16 @@
 lendar;%0A
-
 import j
@@ -1131,37 +1131,8 @@
 ime;
-%0Aimport java.time.ZoneOffset;
 %0A%0Aim
@@ -2913,32 +2913,72 @@
 arseException %7B%0A
+        Instant moment = Instant.now();%0A
         Date tim
@@ -2977,33 +2977,40 @@
 Date time = 
-new Date(
+Date.from(moment
 );%0A        S
@@ -3028,36 +3028,72 @@
  OffsetDateTime.
-now(
+ofInstant(moment, ZoneId.systemDefault()
 ).format(DateTim
@@ -3327,32 +3327,72 @@
 arseException %7B%0A
+        Instant moment = Instant.now();%0A
         Date tim
@@ -3391,33 +3391,40 @@
 Date time = 
-new Date(
+Date.from(moment
 );%0A        S
@@ -3442,36 +3442,72 @@
  OffsetDateTime.
-now(
+ofInstant(moment, ZoneId.systemDefault()
 ).format(DateTim
@@ -3741,32 +3741,72 @@
 arseException %7B%0A
+        Instant moment = Instant.now();%0A
         Date tim
@@ -3805,33 +3805,40 @@
 Date time = 
-new Date(
+Date.from(moment
 );%0A        D
@@ -3956,36 +3956,72 @@
  OffsetDateTime.
-now(
+ofInstant(moment, ZoneId.systemDefault()
 ).format(formatt
@@ -4228,32 +4228,72 @@
 arseException %7B%0A
+        Instant moment = Instant.now();%0A
         Date tim
@@ -4292,33 +4292,40 @@
 Date time = 
-new Date(
+Date.from(moment
 );%0A        D
@@ -4457,26 +4457,42 @@
 ime.
-now(ZoneOffset.
+ofInstant(moment, ZoneId.of(%22
 UTC
+%22)
 ).fo
@@ -4712,32 +4712,72 @@
 arseException %7B%0A
+        Instant moment = Instant.now();%0A
         Date tim
@@ -4776,33 +4776,40 @@
 Date time = 
-new Date(
+Date.from(moment
 );%0A        D
@@ -4935,20 +4935,56 @@
 ateTime.
-now(
+ofInstant(moment, ZoneId.systemDefault()
 ).format

make sure no time skew can interfere with comparison (#3003)