MessageMoment.java
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2024, Arnaud Roques
*
* Project Info: https://plantuml.com
*
* If you like this project or if you find it useful, you can support us at:
*
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/paypal
*
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
*
* Original Author: Arnaud Roques
*
*/
package net.sourceforge.plantuml.error.messages;
import java.util.Calendar;
import java.util.GregorianCalendar;
/**
* The current local date and time, read once and split into integers, which is
* all the rules need. {@code java.time} is not used, to stay easy to run under
* TeaVM.
*
* {@link #now()} reads the local clock; {@link #parse(String)} only serves the
* Vega clock, see {@link MessageSelector#setVegaClock(String)}.
*/
public final class MessageMoment {
private final int year;
private final int month;
private final int dayOfMonth;
private final int hour;
private final int minute;
private final int dayOfWeek;
/**
* @param month 1-12 (January is 1)
* @param dayOfWeek 0-6 (Sunday is 0)
*/
MessageMoment(int year, int month, int dayOfMonth, int hour, int minute, int dayOfWeek) {
this.year = year;
this.month = month;
this.dayOfMonth = dayOfMonth;
this.hour = hour;
this.minute = minute;
this.dayOfWeek = dayOfWeek;
}
public static MessageMoment now() {
final Calendar cal = new GregorianCalendar();
return new MessageMoment(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH),
cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY);
}
/**
* Reads a moment written as {@code "2026-12-02 10:15"}; the day of the week is
* computed.
*
* @throws IllegalArgumentException when the moment is not written that way, or
* does not exist
*/
static MessageMoment parse(String moment) {
final String error = "invalid moment '" + moment + "', expected YYYY-MM-DD HH:MM";
if (moment == null)
throw new IllegalArgumentException(error);
final String s = moment.trim();
if (s.length() != 16 || s.charAt(4) != '-' || s.charAt(7) != '-' || s.charAt(10) != ' '
|| s.charAt(13) != ':')
throw new IllegalArgumentException(error);
final int year = parseDigits(s.substring(0, 4));
final int month = parseDigits(s.substring(5, 7));
final int day = parseDigits(s.substring(8, 10));
final int hour = parseDigits(s.substring(11, 13));
final int minute = parseDigits(s.substring(14, 16));
if (year < 1 || month < 1 || month > 12 || day < 1 || hour < 0 || hour > 23 || minute < 0 || minute > 59)
throw new IllegalArgumentException(error);
final Calendar cal = new GregorianCalendar(year, month - 1, day);
cal.setLenient(false);
try {
final int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY;
return new MessageMoment(year, month, day, hour, minute, dayOfWeek);
} catch (IllegalArgumentException e) {
// A day that does not exist, such as 2027-02-30
throw new IllegalArgumentException(error);
}
}
private static int parseDigits(String s) {
int result = 0;
for (int i = 0; i < s.length(); i++) {
final char c = s.charAt(i);
if (c < '0' || c > '9')
return -1;
result = result * 10 + (c - '0');
}
return result;
}
/** The date as one integer, for instance 20271225, easy to compare. */
int getDate() {
return year * 10000 + month * 100 + dayOfMonth;
}
int getMonth() {
return month;
}
int getDayOfMonth() {
return dayOfMonth;
}
int getHour() {
return hour;
}
int getMinute() {
return minute;
}
int getDayOfWeek() {
return dayOfWeek;
}
@Override
public String toString() {
return getDate() + " " + hour + ":" + minute + " dow=" + dayOfWeek;
}
}