/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.util;

import java.io.*;
import java.text.*;
import java.util.*;

import org.apache.log4j.Logger;

public final class DateFormatter
    {
    public static final int DEFAULT = 0;
	public static final int MEDIUM = 2;
    public static final int SHORT = 1;
    static DateFormat serverdateformat, shortserverdateformat, mediumserverdateformat;
	static TimeZone servertimezone;
	
	private static Logger log = Logger.getLogger(DateFormatter.class);

	static
		{
		//information from www.ast.cam.ac.uk
		//British Summer Time ought to be regularised by EEC
		//convention from 1998 on (at least) to 2001 as
		//starting from last Sunday in March ending last Sunday
		//in October.  Changes take place at 1am GMT.

	    log.info("Default Locale: "+ Locale.getDefault());
		shortserverdateformat=(DateFormat)new SimpleDateFormat( "dd/MM/yyyy" );
		mediumserverdateformat=DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT );
		serverdateformat=DateFormat.getDateTimeInstance( DateFormat.FULL, DateFormat.FULL );
		servertimezone = new SimpleTimeZone( 0, "GMT",   //based on existing GMT time zone but with daylight saving
					/*MARCH*/   2, /*LAST*/ -1, /*SUNDAY*/ 1, /*1amGMT*/ 60*60*1000,
					/*OCTOBER*/ 9, /*LAST*/ -1, /*SUNDAY*/ 1, /*1amGMT*/ 60*60*1000 );
		serverdateformat.setTimeZone( servertimezone );
		shortserverdateformat.setTimeZone( servertimezone );
		mediumserverdateformat.setTimeZone( servertimezone );

		}

	public static String formatDate( java.util.Date d, int detail ) 
		{
		String str;
		switch ( detail )
			{
			case SHORT:
				str = shortserverdateformat.format(d);
				break;
			case MEDIUM:
				str = mediumserverdateformat.format(d);
				break;
			default:
				str = serverdateformat.format(d);
				break;
			}
		return str;
		}
	
	/**
     * Returns current date in specified format.
     * @param format The date format to use.
     */
	public static String formatDate( String format )
	{
	    Date date = new Date();
	    SimpleDateFormat sdf = new SimpleDateFormat( format );
	    return sdf.format(date);
	}
       
    /**
     * Formats a durations displaying it in a human readable form.
     * @param milliseconds The number of milliseconds.
     * @return A readable string for the duration. Eg. 7 seconds.
     */
    public static String formatDuration(long milliseconds)
    {
        final long SIZES[] = {1L, 1000L, 60000L, 3600000L, 86400000L, 31536000000L };
        final String NAMES[] = { "millisecond", "second", "minute", "hour", "day", "year" };
        int index = 0;
        while ( index < SIZES.length && milliseconds/SIZES[index] > 0)
            index++;
        
        if (index > 0) index--;
        
        long num = milliseconds/SIZES[index];
        return num + " "+ NAMES[index]+((num==1)?"":"s");
    }

    }
    
