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

Program:   Orbit Visualization Tool
Source:    $Source: /stor/devel/ovt2g/ovt/datatype/TimeFormat.java,v $
Date:      $Date: 2001/03/17 18:41:46 $
Version:   $Revision: 1.1 $


Copyright (c) 2000 OVT Team (Kristof Stasiewicz, Mykola Khotyaintsev,
Yuri Khotyaintsev)
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification is permitted provided that the following conditions are met:

 * No part of the software can be included in any commercial package without
written consent from the OVT team.

 * Redistributions of the source or binary code must retain the above
copyright notice, this list of conditions and the following disclaimer.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT OR
INDIRECT DAMAGES  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE.

OVT Team (http://ovt.irfu.se)   K. Stasiewicz, M. Khotyaintsev, Y.
Khotyaintsev

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

/*
 
yyyy ddd


hh.hhhh
hh:mm:ss
hh:mm

 *
 * Created on 9 Ноябрь 2000 г., 18:56
 */

package ovt.datatype;

import java.text.*;
import java.lang.*;

/**
 *
 * @author  ko
 * @version 
 */
public class TimeFormat {
    /** date format : <CODE>yyyy-mm-dd</CODE> */
    public final static int YYYY_MM_DD = 1; 
    /** date format : <CODE>yyyy/mm/dd</CODE> */
    public final static int YYYY_MM_DD_SLASH = 2; 
    /** date format : <CODE>yyyy ddd</CODE> */
    public final static int YYYY_DDD = 3;
    // hours format
    /** hours format : <CODE>hh:mm:ss</CODE> */
    public final static int HH_MM_SS = 8;
    /** hours format : <CODE>hh:mm</CODE> */
    public final static int HH_MM = 9; 
    /** hours format : <CODE>hh.hhhh</CODE> */
    public final static int HH_HHHH = 10;
    /** hours format : <CODE>sssss.ss</CODE> */
    public final static int SSSSS_SS = 11;
    
    /** Holds value of dateFormat. By default {@link YYYY_MM_DD}. */
    private int dateFormat = YYYY_MM_DD;
    /** Holds value of property hoursFormat. */
    private int hoursFormat = HH_MM_SS;
    
    private DecimalFormat DDD_format = new DecimalFormat("000");
    private DecimalFormat HH_HHHH_format = new DecimalFormat("00.####");
    private DecimalFormat SSSSS_SS_format = new DecimalFormat("00000.##");
    /** Creates new TimeFormat */
    public TimeFormat() {
    }
    
    public String format(ovt.datatype.Time time) {
        String date = "", hours = "";
        switch (dateFormat) {
            case YYYY_MM_DD : date = time.getAsText(Time.YEAR)+"-"+
                                 time.getAsText(Time.MONTH)+"-"+
                                 time.getAsText(Time.DAY);
                                 break;
            case YYYY_MM_DD_SLASH : date = time.getAsText(Time.YEAR)+"/"+
                                 time.getAsText(Time.MONTH)+"/"+
                                 time.getAsText(Time.DAY);
                                 break;
            // this is not already implemented
            case YYYY_DDD : date = time.getAsText(Time.YEAR) + " " +
                                    DDD_format.format(time.getDayOfTheYear());
                                 break;
        }
        switch (hoursFormat) {
            case SSSSS_SS : double s = (time.getHour()*60 + time.getMinutes())*60 + time.getSeconds() + time.getMsec()/60.;
                            hours = SSSSS_SS_format.format(s);
                            break;
            case HH_HHHH : double h = time.getHour() + (time.getMinutes() + (time.getSeconds() + time.getMsec()/60.)/60.)/60.;
                            hours = HH_HHHH_format.format(h);
                            break;
            case HH_MM_SS: hours = time.getAsText(Time.HOUR)+":"+
                                   time.getAsText(Time.MINUTE)+":"+
                                   time.getAsText(Time.SECOND);
                                   break;
            case HH_MM : hours = time.getAsText(Time.HOUR)+":"+
                                   time.getAsText(Time.MINUTE);
                                    break;
        }
        return date + " " + hours;
    }
    
    public StringBuffer format(Object obj, StringBuffer buf, FieldPosition pos) {
        return new StringBuffer("a");
    }
    
    /** Getter for property dateFormat.
     * @return Value of property dateFormat.
 */
    public int getDateFormat() {
        return dateFormat;
    }
    
    public String getDateFormatString() {
        return getFormat(dateFormat);
    }
    /** Setter for property dateFormat.
     * @param dateFormat New value of property dateFormat.
 */
    public void setDateFormat(int dateFormat) throws IllegalArgumentException {
        if (dateFormat != YYYY_DDD  &&  dateFormat != YYYY_MM_DD  && dateFormat != YYYY_MM_DD_SLASH)
            throw new IllegalArgumentException("Invalid format type '"+ dateFormat +"'");
        this.dateFormat = dateFormat;
    }
    
    /** Getter for property hoursFormat.
     * @return Value of property hoursFormat.
 */
    public int getHoursFormat() {
        return hoursFormat;
    }
    
    public String getHoursFormatString() {
        return getFormat(hoursFormat);
    }
    
    /** Setter for property hoursFormat.
     * @param hoursFormat New value of property hoursFormat.
 */
    public void setHoursFormat(int hoursFormat) {
        if ( hoursFormat != HH_HHHH  &&  hoursFormat != HH_MM  
                && hoursFormat != HH_MM_SS && hoursFormat != SSSSS_SS )
            throw new IllegalArgumentException("Invalid format type '"+ hoursFormat +"'");
        
        this.hoursFormat = hoursFormat;
    }
    
    public static String getFormat(int type) {
        switch (type) {
            case YYYY_MM_DD :  return "yyyy-mm-dd";
            case YYYY_MM_DD_SLASH : return "yyyy/mm/dd";
            case YYYY_DDD :  return "yyyy ddd";
            case HH_MM_SS :  return "hh:mm:ss";
            case HH_MM    :  return "hh:mm";
            case HH_HHHH  :  return "hh.hhhh";
            case SSSSS_SS :  return "sssss.ss";
        }
        throw new IllegalArgumentException("Invalid format type '"+ type +"'");
    }
    
    public static int[] getDateFormats() {
        return new int[]{ YYYY_MM_DD, YYYY_MM_DD_SLASH, YYYY_DDD };
    }
    
    public static String[] getDateFormatNames() {
        int[] formats = getDateFormats();
        String[] res = new String[formats.length];
        for (int i=0; i<formats.length; i++)
            res[i] = getFormat(formats[i]);
        return res;
    }
    
    public static int[] getHoursFormats() {
        return new int[]{ HH_MM, HH_MM_SS, HH_HHHH, SSSSS_SS };
    }
    
    public static String[] getHoursFormatNames() {
        int[] formats = getHoursFormats();
        String[] res = new String[formats.length];
        for (int i=0; i<formats.length; i++)
            res[i] = getFormat(formats[i]);
        return res;
    }
    
    public static void main(String[] args) {
        Time time = new Time("2001-02-03 10:00:36");
        TimeFormat tf = new TimeFormat();
        tf.setHoursFormat(tf.HH_HHHH);
        System.out.println(tf.format(time));
    }
}

