001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.rolling.helper;
015
016import java.time.Instant;
017import java.time.ZoneId;
018import java.util.Date;
019import java.util.List;
020
021import ch.qos.logback.core.CoreConstants;
022import ch.qos.logback.core.pattern.DynamicConverter;
023import ch.qos.logback.core.util.CachingDateFormatter;
024import ch.qos.logback.core.util.DatePatternToRegexUtil;
025
026/**
027 * Returns a date formatted by SimpleDateFormatter.
028 * 
029 * @author Ceki Gülcü
030 */
031public class DateTokenConverter<E> extends DynamicConverter<E> implements MonoTypedConverter {
032
033    /**
034     * The conversion word/character with which this converter is registered.
035     */
036    public final static String CONVERTER_KEY = "d";
037    public final static String AUXILIARY_TOKEN = "AUX";
038    public static final String DEFAULT_DATE_PATTERN = CoreConstants.DAILY_DATE_PATTERN;
039
040    private String datePattern;
041    private ZoneId zoneId;
042    private CachingDateFormatter cdf;
043    // is this token converter primary or auxiliary? Only the primary converter
044    // determines the rolling period
045    private boolean primary = true;
046
047    public void start() {
048        this.datePattern = getFirstOption();
049        if (this.datePattern == null) {
050            this.datePattern = DEFAULT_DATE_PATTERN;
051        }
052
053        final List<String> optionList = getOptionList();
054        if (optionList != null) {
055            for (int optionIndex = 1; optionIndex < optionList.size(); optionIndex++) {
056                String option = optionList.get(optionIndex);
057                if (AUXILIARY_TOKEN.equalsIgnoreCase(option)) {
058                    primary = false;
059                } else {
060                    zoneId = ZoneId.of(option);
061                }
062            }
063        }
064
065        cdf = new CachingDateFormatter(datePattern, zoneId);
066    }
067
068    public String convert(Date date) {
069        return cdf.format(date.getTime());
070    }
071
072    public String convert(Instant instant) {
073        return cdf.format(instant.toEpochMilli());
074    }
075
076    public String convert(Object o) {
077        if (o == null) {
078            throw new IllegalArgumentException("Null argument forbidden");
079        }
080        if (o instanceof Date) {
081            return convert((Date) o);
082        }
083        if (o instanceof Instant) {
084            return convert((Instant) o);
085        }
086
087        throw new IllegalArgumentException("Cannot convert " + o + " of type" + o.getClass().getName());
088    }
089
090    /**
091     * Return the date pattern.
092     */
093    public String getDatePattern() {
094        return datePattern;
095    }
096
097    public ZoneId getZoneId() {
098        return zoneId;
099    }
100
101    public boolean isApplicable(Object o) {
102        if(o instanceof Date)
103            return true;
104        if(o instanceof Instant)
105            return true;
106        return false;
107    }
108
109    public String toRegex() {
110        DatePatternToRegexUtil datePatternToRegexUtil = new DatePatternToRegexUtil(datePattern);
111        return datePatternToRegexUtil.toRegex();
112    }
113
114    public boolean isPrimary() {
115        return primary;
116    }
117}