blob: 0a5f5e39d7e070837ed7da1df32e104f609fe927 [file] [log] [blame]
Michael Hanlca740d72015-06-16 10:04:58 +02001package de.ids_mannheim.korap.utils;
2
margaretha49cb6882018-07-04 04:19:54 +02003import org.apache.logging.log4j.LogManager;
4import org.apache.logging.log4j.Logger;
Michael Hanlca740d72015-06-16 10:04:58 +02005import org.joda.time.DateTime;
6import org.joda.time.DateTimeZone;
7import org.joda.time.LocalDate;
Michael Hanlc0ed00f2016-06-23 14:33:10 +02008import org.joda.time.format.DateTimeFormat;
Michael Hanlca740d72015-06-16 10:04:58 +02009import org.joda.time.format.DateTimeFormatter;
10import org.joda.time.format.ISODateTimeFormat;
Michael Hanlca740d72015-06-16 10:04:58 +020011
margaretha07a356a2018-07-11 19:12:21 +020012import de.ids_mannheim.korap.config.Attributes;
13
Michael Hanlca740d72015-06-16 10:04:58 +020014import java.math.BigDecimal;
15import java.math.RoundingMode;
16import java.text.DecimalFormat;
17import java.util.ArrayList;
18import java.util.List;
19import java.util.Locale;
20
21/**
22 * @author hanl
23 * <p/>
Michael Hanl8abaf9e2016-05-23 16:46:35 +020024 * calculates current, expiration and inactive time for
25 * security
Michael Hanlca740d72015-06-16 10:04:58 +020026 * purposes.
27 * @return
28 */
29public class TimeUtils {
30
31 private static DecimalFormat df = new DecimalFormat("#.#############");
Michael Hanlc0ed00f2016-06-23 14:33:10 +020032 private static DateTimeFormatter dtf = DateTimeFormat
33 .forPattern("dd/MM/yyyy");
margaretha35e1ca22023-11-16 22:00:01 +010034 private static final DateTimeZone dtz = DateTimeZone
35 .forID(Attributes.DEFAULT_TIME_ZONE);
margarethaa85965d2018-12-19 15:58:21 +010036 private static final boolean DEBUG = false;
margaretha49cb6882018-07-04 04:19:54 +020037 private static Logger jlog = LogManager.getLogger(TimeUtils.class);
Michael Hanlca740d72015-06-16 10:04:58 +020038
Michael Hanl8abaf9e2016-05-23 16:46:35 +020039 public static int convertTimeToSeconds (String expirationVal) {
Michael Hanlca740d72015-06-16 10:04:58 +020040 expirationVal = expirationVal.trim();
41 int finIndex = expirationVal.length() - 1;
42 char entity = expirationVal.charAt(finIndex);
43 int returnSec = Integer.valueOf(expirationVal.substring(0, finIndex));
margarethaa85965d2018-12-19 15:58:21 +010044 if (DEBUG) {
45 jlog.debug("setting time value to " + returnSec + " with time in "
46 + entity);
47 }
Michael Hanlca740d72015-06-16 10:04:58 +020048 switch (entity) {
49 case 'D':
50 return returnSec * 60 * 60 * 24;
51 case 'H':
52 return returnSec * 60 * 60;
53 case 'M':
54 return returnSec * 60;
55 case 'S':
56 return returnSec;
57 default:
margaretha35e1ca22023-11-16 22:00:01 +010058 if (DEBUG) {
59 jlog.debug(
60 "no time unit specified. Trying to read from default (minutes)");
margarethaa85965d2018-12-19 15:58:21 +010061 }
Michael Hanlca740d72015-06-16 10:04:58 +020062 return Integer.valueOf(expirationVal) * 60;
63 }
64
65 }
66
Michael Hanl8abaf9e2016-05-23 16:46:35 +020067 public static DateTime getNow () {
Michael Hanlc0ed00f2016-06-23 14:33:10 +020068 return DateTime.now(dtz);
Michael Hanlca740d72015-06-16 10:04:58 +020069 }
70
margaretha35e1ca22023-11-16 22:00:01 +010071 public static DateTime getTime (String time) {
Michael Hanlc0ed00f2016-06-23 14:33:10 +020072 return DateTime.parse(time).withZone(dtz);
73 }
74
margaretha35e1ca22023-11-16 22:00:01 +010075 public static DateTime getTime (long time) {
Michael Hanlc0ed00f2016-06-23 14:33:10 +020076 return new DateTime(time).withZone(dtz);
77 }
Michael Hanl8abaf9e2016-05-23 16:46:35 +020078
Michael Hanlca740d72015-06-16 10:04:58 +020079 //returns difference in milliseconds
Michael Hanl8abaf9e2016-05-23 16:46:35 +020080 public static long calcDiff (DateTime now, DateTime future) {
margaretha35e1ca22023-11-16 22:00:01 +010081 long diff = (future.withZone(dtz).getMillis()
82 - now.withZone(dtz).getMillis());
Michael Hanlca740d72015-06-16 10:04:58 +020083 return diff;
84 }
85
margaretha35e1ca22023-11-16 22:00:01 +010086 public static boolean isExpired (long time) {
Michael Hanlca740d72015-06-16 10:04:58 +020087 return getNow().isAfter(time);
88
89 }
90
Michael Hanlca740d72015-06-16 10:04:58 +020091 // returns difference in seconds in floating number
Michael Hanl8abaf9e2016-05-23 16:46:35 +020092 public static float floating (DateTime past, DateTime now) {
margaretha35e1ca22023-11-16 22:00:01 +010093 long diff = (now.withZone(dtz).getMillis()
94 - past.withZone(dtz).getMillis());
Michael Hanlca740d72015-06-16 10:04:58 +020095 double fin = diff / 1000.0;
96 BigDecimal bd = new BigDecimal(fin).setScale(8, RoundingMode.HALF_EVEN);
97 return bd.floatValue();
98 }
99
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200100 public static DateTime fromCosmas (String date) {
Michael Hanlca740d72015-06-16 10:04:58 +0200101 int idx = date.length();
102 try {
margaretha35e1ca22023-11-16 22:00:01 +0100103 Integer sec = Integer.valueOf(
104 date.substring((idx = idx - 2), date.length()).trim());
105 Integer min = Integer
106 .valueOf(date.substring((idx = idx - 2), idx + 2).trim());
107 Integer hours = Integer
108 .valueOf(date.substring((idx = idx - 2), idx + 2).trim());
109 Integer day = Integer
110 .valueOf(date.substring((idx = idx - 2), idx + 2).trim());
111 Integer month = Integer
112 .valueOf(date.substring((idx = idx - 2), idx + 2).trim());
113 Integer year = Integer
114 .valueOf(date.substring((idx = idx - 4), idx + 4).trim());
Michael Hanlca740d72015-06-16 10:04:58 +0200115 return new DateTime(year, month, day, hours, min, sec);
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200116 }
117 catch (NumberFormatException e) {
Michael Hanlca740d72015-06-16 10:04:58 +0200118 return getNow().toDateTime();
119 }
120 }
121
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200122 public static String formatDiff (DateTime now, DateTime after) {
Michael Hanlca740d72015-06-16 10:04:58 +0200123 return df.format(calcDiff(now, after));
124 }
125
126 /**
127 * converts time to the ISO8601 standard.
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200128 *
Michael Hanlca740d72015-06-16 10:04:58 +0200129 * @param time
130 * @return
131 */
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200132 public static String format (DateTime time) {
Michael Hanlca740d72015-06-16 10:04:58 +0200133 DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
Michael Hanlc0ed00f2016-06-23 14:33:10 +0200134 return fmt.withZone(dtz).print(time);
Michael Hanlca740d72015-06-16 10:04:58 +0200135 }
136
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200137 public static String format (long time) {
Michael Hanlca740d72015-06-16 10:04:58 +0200138 DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
Michael Hanlc0ed00f2016-06-23 14:33:10 +0200139 return fmt.withZone(dtz).print(time);
Michael Hanlca740d72015-06-16 10:04:58 +0200140 }
141
142 /**
143 * calculate expiration time
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200144 *
Michael Hanlca740d72015-06-16 10:04:58 +0200145 * @param creation
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200146 * @param plus
147 * time in seconds
Michael Hanlca740d72015-06-16 10:04:58 +0200148 * @return
149 */
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200150 public static DateTime plusSeconds (long creation, int plus) {
Michael Hanlca740d72015-06-16 10:04:58 +0200151 return new DateTime(creation).withZone(dtz).plusSeconds(plus);
152 }
153
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200154 public static DateTime getExpiration (long now, int exp) {
Michael Hanlca740d72015-06-16 10:04:58 +0200155 return new DateTime(now).withZone(dtz).plusSeconds(exp);
156 }
157
158 /**
159 * @param plus
160 * @return
161 */
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200162 public static DateTime plusSeconds (int plus) {
Michael Hanlca740d72015-06-16 10:04:58 +0200163 return getNow().withZone(dtz).plusSeconds(plus);
164 }
165
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200166 public static DateTime plusHours (int hours) {
Michael Hanlca740d72015-06-16 10:04:58 +0200167 return getNow().withZone(dtz).plusHours(hours);
168 }
169
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200170 public static DateTime plusMinutes (int minutes) {
Michael Hanlca740d72015-06-16 10:04:58 +0200171 return getNow().withZone(dtz).plusMinutes(minutes);
172 }
173
174 /**
175 * create time stamp from long value
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200176 *
177 * @param t
178 * time
Michael Hanlca740d72015-06-16 10:04:58 +0200179 * @return Timestamp
180 */
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200181 public static LocalDate getTimeStamp (long t) {
Michael Hanlca740d72015-06-16 10:04:58 +0200182 return new DateTime(t).withZone(dtz).toLocalDate();
183 }
184
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200185 public static DateTime getDate (int day, int month, int year) {
Michael Hanlca740d72015-06-16 10:04:58 +0200186 DateTime date = new DateTime().withZone(dtz);
187 return date.withDate(year, month, day);
188 }
189
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200190 public static String toString (long val, Locale locale) {
Michael Hanlca740d72015-06-16 10:04:58 +0200191 if (locale == Locale.GERMAN)
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200192 return new DateTime(val).toString("dd. MMMM yyyy, HH:mm",
193 Locale.GERMAN);
Michael Hanlca740d72015-06-16 10:04:58 +0200194 else
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200195 return new DateTime(val).toString("MM-dd-yyyy, hh:mm",
196 Locale.ENGLISH);
Michael Hanlca740d72015-06-16 10:04:58 +0200197
198 }
199
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200200 public static String dateToString (long val, int i) {
Michael Hanlca740d72015-06-16 10:04:58 +0200201 switch (i) {
202 case 1:
203 return new DateTime(val).toString("yyyy-MM");
204 case 2:
205 return new DateTime(val).toString("yyyy-MM-dd");
206 default:
207 return new DateTime(val).toString("yyyy");
208 }
209 }
210
211 private static final List<DateTime> times = new ArrayList<>();
212
Michael Hanlca740d72015-06-16 10:04:58 +0200213}