package com.photogallery.log;

import org.apache.commons.lang3.time.FastDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimeUtils {

    public static final String FULL_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";

    public static String formatDateTime(Date dateTime, String format) {
        if (dateTime == null) {
            return null;
        }
        return FastDateFormat.getInstance(format).format(dateTime);
    }

    public static Date getUtcDatetime() {
        final Calendar cal = Calendar.getInstance();
        final int zoneOffset = cal.get(Calendar.ZONE_OFFSET);
        final int dstOffset = cal.get(Calendar.DST_OFFSET);
        cal.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
        return cal.getTime();
    }

    public static String getLogTimestamp(){
        return formatDateTime(getUtcDatetime(), FULL_DATE_TIME_FORMAT);
    }

}
