summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/util
diff options
context:
space:
mode:
authorKeuin <[email protected]>2020-04-24 11:55:18 +0800
committerkeuin <[email protected]>2020-04-24 11:55:18 +0800
commitb692a73e039fbdb404ef1219785660dc9eb60e60 (patch)
tree598351d6fcf5f4dcad0212e4cca37ffc16966e0d /src/main/java/com/keuin/kbackupfabric/util
parent87f5cd97ab22b17a948eb9af1b49a0b1e777048d (diff)
Added /kb delete <backup_name> for deleting an backup (with auto-complete supported).
Command /kb confirm now confirms both /kb restore and /kb delete. Added /kb prev for easily check out the latest backup.
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/util')
-rw-r--r--src/main/java/com/keuin/kbackupfabric/util/BackupFilesystemUtil.java25
-rw-r--r--src/main/java/com/keuin/kbackupfabric/util/BackupNameTimeFormatter.java22
2 files changed, 47 insertions, 0 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/util/BackupFilesystemUtil.java b/src/main/java/com/keuin/kbackupfabric/util/BackupFilesystemUtil.java
index 5b8ba5a..ce39615 100644
--- a/src/main/java/com/keuin/kbackupfabric/util/BackupFilesystemUtil.java
+++ b/src/main/java/com/keuin/kbackupfabric/util/BackupFilesystemUtil.java
@@ -5,6 +5,8 @@ import net.minecraft.server.world.ThreadedAnvilChunkStorage;
import net.minecraft.world.World;
import java.io.File;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
/**
* Functions deal with file name, directory name about Minecraft saves.
@@ -50,4 +52,27 @@ public final class BackupFilesystemUtil {
public static String getBackupFileNamePrefix() {
return backupFileNamePrefix;
}
+
+ public static long getBackupTimeFromBackupFileName(String backupFileName) {
+ Matcher matcher = Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2}").matcher(backupFileName);
+ if (matcher.find()) {
+ String timeString = matcher.group(0);
+ long timeStamp = BackupNameTimeFormatter.timeStringToEpochSeconds(timeString);
+ System.out.println(backupFileName + " -> " + timeStamp);
+ return timeStamp;
+ } else {
+ System.err.println("Failed to extract time from " + backupFileName);
+ }
+ return -1;
+ }
+
+ public static String humanFileSize(long size) {
+ double fileSize = size * 1.0 / 1024 / 1024; // Default unit is MB
+ if (fileSize > 1000)
+ //msgInfo(context, String.format("File size: %.2fGB", fileSize / 1024));
+ return String.format("%.2fGB", fileSize / 1024);
+ else
+ //msgInfo(context, String.format("File size: %.2fMB", fileSize));
+ return String.format("%.2fMB", fileSize);
+ }
}
diff --git a/src/main/java/com/keuin/kbackupfabric/util/BackupNameTimeFormatter.java b/src/main/java/com/keuin/kbackupfabric/util/BackupNameTimeFormatter.java
new file mode 100644
index 0000000..5d00270
--- /dev/null
+++ b/src/main/java/com/keuin/kbackupfabric/util/BackupNameTimeFormatter.java
@@ -0,0 +1,22 @@
+package com.keuin.kbackupfabric.util;
+
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.time.format.DateTimeFormatter;
+
+public class BackupNameTimeFormatter {
+ private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss");
+
+ public static String getTimeString() {
+ return LocalDateTime.now().format(formatter);
+ }
+
+ public static long timeStringToEpochSeconds(String timeString) {
+ ZoneId systemZone = ZoneId.systemDefault(); // my timezone
+ LocalDateTime localDateTime = LocalDateTime.parse(timeString, formatter);
+ ZoneOffset currentOffsetForMyZone = systemZone.getRules().getOffset(localDateTime);
+ return localDateTime.toEpochSecond(currentOffsetForMyZone);
+ }
+
+}