package com.hkm.disqus.api.gson;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
/**
* Standard ISO8601 date formatter minus milliseconds and time zone spec since Disqus API does not
* send these values on GET.
*/
public class Iso8601DateFormat {
/**
* ID to represent the 'GMT' string
*/
private static final String GMT_ID = "GMT";
/**
* The GMT timezone
*/
private static final TimeZone TIMEZONE_GMT = TimeZone.getTimeZone(GMT_ID);
/**
* Format date into yyyy-MM-ddThh:mm:ss
*
* @param date the date to format
* @return the date formatted as yyyy-MM-ddThh:mm:ss
*/
public static String format(Date date) {
Calendar calendar = new GregorianCalendar(TIMEZONE_GMT, Locale.US);
calendar.setTime(date);
// estimate capacity of buffer as close as we can (yeah, that's pedantic ;)
int capacity = "yyyy-MM-ddThh:mm:ss".length();
StringBuilder formatted = new StringBuilder(capacity);
padInt(formatted, calendar.get(Calendar.YEAR), "yyyy".length());
formatted.append('-');
padInt(formatted, calendar.get(Calendar.MONTH) + 1, "MM".length());
formatted.append('-');
padInt(formatted, calendar.get(Calendar.DAY_OF_MONTH), "dd".length());
formatted.append('T');
padInt(formatted, calendar.get(Calendar.HOUR_OF_DAY), "hh".length());
formatted.append(':');
padInt(formatted, calendar.get(Calendar.MINUTE), "mm".length());
formatted.append(':');
padInt(formatted, calendar.get(Calendar.SECOND), "ss".length());
return formatted.toString();
}
/**
* Parse a date from ISO-8601 formatted string. It expects a format yyyy-MM-ddThh:mm:ss
*
* @param date ISO string to parse in the appropriate format.
* @return the parsed date
* @throws IllegalArgumentException if the date is not in the appropriate format
*/
public static Date parse(String date) {
try {
int offset = 0;
// extract year
int year = parseInt(date, offset, offset += 4);
checkOffset(date, offset, '-');
// extract month
int month = parseInt(date, offset += 1, offset += 2);
checkOffset(date, offset, '-');
// extract day
int day = parseInt(date, offset += 1, offset += 2);
checkOffset(date, offset, 'T');
// extract hours, minutes, seconds and milliseconds
int hour = parseInt(date, offset += 1, offset += 2);
checkOffset(date, offset, ':');
int minutes = parseInt(date, offset += 1, offset += 2);
checkOffset(date, offset, ':');
int seconds = parseInt(date, offset += 1, offset += 2);
Calendar calendar = new GregorianCalendar(TIMEZONE_GMT);
calendar.setLenient(false);
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
calendar.set(Calendar.SECOND, seconds);
return calendar.getTime();
} catch (IndexOutOfBoundsException e) {
throw new IllegalArgumentException("Failed to parse date " + date, e);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Failed to parse date " + date, e);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Failed to parse date " + date, e);
}
}
/**
* Check if the expected character exist at the given offset of the
*
* @param value the string to check at the specified offset
* @param offset the offset to look for the expected character
* @param expected the expected character
* @throws IndexOutOfBoundsException if the expected character is not found
*/
private static void checkOffset(String value, int offset, char expected) throws IndexOutOfBoundsException {
char found = value.charAt(offset);
if (found != expected) {
throw new IndexOutOfBoundsException("Expected '" + expected + "' character but found '" + found + "'");
}
}
/**
* Parse an integer located between 2 given offsets in a string
*
* @param value the string to parse
* @param beginIndex the start index for the integer in the string
* @param endIndex the end index for the integer in the string
* @return the int
* @throws NumberFormatException if the value is not a number
*/
private static int parseInt(String value, int beginIndex, int endIndex) throws NumberFormatException {
if (beginIndex < 0 || endIndex > value.length() || beginIndex > endIndex) {
throw new NumberFormatException(value);
}
// use same logic as in Integer.parseInt() but less generic we're not supporting negative values
int i = beginIndex;
int result = 0;
int digit;
if (i < endIndex) {
digit = Character.digit(value.charAt(i++), 10);
if (digit < 0) {
throw new NumberFormatException("Invalid number: " + value);
}
result = -digit;
}
while (i < endIndex) {
digit = Character.digit(value.charAt(i++), 10);
if (digit < 0) {
throw new NumberFormatException("Invalid number: " + value);
}
result *= 10;
result -= digit;
}
return -result;
}
/**
* Zero pad a number to a specified length
*
* @param buffer buffer to use for padding
* @param value the integer value to pad if necessary.
* @param length the length of the string we should zero pad
*/
private static void padInt(StringBuilder buffer, int value, int length) {
String strValue = Integer.toString(value);
for (int i = length - strValue.length(); i > 0; i--) {
buffer.append('0');
}
buffer.append(strValue);
}
}