21 package org.matsim.core.utils.misc;
44 final static double UNDEFINED_TIME = Double.NEGATIVE_INFINITY;
48 public final static double MIDNIGHT = 24 * 3600.0;
61 timeElements =
new String[60];
62 for (
int i = 0; i < 10; i++) {
63 timeElements[i] =
"0" + i;
65 for (
int i = 10; i < 60; i++) {
66 timeElements[i] = Integer.toString(i);
77 defaultTimeFormat = format;
80 public static final String
writeTime(
final double seconds,
final String timeformat) {
81 return writeTime(seconds, timeformat,
':');
84 public static final String
writeTime(
final double seconds,
final char separator) {
85 return writeTime(seconds, defaultTimeFormat, separator);
88 public static final String
writeTime(
final double seconds) {
89 return writeTime(seconds, defaultTimeFormat,
':');
104 public static final String
writeTime(
final double seconds,
final String timeformat,
final char separator) {
105 if (TIMEFORMAT_SSSS.equals(timeformat)) {
106 return Long.toString((
long)seconds);
108 if (seconds == UNDEFINED_TIME) {
112 return "-" +
writeTime(Math.abs(seconds), timeformat, separator);
115 long h = (long)(s / 3600);
117 int m = (int)(s / 60);
120 StringBuilder str =
new StringBuilder(10);
122 if (h < timeElements.length) {
123 str.append(timeElements[(
int) h]);
125 str.append(Long.toString(h));
128 str.append(separator);
129 str.append(timeElements[m]);
131 if (TIMEFORMAT_HHMM.equals(timeformat)) {
132 return str.toString();
134 if (TIMEFORMAT_HHMMSS.equals(timeformat)) {
135 str.append(separator);
136 str.append(timeElements[(
int)s]);
137 return str.toString();
139 if ( TIMEFORMAT_HHMMSSDOTSS.equals(timeformat)) {
140 str.append(separator);
146 return str.toString();
149 throw new IllegalArgumentException(
"The time format (" + timeformat +
") is not known.");
163 public static final double parseTime(
final String time) {
185 if (time == null || time.length() == 0 || time.equals(
"undefined")) {
188 boolean isNegative = (time.charAt(0) ==
'-');
189 String[] strings = (isNegative
193 if (strings.length == 1) {
194 seconds = Math.abs(Double.parseDouble(strings[0]));
195 }
else if (strings.length == 2) {
196 long h = Long.parseLong(strings[0]);
197 int m = Integer.parseInt(strings[1]);
199 if ((m < 0) || (m > 59)) {
200 throw new IllegalArgumentException(
"minutes are out of range in " + time);
203 seconds = Math.abs(h) * 3600 + m * 60;
204 }
else if (strings.length == 3) {
205 long h = Long.parseLong(strings[0]);
206 int m = Integer.parseInt(strings[1]);
207 double s = Double.parseDouble(strings[2]);
209 if ((m < 0) || (m > 59)) {
210 throw new IllegalArgumentException(
"minutes are out of range in " + time);
212 if ((s < 0) || (s >= 60)) {
213 throw new IllegalArgumentException(
"seconds are out of range in " + time);
216 seconds = Math.abs(h) * 3600 + m * 60 + s;
218 throw new IllegalArgumentException(
"time format is not valid in " + time);
235 int m = hhmm - (h * 100);
236 double seconds = Math.abs(h) * 3600 + m * 60;
static final double MIDNIGHT
double orElse(double other)
static final String TIMEFORMAT_HHMMSS
static double convertHHMMInteger(int hhmm)
static final String TIMEFORMAT_SSSS
static String [] explode(final String str, final char delimiter, final int limit)
static final String writeTime(final double seconds, final char separator)
static final OptionalTime parseOptionalTime(final String time)
static final double parseTime(final String time)
static final void setDefaultTimeFormat(final String format)
static String defaultTimeFormat
static final String writeTime(final double seconds, final String timeformat, final char separator)
static final String writeTime(final double seconds, final String timeformat)
static OptionalTime defined(double seconds)
static final String [] timeElements
static final OptionalTime parseTime(final String time, final char separator)
static final String TIMEFORMAT_HHMM
static final String writeTime(final double seconds)
static final String TIMEFORMAT_HHMMSSDOTSS
static OptionalTime undefined()
static final String writeTime(final OptionalTime time)