public class TimeParser
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
TimeParser.EnumFieldHandler
"$Y$m$d-$(enum;values=a,b,c,d)", "20130202-a", "2013-02-02/2013-02-03"
|
static interface |
TimeParser.FieldHandler
Interface to add custom handlers for strings with unique formats.
|
static class |
TimeParser.HrintervalFieldHandler
$(hrinterval;names=a,b,c,d) "b" → "06:00/12:00"
|
static class |
TimeParser.IgnoreFieldHandler
Just skip the field.
|
static class |
TimeParser.PeriodicFieldHandler
regular intervals are numbered.
|
static class |
TimeParser.SubsecFieldHandler
$(subsec;places=6) "36" → "36 microseconds"
|
Modifier and Type | Field and Description |
---|---|
static TimeParser.FieldHandler |
IGNORE_FIELD_HANDLER
handy FieldHandler that ignores the contents.
|
static java.lang.String |
TIMEFORMAT_Z
$Y-$m-$dT$H:$M:$S.$(subsec;places=3)Z
|
Modifier and Type | Method and Description |
---|---|
static TimeParser |
create(java.lang.String formatString)
Create a TimeParser object, which is the fast time parser for use when a known format specification is used to
parse many instances of a formatted string.
|
static TimeParser |
create(java.lang.String formatString,
java.lang.String fieldName,
TimeParser.FieldHandler handler,
java.lang.Object... moreHandler)
create the time parser, and add specialized handlers.
|
java.lang.String |
format(Datum start)
The TimeParser can be used to format times as well.
|
java.lang.String |
format(Datum start,
Datum stop)
The TimeParser can be used to format times as well.
|
java.lang.String |
format(Datum start,
Datum stop,
java.util.Map<java.lang.String,java.lang.String> extra)
The TimeParser can be used to format times as well.
|
java.lang.String |
format(DatumRange range)
return the formatted range.
|
double |
getEndTime(Units units)
return the end time of the range in units, e.g.
|
TimeParser.FieldHandler |
getFieldHandlerByCode(java.lang.String code)
return the field handler for the id.
|
TimeParser.FieldHandler |
getFieldHandlerById(java.lang.String id)
return the field handler for the id.
|
static char |
getPad(java.util.Map<java.lang.String,java.lang.String> args)
return the pad for the spec, like "underscore" "space" "zero" or "none"
For "none", space is returned, and clients allowing special behavior should check for this.
|
java.lang.String |
getRegex()
peek at the regular expression used.
|
double |
getTime(Units units)
return the parsed time in the given units.
|
Datum |
getTimeDatum()
return the parsed time as a Datum.
|
DatumRange |
getTimeRange()
Returns the implicit interval as a DatumRange.
|
DatumRange |
getValidRange()
return the limits of the range we can parse.
|
boolean |
hasField(java.lang.String field)
return true if the parser has a field.
|
static boolean |
isIso8601String(java.lang.String exampleTime)
return true if the string appears to be an ISO8601 time.
|
boolean |
isNested()
return true if each successive field is nested within the previous,
e.g.
|
static java.lang.String |
iso8601String(java.lang.String exampleTime)
must contain T or space to delimit date and time.
|
static boolean |
isSpec(java.lang.String spec)
Provide standard means of indicating this appears to be a spec by
looking for something that would assert the year.
|
boolean |
isStartTimeOnly()
true if the flag (startTimeOnly) was set in the spec.
|
static void |
main(java.lang.String[] aa) |
TimeParser |
parse(java.lang.String timeString)
parse the string, which presumably contains a time matching the
spec.
|
TimeParser |
parse(java.lang.String timeString,
java.util.Map<java.lang.String,java.lang.String> extra)
attempt to parse the string.
|
void |
setContext(DatumRange tr)
explicitly set the context for time parsing.
|
TimeParser |
setDigit(int digitNumber,
int digit)
This allows for string split into elements to be interpreted here.
|
void |
setDigit(java.lang.String format,
double value)
set the digit with the integer part, and move the fractional part to the
less significant digits.
|
TimeParser |
setDigit(java.lang.String format,
int value)
Set the digit using the format code.
|
void |
sloppyColumns()
force the parser to look for delimiters.
|
static void |
testTimeParser()
test time parsing when the format is known.
|
java.lang.String |
toString() |
public static final java.lang.String TIMEFORMAT_Z
public static final TimeParser.FieldHandler IGNORE_FIELD_HANDLER
tp= TimeParser.create(sagg,"v", TimeParser.IGNORE_FIELD_HANDLER );
public boolean hasField(java.lang.String field)
field
- e.g. "x"public static boolean isIso8601String(java.lang.String exampleTime)
exampleTime
- string like "1992-353T02:00"public static java.lang.String iso8601String(java.lang.String exampleTime)
exampleTime
- "1992-353T02:00"java.lang.IllegalArgumentException
- if the time does not appear to be ISO8601.public boolean isNested()
public boolean isStartTimeOnly()
public static boolean isSpec(java.lang.String spec)
spec
- public static TimeParser create(java.lang.String formatString)
$[fieldLength]<1-char code> or
$[fieldLength](<code>)
$[fieldLength](<code>;qualifiers)
fieldLength=0 --> makes field length indeterminate, deliminator must follow.
$Y 4-digit year
$y 2-digit year
$j 3-digit day of year
$m 2-digit month
$b 3-char month name (jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec. Sorry, rest of world...)
$d 2-digit day
$H 2-digit hour
$M 2-digit minute
$S 2-digit second
$(milli) 3-digit milliseconds
$(ignore) skip this field
$x skip this field
$(enum) skip this field. If id is specified, then id can be retrieved.
$v skip this field
$(hrinterval;values=0,1,2,3) enumeration of part of day
$(subsec;places=6) fractional seconds (6->microseconds)
$(periodic;offset=0;start=2000-001;period=P1D)
Qualifiers:
span=<int>
delta=<int>
Y=2004 Also for Y,m,d,H,M,S
For example:
$(j;Y=2004) means the day-of-year, within the year 2004.
$(H;Y=2004;j=117) means the hour of day 2004-117
$(m;span=6) means the 6-month interval starting at the given month.
formatString
- the format string.public static TimeParser create(java.lang.String formatString, java.lang.String fieldName, TimeParser.FieldHandler handler, java.lang.Object... moreHandler)
formatString
- like $Y$m$dT$HfieldName
- name for the special field, like "o"handler
- handler for the special field, like OrbitFieldHandlermoreHandler
- additional name/handler pairs.public void sloppyColumns()
public TimeParser parse(java.lang.String timeString) throws java.text.ParseException
tp.parse("2014-01-06T02").getTime( Units.us2000 )
Since this the TimeParser has a state, it is not safe to use simultaneously
by multiple threads. Each thread should create its own parser.timeString
- string containing a timejava.text.ParseException
- if the string cannot be parsed.public TimeParser parse(java.lang.String timeString, java.util.Map<java.lang.String,java.lang.String> extra) throws java.text.ParseException
timeString
- extra
- map that is passed into field handlersjava.text.ParseException
public static char getPad(java.util.Map<java.lang.String,java.lang.String> args)
args
- public void setDigit(java.lang.String format, double value)
format
- like "Y"value
- like 2014public TimeParser setDigit(java.lang.String format, int value)
format
- spec like "%Y%m%d"value
- integer like 20080830.public TimeParser setDigit(int digitNumber, int digit)
TimeParser p= TimeParser.create("%Y %m %d");
p.setDigit(0,2007).setDigit(1,12).setDigit(2,5).getTime( Units.us2000 );
p.format(); // maybe in the future
digitNumber,
- the digit to set (starting with 0).digit,
- value to set the digit.java.lang.IllegalArgumentException
- if the digit has a custom field handlerjava.lang.IllegalArgumentException
- if the digit does not exist.public void setContext(DatumRange tr)
tr
- the rangepublic double getTime(Units units)
from org.virbo.dataset import SemanticOps
tp= TimeParser.create("$Y-$m-$dT$H")
u= SemanticOps.lookupTimeUnits("seconds since 2014-01-01T00:00")
print tp.parse("2014-01-06T02").getTime( u )
units
- as in Units.us2000public Datum getTimeDatum()
public DatumRange getValidRange()
public DatumRange getTimeRange()
This accesses time, timeWidth, orbitDatumRange, startTime.tr= tp.getTimeRange() // "Jan 2015" tr= tr.next() // "Feb 2015", not 31 days starting Feb 1
public double getEndTime(Units units)
units
- public java.lang.String getRegex()
public java.lang.String format(DatumRange range)
range
- public java.lang.String format(Datum start)
start
- public java.lang.String format(Datum start, Datum stop)
start
- beginning of the intervalstop
- null if not needed or implicit.public java.lang.String format(Datum start, Datum stop, java.util.Map<java.lang.String,java.lang.String> extra)
start
- beginning of the intervalstop
- null if not needed or implicit.extra
- null or a map of additional identifiers, see enum and x.public TimeParser.FieldHandler getFieldHandlerByCode(java.lang.String code)
code
- public TimeParser.FieldHandler getFieldHandlerById(java.lang.String id)
id
- the field handler idpublic java.lang.String toString()
toString
in class java.lang.Object
public static void main(java.lang.String[] aa) throws java.lang.Exception
java.lang.Exception
public static void testTimeParser() throws java.lang.Exception
java.lang.Exception