public class TimeParser extends Object
This class parses human-readable time specifications in the format described in detail on the rrdfetch man page. It is primarily a Java port of Tobi Oetiker's parsetime.c code, adapted for Java conventions.
Supported time specification formats include:
- Absolute times: "Jan 1 2023", "12:30", "2023/01/01 12:30:45"
- Relative times: "now-2hours", "tomorrow", "noon yesterday"
- Natural language: "midnight", "teatime", "start of week"
- Time ranges: "start+1week", "end-2days"
Special parsing rules:
- Two-digit years before 38 are interpreted as post-2000
- Ambiguous "m" token resolved based on context (months vs minutes)
- Supports both 12-hour (AM/PM) and 24-hour time formats
- Multiple date separators accepted: "/", ".", "-"
The parser uses a two-phase approach:
1. Lexical scanning (TimeScanner) breaks input into tokens
2. Grammar parsing interprets tokens according to at-style syntax rules
Example usage:
TimeParser parser = new TimeParser("now-1day+2hours");
TimeSpec spec = parser.parse();
long timestamp = spec.getTimestamp();
| Modifier and Type | Field and Description |
|---|---|
(package private) int |
op |
(package private) int |
prev_multiplier |
(package private) TimeScanner |
scanner |
(package private) TimeSpec |
spec |
(package private) TimeToken |
token |
| Constructor and Description |
|---|
TimeParser(String dateString)
Constructs TimeParser instance from the given input string.
|
| Modifier and Type | Method and Description |
|---|---|
TimeSpec |
parse()
Parses the input string specified in the constructor.
|
int op
int prev_multiplier
final TimeScanner scanner
final TimeSpec spec
TimeToken token
public TimeParser(String dateString)
dateString - at-style time specification (read rrdfetch man page for the complete
explanation)public TimeSpec parse()