This class stores the result of parsing time specifications in the format used by RRDTool and described in detail on the rrdfetch man page. It supports both absolute timestamps and relative time adjustments.
Time specification types:
- Absolute time: Specific date and time (e.g., "Jan 1 2023 12:00")
- Relative time: Offsets from reference time (e.g., "now-2hours")
- Start/End markers: Relative to another time spec (e.g., "start+1day")
Common usage patterns:
// Absolute time examples
TimeParser p1 = new TimeParser("Jan 1 2023");
TimeParser p2 = new TimeParser("12:30");
TimeParser p3 = new TimeParser("2023/01/01 12:30:45");
// Relative time examples
TimeParser p4 = new TimeParser("now-1day"); // Yesterday
TimeParser p5 = new TimeParser("now+2hours30min"); // 2.5 hours from now
TimeParser p6 = new TimeParser("tomorrow"); // Tomorrow at midnight
TimeParser p7 = new TimeParser("noon"); // Today at 12:00
// Range examples
TimeParser p8 = new TimeParser("start"); // Start of range
TimeParser p9 = new TimeParser("end+1week"); // End of range + 1 week
See TimeParser for complete syntax reference and parsing details.
Field Summary
FieldsModifier and TypeFieldDescription(package private) TimeSpecAbsolute spec this one resolves against, wired up bygetTimes(TimeSpec, TimeSpec).(package private) final StringThe at-style text this spec was parsed from, echoed in resolution error messages.(package private) intBroken-down date and wall-clock time of the instant this spec resolves to, followingGregorianCalendarconventions:monthcounts from 0 for January, andsecholds whole seconds with no sub-second part.(package private) intOffset from the context's instant, in the same units as the components above:dmonthis a whole number of months, the rest whole years, days, hours, minutes and seconds.(package private) intOffset from the context's instant, in the same units as the components above:dmonthis a whole number of months, the rest whole years, days, hours, minutes and seconds.(package private) intOffset from the context's instant, in the same units as the components above:dmonthis a whole number of months, the rest whole years, days, hours, minutes and seconds.(package private) intOffset from the context's instant, in the same units as the components above:dmonthis a whole number of months, the rest whole years, days, hours, minutes and seconds.(package private) intOffset from the context's instant, in the same units as the components above:dmonthis a whole number of months, the rest whole years, days, hours, minutes and seconds.(package private) intOffset from the context's instant, in the same units as the components above:dmonthis a whole number of months, the rest whole years, days, hours, minutes and seconds.(package private) intBroken-down date and wall-clock time of the instant this spec resolves to, followingGregorianCalendarconventions:monthcounts from 0 for January, andsecholds whole seconds with no sub-second part.(package private) intBroken-down date and wall-clock time of the instant this spec resolves to, followingGregorianCalendarconventions:monthcounts from 0 for January, andsecholds whole seconds with no sub-second part.(package private) intBroken-down date and wall-clock time of the instant this spec resolves to, followingGregorianCalendarconventions:monthcounts from 0 for January, andsecholds whole seconds with no sub-second part.(package private) intBroken-down date and wall-clock time of the instant this spec resolves to, followingGregorianCalendarconventions:monthcounts from 0 for January, andsecholds whole seconds with no sub-second part.(package private) intWhich of the three forms above this spec takes; an absolute date/time unless a parser says otherwise.(package private) static final intTime specification represents an absolute date/time.(package private) static final intTime specification is relative to the end of the time range.(package private) static final intTime specification is relative to the start of the time range.(package private) intDay of week, 0 for Sunday through 6 for Saturday, used to resolve weekday names.(package private) intBroken-down date and wall-clock time of the instant this spec resolves to, followingGregorianCalendarconventions:monthcounts from 0 for January, andsecholds whole seconds with no sub-second part.Constructor Summary
ConstructorsConstructorDescriptionCreates an empty spec bound to the text it will be parsed from;TimeParserfills in the time components afterwards.Method Summary
Modifier and TypeMethodDescription(package private) Stringdump()Returns a debug string representation of this TimeSpec.(package private) GregorianCalendargetTime()Resolves this spec against its context, applying the relative offsets.static Calendar[]Use this static method to resolve relative time references and obtain the corresponding Calendar objects.longReturns the corresponding timestamp (seconds since Epoch).static long[]getTimestamps(TimeSpec spec1, TimeSpec spec2) Use this static method to resolve relative time references and obtain the corresponding timestamps (seconds since epoch).(package private) voidlocaltime(long timestamp) Initializes this TimeSpec with local time components from a timestamp.
Field Details
TYPE_ABSOLUTE
static final int TYPE_ABSOLUTETime specification represents an absolute date/time.- See Also:
TYPE_START
static final int TYPE_STARTTime specification is relative to the start of the time range.- See Also:
TYPE_END
static final int TYPE_ENDTime specification is relative to the end of the time range.- See Also:
type
int typeWhich of the three forms above this spec takes; an absolute date/time unless a parser says otherwise.year
int yearBroken-down date and wall-clock time of the instant this spec resolves to, followingGregorianCalendarconventions:monthcounts from 0 for January, andsecholds whole seconds with no sub-second part.month
int monthBroken-down date and wall-clock time of the instant this spec resolves to, followingGregorianCalendarconventions:monthcounts from 0 for January, andsecholds whole seconds with no sub-second part.day
int dayBroken-down date and wall-clock time of the instant this spec resolves to, followingGregorianCalendarconventions:monthcounts from 0 for January, andsecholds whole seconds with no sub-second part.hour
int hourBroken-down date and wall-clock time of the instant this spec resolves to, followingGregorianCalendarconventions:monthcounts from 0 for January, andsecholds whole seconds with no sub-second part.min
int minBroken-down date and wall-clock time of the instant this spec resolves to, followingGregorianCalendarconventions:monthcounts from 0 for January, andsecholds whole seconds with no sub-second part.sec
int secBroken-down date and wall-clock time of the instant this spec resolves to, followingGregorianCalendarconventions:monthcounts from 0 for January, andsecholds whole seconds with no sub-second part.wday
int wdayDay of week, 0 for Sunday through 6 for Saturday, used to resolve weekday names.dyear
int dyearOffset from the context's instant, in the same units as the components above:dmonthis a whole number of months, the rest whole years, days, hours, minutes and seconds.dmonth
int dmonthOffset from the context's instant, in the same units as the components above:dmonthis a whole number of months, the rest whole years, days, hours, minutes and seconds.dday
int ddayOffset from the context's instant, in the same units as the components above:dmonthis a whole number of months, the rest whole years, days, hours, minutes and seconds.dhour
int dhourOffset from the context's instant, in the same units as the components above:dmonthis a whole number of months, the rest whole years, days, hours, minutes and seconds.dmin
int dminOffset from the context's instant, in the same units as the components above:dmonthis a whole number of months, the rest whole years, days, hours, minutes and seconds.dsec
int dsecOffset from the context's instant, in the same units as the components above:dmonthis a whole number of months, the rest whole years, days, hours, minutes and seconds.dateString
The at-style text this spec was parsed from, echoed in resolution error messages.context
TimeSpec contextAbsolute spec this one resolves against, wired up bygetTimes(TimeSpec, TimeSpec).
Constructor Details
TimeSpec
TimeSpec(String dateString) Creates an empty spec bound to the text it will be parsed from;TimeParserfills in the time components afterwards.- Parameters:
dateString- at-style time specification, echoed in resolution error messages
Method Details
localtime
void localtime(long timestamp) Initializes this TimeSpec with local time components from a timestamp.This helper method converts a Unix timestamp (seconds since epoch) into individual time components (year, month, day, hour, minute, second) and day of week. This is used to establish a baseline time for relative time calculations.
- Parameters:
timestamp- Unix timestamp in seconds since epoch
getTime
GregorianCalendar getTime()Resolves this spec against its context, applying the relative offsets.- Returns:
- the resolved instant, with the offsets applied
- Throws:
IllegalStateException- if a relative spec has no absolute context to resolve against
getTimestamp
public long getTimestamp()Returns the corresponding timestamp (seconds since Epoch). Example:TimeParser p = new TimeParser("now-1day"); TimeSpec ts = p.parse(); System.out.println("Timestamp was: " + ts.getTimestamp();- Returns:
- Timestamp (in seconds, no milliseconds)
dump
String dump()Returns a debug string representation of this TimeSpec.This method formats the TimeSpec for debugging purposes, showing both the absolute time components and the relative delta components in a compact format.
- Returns:
- debug string showing type and all time components
getTimes
Use this static method to resolve relative time references and obtain the corresponding Calendar objects. Example:TimeParser pStart = new TimeParser("now-1month"); // starting time TimeParser pEnd = new TimeParser("start+1week"); // ending time TimeSpec specStart = pStart.parse(); TimeSpec specEnd = pEnd.parse(); GregorianCalendar[] gc = TimeSpec.getTimes(specStart, specEnd);- Parameters:
spec1- Starting time specificationspec2- Ending time specification- Returns:
- Two element array containing Calendar objects
getTimestamps
Use this static method to resolve relative time references and obtain the corresponding timestamps (seconds since epoch). Example:TimeParser pStart = new TimeParser("now-1month"); // starting time TimeParser pEnd = new TimeParser("start+1week"); // ending time TimeSpec specStart = pStart.parse(); TimeSpec specEnd = pEnd.parse(); long[] ts = TimeSpec.getTimestamps(specStart, specEnd);- Parameters:
spec1- Starting time specificationspec2- Ending time specification- Returns:
- array containing two timestamps (in seconds since epoch)