/*
* Copyright (c) 2004-2005, The Hong Kong University of Science and Technology (HKUST)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*   * Redistributions of source code must retain the above copyright notice,
*     this list of conditions,the authors and the following disclaimer.
*   * Redistributions in binary form must reproduce the above copyright notice,
*     this list of conditions,the authors and the following disclaimer in
*     the documentation and/or other materials provided with the distribution.
*   * Neither the name of the university nor the names of its
*     contributors may be used to endorse or promote products derived from
*     this software without specific prior written permission.
*
* 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 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
/*
 * SDGEN: a data generator for Bisque
 * @Author: Bingsheng He (saven@cs.ust.hk)
 * @Date:28/3/3005
 * @Version: 1.0
 * @Function: utilities for SDGEN
 */

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

 public class Misc{
 	/*
 	 * ts1,ts2 are timestamp
 	 * we return ts2-ts1 in terms of number of epoch(in seconds);
 	 * Misc.convertIntoEpochs("2002-08-06 10:17:04","2002-08-07 08:33:26",30);
 	 */
 	public static int convertIntoEpochs(String ts1, String ts2, double epochLength)
 	{
 		int result=0;
 		//for ts1
 		Calendar cal1=Calendar.getInstance();
 		int year=0, month=0,day=0,hour=0,minute=0,second=0;
 		String[] tokens=ts1.split(" ");
 		String[] firstTokens=tokens[0].split("-");
 		year=Integer.parseInt(firstTokens[0]);
 		month=Integer.parseInt(firstTokens[1]);
 		day=Integer.parseInt(firstTokens[2]);

 		String[] secTokens=tokens[1].split(":");
 		hour=Integer.parseInt(secTokens[0]);
 		minute=Integer.parseInt(secTokens[1]);
 		second=Integer.parseInt(secTokens[2]);
 		cal1.set(year,month,day,hour,minute,second);
 		long sec1=cal1.getTimeInMillis();

 		//for ts2
 		Calendar cal2=Calendar.getInstance();
 	    tokens=ts2.split(" ");
 		firstTokens=tokens[0].split("-");
 		year=Integer.parseInt(firstTokens[0]);
 		month=Integer.parseInt(firstTokens[1]);
 		day=Integer.parseInt(firstTokens[2]);

 		secTokens=tokens[1].split(":");
 		hour=Integer.parseInt(secTokens[0]);
 		minute=Integer.parseInt(secTokens[1]);
 		second=Integer.parseInt(secTokens[2]);
 		cal2.set(year,month,day,hour,minute,second);
 		long sec2=cal2.getTimeInMillis();
 		result=(int)(((sec2-sec1)/1000)/epochLength);
 		return result;
 	}
 	/*
 	 * ts2 are timestamp
 	 * convert ts2 using a long number
 	 * Misc.getSeconds("2002-08-06 10:17:04");
 	 */
 	public static long getSeconds(String ts2)
 	{
 		int year=0, month=0,day=0,hour=0,minute=0,second=0;
 		Calendar cal2=Calendar.getInstance();
 	    String[] tokens=ts2.split(" ");
 		String[] firstTokens=tokens[0].split("-");
 		year=Integer.parseInt(firstTokens[0]);
 		month=Integer.parseInt(firstTokens[1]);
 		day=Integer.parseInt(firstTokens[2]);

 		String[] secTokens=tokens[1].split(":");
 		hour=Integer.parseInt(secTokens[0]);
 		minute=Integer.parseInt(secTokens[1]);
 		second=Integer.parseInt(secTokens[2]);
 		cal2.set(year,month,day,hour,minute,second);
 		long sec2=cal2.getTimeInMillis();
 		return (long)( sec2/1000);
 	}

 	/*
 	 * ts1,ts2 are timestamp
 	 * we return whether ts2 is after ts1
 	 * Misc.convertIntoEpochs("2002-08-06 10:17:04","2002-08-07 08:33:26",30);
 	 */
 	public static boolean isBefore(String ts1, String ts2)
 	{
 		int result=0;
 		//for ts1
 		Calendar cal1=Calendar.getInstance();
 		int year=0, month=0,day=0,hour=0,minute=0,second=0;
 		String[] tokens=ts1.split(" ");
 		String[] firstTokens=tokens[0].split("-");
 		year=Integer.parseInt(firstTokens[0]);
 		month=Integer.parseInt(firstTokens[1]);
 		day=Integer.parseInt(firstTokens[2]);

 		String[] secTokens=tokens[1].split(":");
 		hour=Integer.parseInt(secTokens[0]);
 		minute=Integer.parseInt(secTokens[1]);
 		second=Integer.parseInt(secTokens[2]);
 		cal1.set(year,month,day,hour,minute,second);
 		long sec1=cal1.getTimeInMillis();

 		//for ts2
 		Calendar cal2=Calendar.getInstance();
 	    tokens=ts2.split(" ");
 		firstTokens=tokens[0].split("-");
 		year=Integer.parseInt(firstTokens[0]);
 		month=Integer.parseInt(firstTokens[1]);
 		day=Integer.parseInt(firstTokens[2]);

 		secTokens=tokens[1].split(":");
 		hour=Integer.parseInt(secTokens[0]);
 		minute=Integer.parseInt(secTokens[1]);
 		second=Integer.parseInt(secTokens[2]);
 		cal2.set(year,month,day,hour,minute,second);
 		long sec2=cal2.getTimeInMillis();
 		return (sec2>sec1);
 	}

 	public static boolean isNameCh(char ch)
 	{
 		boolean result=false;
 		if(ch>='A' && ch <='Z')
 		result=true;
 		if(ch>='a' && ch <='z')
 		result=true;
 		if(ch>='0' && ch <='9')
 		result=true;
 		if(ch=='_' || ch=='-'|| ch=='.')
 		result=true;
 		return result;
 	}

 	public static void main(String [] argv)
 	{
 		boolean result=Misc.isBefore("2002-08-06 10:17:04","2002-08-06 10:17:34");
 		System.out.println(Misc.getSeconds("2002-08-06 10:17:05"));
 	}
 }