001package org.apache.maven.model.interpolation;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.text.SimpleDateFormat;
023import java.util.Date;
024import java.util.GregorianCalendar;
025import java.util.Properties;
026import java.util.TimeZone;
027
028public class MavenBuildTimestamp
029{
030    // ISO 8601-compliant timestamp for machine readability
031    public static final String DEFAULT_BUILD_TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
032
033    public static final String BUILD_TIMESTAMP_FORMAT_PROPERTY = "maven.build.timestamp.format";
034
035    public static final TimeZone DEFAULT_BUILD_TIME_ZONE = TimeZone.getTimeZone( "Etc/UTC" );
036
037    private String formattedTimestamp;
038
039    public MavenBuildTimestamp()
040    {
041        this( new Date() );
042    }
043
044    public MavenBuildTimestamp( Date time )
045    {
046        this( time, DEFAULT_BUILD_TIMESTAMP_FORMAT );
047    }
048
049    public MavenBuildTimestamp( Date time, Properties properties )
050    {
051        this( time, properties != null ? properties.getProperty( BUILD_TIMESTAMP_FORMAT_PROPERTY ) : null );
052    }
053
054    public MavenBuildTimestamp( Date time, String timestampFormat )
055    {
056        if ( timestampFormat == null )
057        {
058            timestampFormat = DEFAULT_BUILD_TIMESTAMP_FORMAT;
059        }
060        if ( time == null )
061        {
062            time = new Date();
063        }
064        SimpleDateFormat dateFormat = new SimpleDateFormat( timestampFormat );
065        dateFormat.setCalendar( new GregorianCalendar() );
066        dateFormat.setTimeZone( DEFAULT_BUILD_TIME_ZONE );
067        formattedTimestamp = dateFormat.format( time );
068    }
069
070    public String formattedTimestamp()
071    {
072        return formattedTimestamp;
073    }
074}