package com.vladmihalcea.datetime;

import org.joda.time.DateTime;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * DateTimeTest - Date and Time Test
 *
 * @author Vlad Mihalcea
 */
public class DateTimeTest {

    private Logger LOGGER = LoggerFactory.getLogger(DateTimeTest.class);

    // ISO 8601
    // http://www.w3.org/TR/NOTE-datetime
    @Test
    public void testSimpleDateFormatterWithUTCStrings() throws ParseException {
        dateFormatParse("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "1970-01-01T00:00:00.200Z", 200L);
        dateFormatParse("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "1970-01-01T00:00:00.200Z", 200L);
        dateFormatParse("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "1970-01-01T00:00:00.200+0000", 200L);
        dateFormatParse("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", "1970-01-01T00:00:00.200Z", 200L);
        dateFormatParse("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", "1970-01-01T00:00:00.200+0000", 200L);
        dateFormatParse("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "1970-01-01T00:00:00.200+0100", 200L - 1000 * 60 * 60);
    }

    @Test
    public void testJodaDateTimeWithUTCStrings() throws ParseException {
        jodaParse("1970-01-01T00:00:00.200Z", 200L);
        jodaParse("1970-01-01T00:00:00.200+0000", 200L);
        jodaParse("1970-01-01T00:00:00.200+0100", 200L - 1000 * 60 * 60);
    }

    private void dateFormatParse(String pattern, String stringValue, long expectedEpoch) throws ParseException {
        try {
            Date utcDate = new SimpleDateFormat(pattern).parse(stringValue);
            if(expectedEpoch != utcDate.getTime()) {
                LOGGER.warn("pattern: {}, date: {} actual epoch {} while expected epoch: {}", new Object[]{pattern, stringValue, utcDate.getTime(), expectedEpoch});
            }
        } catch (ParseException e) {
            LOGGER.warn("pattern: {}, date: {} threw {}", new Object[]{pattern, stringValue, e.getClass().getSimpleName()});
        }
    }

    private void jodaParse(String stringValue, long expectedEpoch) {
        Date utcDate = DateTime.parse(stringValue).toDate();
        if(expectedEpoch != utcDate.getTime()) {
            LOGGER.warn("date: {} actual epoch {} while expected epoch: {}", new Object[]{stringValue, utcDate.getTime(), expectedEpoch});
        }
    }
}

@@ -1220,32 +1220,36 @@
 n %7B%0A        joda
+Time
 Parse(%221970-01-0
@@ -1277,32 +1277,36 @@
 L);%0A        joda
+Time
 Parse(%221970-01-0
@@ -1342,24 +1342,28 @@
         joda
+Time
 Parse(%221970-
@@ -1411,32 +1411,241 @@
 0 * 60);%0A    %7D%0A%0A
+    /**%0A     * DateFormat parsing utility%0A     * @param pattern date/time pattern%0A     * @param dateTimeString date/time string value%0A     * @param expectedNumericTimestamp expected millis since epoch%0A     */%0A
     private void
@@ -1680,35 +1680,38 @@
 ern, String 
-stringValue
+dateTimeString
 , long expec
@@ -1717,36 +1717,25 @@
 cted
-Epoch) throws ParseException
+NumericTimestamp)
  %7B%0A 
@@ -1810,27 +1810,30 @@
 ).parse(
-stringValue
+dateTimeString
 );%0A     
@@ -1842,37 +1842,48 @@
      if(expected
-Epoch
+NumericTimestamp
  != utcDate.getT
@@ -1912,33 +1912,33 @@
    LOGGER.warn(%22
-p
+P
 attern: %7B%7D, date
@@ -2007,27 +2007,30 @@
 attern, 
-stringValue
+dateTimeString
 , utcDat
@@ -2042,37 +2042,48 @@
 Time(), expected
-Epoch
+NumericTimestamp
 %7D);%0A            
@@ -2146,17 +2146,17 @@
 R.warn(%22
-p
+P
 attern: 
@@ -2201,27 +2201,30 @@
 attern, 
-stringValue
+dateTimeString
 , e.getC
@@ -2262,24 +2262,192 @@
    %7D%0A    %7D%0A%0A
+    /**%0A     * Joda-Time parsing utility%0A     * @param dateTimeString date/time string value%0A     * @param expectedNumericTimestamp expected millis since epoch%0A     */%0A
     private 
@@ -2455,16 +2455,20 @@
 oid joda
+Time
 Parse(St
@@ -2472,27 +2472,30 @@
 (String 
-stringValue
+dateTimeString
 , long e
@@ -2501,21 +2501,32 @@
 expected
-Epoch
+NumericTimestamp
 ) %7B%0A    
@@ -2559,27 +2559,30 @@
 e.parse(
-stringValue
+dateTimeString
 ).toDate
@@ -2604,21 +2604,32 @@
 expected
-Epoch
+NumericTimestamp
  != utcD
@@ -2739,19 +2739,22 @@
 t%5B%5D%7B
-stringValue
+dateTimeString
 , ut
@@ -2782,13 +2782,24 @@
 cted
-Epoch
+NumericTimestamp
 %7D);%0A

Refactor method naming conevtions