MATSIM
Time.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * Time.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.utils.misc;
22 
25 
26 
27 public class Time {
28  // yy there is now java.time, which integrates joda.time into the standard
29  // jdk. should we consider looking into this? kai, dec'17
30 
31 
32  private Time() {} // namespace only, do not instantiate
33 
43  // of the convention. kai, nov'17
44  final static double UNDEFINED_TIME = Double.NEGATIVE_INFINITY;
48  public final static double MIDNIGHT = 24 * 3600.0;
49 
50  public static final String TIMEFORMAT_HHMM = "HH:mm";
51  public static final String TIMEFORMAT_HHMMSS = "HH:mm:ss";
52  public static final String TIMEFORMAT_SSSS = "ssss";
53 
54  public static final String TIMEFORMAT_HHMMSSDOTSS = "HH:mm:ss.ss" ;
55 
56  private static String defaultTimeFormat = TIMEFORMAT_HHMMSS;
57 
58  private final static String[] timeElements;
59 
60  static {
61  timeElements = new String[60];
62  for (int i = 0; i < 10; i++) {
63  timeElements[i] = "0" + i;
64  }
65  for (int i = 10; i < 60; i++) {
66  timeElements[i] = Integer.toString(i);
67  }
68  }
69 
76  public static final void setDefaultTimeFormat(final String format) {
77  defaultTimeFormat = format;
78  }
79 
80  public static final String writeTime(final double seconds, final String timeformat) {
81  return writeTime(seconds, timeformat, ':');
82  }
83 
84  public static final String writeTime(final double seconds, final char separator) {
85  return writeTime(seconds, defaultTimeFormat, separator);
86  }
87 
88  public static final String writeTime(final double seconds) {
89  return writeTime(seconds, defaultTimeFormat, ':');
90  }
91 
92  public static final String writeTime(final OptionalTime time) {
93  return writeTime(time.orElse(UNDEFINED_TIME));
94  }
95 
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);
107  }
108  if (seconds == UNDEFINED_TIME) {
109  return "undefined";
110  }
111  if (seconds < 0) {
112  return "-" + writeTime(Math.abs(seconds), timeformat, separator);
113  }
114  double s = seconds;
115  long h = (long)(s / 3600);
116  s = s % 3600;
117  int m = (int)(s / 60);
118  s = s % 60;
119 
120  StringBuilder str = new StringBuilder(10);
121 
122  if (h < timeElements.length) {
123  str.append(timeElements[(int) h]);
124  } else {
125  str.append(Long.toString(h));
126  }
127 
128  str.append(separator);
129  str.append(timeElements[m]);
130 
131  if (TIMEFORMAT_HHMM.equals(timeformat)) {
132  return str.toString();
133  }
134  if (TIMEFORMAT_HHMMSS.equals(timeformat)) {
135  str.append(separator);
136  str.append(timeElements[(int)s]);
137  return str.toString();
138  }
139  if ( TIMEFORMAT_HHMMSSDOTSS.equals(timeformat)) {
140  str.append(separator);
141 
142  if ( s < 10. ) {
143  str.append("0") ;
144  }
145  str.append(s);
146  return str.toString();
147  }
148 
149  throw new IllegalArgumentException("The time format (" + timeformat + ") is not known.");
150  }
151 
163  public static final double parseTime(final String time) {
164  return parseTime(time, ':').seconds();
165  }
166 
167  public static final OptionalTime parseOptionalTime(final String time) {
168  return parseTime(time, ':');
169  }
170 
171 
184  public static final OptionalTime parseTime(final String time, final char separator) {
185  if (time == null || time.length() == 0 || time.equals("undefined")) {
186  return OptionalTime.undefined();
187  }
188  boolean isNegative = (time.charAt(0) == '-');
189  String[] strings = (isNegative
190  ? StringUtils.explode(time.substring(1), separator)
191  : StringUtils.explode(time, separator));
192  double seconds = 0;
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]);
198 
199  if ((m < 0) || (m > 59)) {
200  throw new IllegalArgumentException("minutes are out of range in " + time);
201  }
202 
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]);
208 
209  if ((m < 0) || (m > 59)) {
210  throw new IllegalArgumentException("minutes are out of range in " + time);
211  }
212  if ((s < 0) || (s >= 60)) {
213  throw new IllegalArgumentException("seconds are out of range in " + time);
214  }
215 
216  seconds = Math.abs(h) * 3600 + m * 60 + s;
217  } else {
218  throw new IllegalArgumentException("time format is not valid in " + time);
219  }
220 
221  if (isNegative) {
222  seconds = -seconds;
223  }
224  return seconds == Time.UNDEFINED_TIME ? OptionalTime.undefined() : OptionalTime.defined(seconds);
225  }
226 
233  public static double convertHHMMInteger(int hhmm) {
234  int h = hhmm / 100;
235  int m = hhmm - (h * 100);
236  double seconds = Math.abs(h) * 3600 + m * 60;
237  return seconds;
238  }
239 
240 }
static final double MIDNIGHT
Definition: Time.java:48
static final String TIMEFORMAT_HHMMSS
Definition: Time.java:51
static double convertHHMMInteger(int hhmm)
Definition: Time.java:233
static final String TIMEFORMAT_SSSS
Definition: Time.java:52
static String [] explode(final String str, final char delimiter, final int limit)
static final String writeTime(final double seconds, final char separator)
Definition: Time.java:84
static final OptionalTime parseOptionalTime(final String time)
Definition: Time.java:167
static final double parseTime(final String time)
Definition: Time.java:163
static final void setDefaultTimeFormat(final String format)
Definition: Time.java:76
static String defaultTimeFormat
Definition: Time.java:56
static final String writeTime(final double seconds, final String timeformat, final char separator)
Definition: Time.java:104
static final String writeTime(final double seconds, final String timeformat)
Definition: Time.java:80
static OptionalTime defined(double seconds)
static final String [] timeElements
Definition: Time.java:58
static final OptionalTime parseTime(final String time, final char separator)
Definition: Time.java:184
static final String TIMEFORMAT_HHMM
Definition: Time.java:50
static final String writeTime(final double seconds)
Definition: Time.java:88
static final String TIMEFORMAT_HHMMSSDOTSS
Definition: Time.java:54
static final String writeTime(final OptionalTime time)
Definition: Time.java:92