started another importer overhaul
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -2,12 +2,17 @@
|
||||
package de.srsoftware.cal;
|
||||
|
||||
import static de.srsoftware.tools.Error.error;
|
||||
import static de.srsoftware.tools.Result.transform;
|
||||
|
||||
import de.srsoftware.cal.api.Coords;
|
||||
import de.srsoftware.tools.Payload;
|
||||
import de.srsoftware.tools.Result;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class Util {
|
||||
public static final String BEGIN = "BEGIN";
|
||||
@@ -26,8 +31,18 @@ public class Util {
|
||||
public static final String VEVENT = "VEVENT";
|
||||
public static final String VCALENDAR = "VCALENDAR";
|
||||
|
||||
public static final Pattern GERMAN_DATE_PATTERN = Pattern.compile("\\D(\\d\\d?)\\.(\\d\\d?)\\.(\\d{4})\\D");
|
||||
public static final Pattern GERMAN_TIME_PATTERN = Pattern.compile("\\D(\\d\\d?):(\\d\\d?)(:(\\d\\d?))?\\D");
|
||||
|
||||
private Util(){}
|
||||
|
||||
|
||||
public static Result<LocalDateTime> combine(Result<LocalDate> date, Result<LocalTime> time) {
|
||||
if (date.optional().isEmpty())return transform(date);
|
||||
if (time.optional().isEmpty())return transform(time);
|
||||
return Payload.of(LocalDateTime.of(date.optional().get(),time.optional().get()));
|
||||
}
|
||||
|
||||
/**
|
||||
* formats a content line as defined in <a href="https://datatracker.ietf.org/doc/html/rfc5545#section-3.1">iCalendar spec</a>
|
||||
* @param key the content line key
|
||||
@@ -76,6 +91,28 @@ public class Util {
|
||||
.replace(":","/");
|
||||
}
|
||||
|
||||
public static Result<LocalDate> parseGermanDate(String s){
|
||||
var match = GERMAN_DATE_PATTERN.matcher(s);
|
||||
if (match.find()){
|
||||
var day = Integer.parseInt(match.group(1));
|
||||
var month = Integer.parseInt(match.group(2));
|
||||
var year = Integer.parseInt(match.group(3));
|
||||
return Payload.of(LocalDate.of(year,month,day));
|
||||
}
|
||||
return error("Failed to find date");
|
||||
}
|
||||
|
||||
public static Result<LocalTime> parseGermanTime(String s){
|
||||
var match = GERMAN_TIME_PATTERN.matcher(s);
|
||||
if (match.find()){
|
||||
var hour = Integer.parseInt(match.group(1));
|
||||
var minute = Integer.parseInt(match.group(2));
|
||||
var sec = match.group(4);
|
||||
var second = sec == null ? 0 : Integer.parseInt(sec);
|
||||
return Payload.of(LocalTime.of(hour,minute,second));
|
||||
}
|
||||
return error("Failed to find date");
|
||||
}
|
||||
|
||||
/**
|
||||
* wraps a text (list of vevents in a vcalendar, as described in th <a href="https://datatracker.ietf.org/doc/html/rfc5545#section-3.4">iCalendar spec</a>
|
||||
|
||||
Reference in New Issue
Block a user