/*
* Created on Feb 23, 2004
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package de.desy.tine.dataUtils;
import java.text.DateFormat;
import java.util.Date;
import java.util.Calendar;
import java.util.TimeZone;
import de.desy.tine.client.TLink;
import de.desy.tine.client.TLinkFactory;
import de.desy.tine.definitions.TAccess;
import de.desy.tine.definitions.TMode;
//import de.desy.tine.server.equipment.TEquipmentModuleFactory;
import de.desy.tine.server.equipment.TSyncCallback;
/**
* TDataTime offers some useful Data and Time Stamp utilities
*
* @author duval
*
*/
public final class TDataTime
{
private static Calendar calendar = Calendar.getInstance();
private static boolean gSynchronizationStarted = false;
private static double[] gSyncTimeStamp = new double[1];
private static long gDataTimeStampOffset = 0;
public static Object syncMutex = new Object();
/**
* @return the current TINE time synchronization offset
*/
public static long getDataTimeStampOffset()
{
return gDataTimeStampOffset;
}
/**
* Sets the TINE time synchronization offset
*
* @param offset is the offset to be applied to the TINE timestamp
*/
public static void setDataTimeStampOffset(long offset)
{
gDataTimeStampOffset = offset;
}
public static double getSyncTimeStamp() { return gSyncTimeStamp[0]; }
/**
* Starts the TINE time synchronization where a TINE Time Server
* is assumed to be running and delivering a reference time stamp per
* multicast.
*
* @note this synchronization applies a time offset to timestamps
* obtained via calls to getDataTimeStamp()
*
* @return 0 upon success otherwise a TINE error code
*/
public static int systemStartGlobalSynchronization()
{
if (gSynchronizationStarted) return 0;
TDataType sdt = new TDataType(gSyncTimeStamp);
TLink tl = new TLink("/SITE","SYSTIME",sdt,null,TAccess.CA_READ);
int id = tl.attach(TMode.CM_GLOBAL, new TSyncCallback(), 1000);
if (id < 0) return -id;
gSynchronizationStarted = true;
return 0;
}
public static void dumpTime()
{
long ts = getLongDataTimeStamp();
String str = toString(ts);
TLinkFactory.dbgPrint(str);
TLinkFactory.dbgPrint("utc : "+ts+" msec");
TLinkFactory.dbgPrint("current time offset : "+getDataTimeStampOffset()+ " msec");
}
/**
* Gets the current TINE data timestamp
*
* @return the current TINE data timestamp as a UTC double containing seconds
* since 1.1.1970 with a fractional part. obtained via applying the
* current TINE time synchronization offset to the system clock
*/
public static double getDataTimeStamp()
{
return getDataTimeStamp(getLongDataTimeStamp());
}
/**
* Gets the current TINE data timestamp
*
* @return the current TINE data timestamp as a UTC long containing milliseconds
* since 1.1.1970. obtained via applying the
* current TINE time synchronization offset to the system clock
*/
public static long getLongDataTimeStamp()
{
long ts = System.currentTimeMillis();
ts += getDataTimeStampOffset();
return ts;
}
/**
* Converts a double timestamp (seconds) to a long timestamp (milliseconds).
*
* @param timeStamp is the timestamp in seconds (with fraction part) to be
* converted.
*
* @return the current TINE data timestamp as a UTC long containing milliseconds
* since 1.1.1970.
*/
public static long getLongDataTimeStamp(double timeStamp)
{
return (long)(timeStamp * 1000);
}
/**
* Converts a long timestamp (milliseconds) to a double timestamp (seconds).
*
* @param timeStamp is the timestamp in milliseconds to be
* converted.
*
* @return the current TINE data timestamp as a UTC double containing seconds
* since 1.1.1970 with a fractional part.
*/
public static double getDataTimeStamp(long longtimestamp)
{
return ((double)longtimestamp)/1000;
}
public static double getDataTimeStamp(String strtimestamp)
{
// 13:03:08 17.4.2012 or Wed May 23 10:23:41 2012
if (strtimestamp == null || strtimestamp.length() == 0) return 0;
long t = 0;
int hh,mm,ss,mo,da,yr;
String str;
String[] parts;
try
{
strtimestamp = strtimestamp.trim();
if (strtimestamp.length() < 20)
{ // e.g. 13:03:08 17.4.2012
str = strtimestamp.substring(0, 8);
parts = str.split(":");
hh = Integer.parseInt(parts[0]);
mm = Integer.parseInt(parts[1]);
ss = Integer.parseInt(parts[2]);
str = strtimestamp.substring(9);
parts = str.split("\\.");
da = Integer.parseInt(parts[0]);
mo = Integer.parseInt(parts[1]) - 1;
yr = Integer.parseInt(parts[2]);
Calendar c = Calendar.getInstance();
c.set(yr, mo, da, hh, mm, ss);
t = c.getTimeInMillis();
}
else
{ // e.g. Wed May 23 10:23:41 2012
//TODO: the parse method (even with lenient) cannot parse
//the same string that Date.toString() generates !! (way to go, Java boys ...)
// fortunately we don't need this to work at the moment ...
Date d = DateFormat.getTimeInstance().parse(strtimestamp);
t = d.getTime();
}
}
catch (Exception any)
{ // most likely a parsing exception -> then t stays at 0
}
return ((double)t)/1000;
}
/**
* Converts the input timestamp to a string in the form
* dow mon dd hh:mm:ss.msec zzz yyyy
*
* @param lTimeStamp is the timestamp (in milliseconds) to be converted
*
* @return string representation of the given timestamp
*/
public static String toString(long lTimeStamp)
{
Date d = new Date(lTimeStamp);
calendar.setTimeInMillis(lTimeStamp);
int mm = calendar.get(Calendar.MONTH)+1;
int yy = calendar.get(Calendar.YEAR) % 100;
StringBuffer sb = new StringBuffer(64);
String ts = d.toString();
sb.append(ts.substring(8,10) + (mm < 10 ? ".0"+mm : "."+mm) + (yy < 10 ? ".0"+yy : "."+yy));
sb.append(ts.substring(10,19));
int msec = (int)(lTimeStamp%1000);
if (msec < 10) sb.append(".00" + msec);
else if (msec < 100) sb.append(".0" + msec);
else sb.append("." + msec);
sb.append(" " +
calendar.getTimeZone().getDisplayName(calendar.getTimeZone().inDaylightTime(d),TimeZone.SHORT));
return sb.toString();
}
/**
* Converts the input timestamp to a string in the form
* dow mon dd hh:mm:ss.msec zzz yyyy
*
* @param dTimeStamp is the timestamp (in seconds with fractional part) to be converted
*
* @return string representation of the given timestamp
*/
public static String toString(double dTimeStamp)
{
long lTimeStamp = (long)(dTimeStamp * 1000);
return toString(lTimeStamp);
}
public final String toString()
{
return toString(new Date().getTime());
}
}