-
Notifications
You must be signed in to change notification settings - Fork 23
/
TimeConfigKey.java
118 lines (109 loc) · 3.84 KB
/
TimeConfigKey.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package uk.ac.starlink.ttools.plot2.config;
import java.text.SimpleDateFormat;
import java.util.Date;
import uk.ac.starlink.ttools.func.Times;
import uk.ac.starlink.ttools.plot2.PlotUtil;
import uk.ac.starlink.ttools.plot2.geom.TimeFormat;
/**
* Config key for values in the time domain.
* The value returned is a time in the domain defined by
* {@link uk.ac.starlink.table.TimeMapper}, that is unix seconds.
*
* @author Mark Taylor
* @since 15 Aug 2013
*/
public class TimeConfigKey extends ConfigKey<Double> {
/** XML <p> element describing the text input format. */
public static final String FORMAT_XML =
PlotUtil.concatLines( new String[] {
"<p>The value may be set with a string that can be interpreted as",
"a decimal year",
"(e.g. \"<code>2007.521</code>\")",
"or an ISO-8601 string",
"(e.g. \"<code>2007-07-10T03:57:36</code>\",",
"\"<code>2007-07-10T03</code>\"",
"or \"<code>2007-07-10</code>\").",
"Note however that the numeric value of this configuration item",
"if accessed programmatically is seconds since 1 Jan 1970.",
"</p>",
} );
/**
* Constructs a key with no default value.
*
* @param meta metadata
*/
public TimeConfigKey( ConfigMeta meta ) {
this( meta, Double.NaN );
if ( meta.getStringUsage() == null ) {
meta.setStringUsage( "<year-or-iso8601>" );
}
}
/**
* Constructs a key with a given default value.
*
* @param meta metadata
* @param dfltUnixSeconds default value as seconds since Unix epoch
*/
public TimeConfigKey( ConfigMeta meta, double dfltUnixSeconds ) {
super( meta, Double.class, new Double( dfltUnixSeconds ) );
}
public String valueToString( Double value ) {
double stime = value == null ? Double.NaN : value.doubleValue();
return Double.isNaN( stime ) ? ""
: formatTime( stime );
}
public Double stringToValue( String txt ) throws ConfigException {
if ( txt == null || txt.trim().length() == 0 ) {
return new Double( Double.NaN );
}
txt = txt.trim();
double dval;
try {
dval = Double.parseDouble( txt );
}
catch ( NumberFormatException e ) {
dval = Double.NaN;
}
if ( ! Double.isNaN( dval ) ) {
return TimeFormat.decimalYearToUnixSeconds( dval );
}
double mjd;
try {
mjd = Times.isoToMjd( txt );
}
catch ( RuntimeException e ) {
mjd = Double.NaN;
}
if ( ! Double.isNaN( mjd ) ) {
return Times.mjdToUnixMillis( mjd ) * 0.001;
}
else {
String msg = "Can't parse \"" + txt
+ "\" as decimal year or ISO-8601 time";
throw new ConfigException( this, msg );
}
}
public Specifier<Double> createSpecifier() {
return new TextFieldSpecifier<Double>( this, new Double( Double.NaN ) );
}
/**
* Formats a time in unix seconds to a string.
*
* @param unixSec time in seconds since unix epoch
* @return formatted value (currently ISO-8601)
*/
private String formatTime( double unixSec ) {
String ftime = new SimpleDateFormat( "yyyy-MM-dd'T'hh:mm:ss" )
.format( new Date( (long) ( unixSec * 1000 ) ) );
while ( ftime.endsWith( ":00" ) ) {
ftime = ftime.substring( 0, ftime.length() - 3 );
}
if ( ftime.endsWith( "T" ) ) {
ftime = ftime.substring( 0, ftime.length() - 1 );
}
if ( ftime.endsWith( "-01-01" ) ) {
ftime = ftime.substring( 0, ftime.length() - 6 );
}
return ftime;
}
}