001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.core.util; 020 021import org.checkerframework.checker.index.qual.NonNegative; 022import org.checkerframework.checker.nullness.qual.NonNull; 023 024public final class TimeUtil { 025 026 private TimeUtil() { 027 } 028 029 /** 030 * Format seconds into a string with the format 031 * #y #w #d #h #s 032 * 033 * @param time Time to format 034 * @return Formatted string 035 */ 036 public static @NonNull String secToTime(@NonNegative long time) { 037 StringBuilder toReturn = new StringBuilder(); 038 if (time >= 33868800) { 039 int years = (int) (time / 33868800); 040 time -= years * 33868800L; 041 toReturn.append(years).append("y "); 042 } 043 if (time >= 604800) { 044 int weeks = (int) (time / 604800); 045 time -= weeks * 604800L; 046 toReturn.append(weeks).append("w "); 047 } 048 if (time >= 86400) { 049 int days = (int) (time / 86400); 050 time -= days * 86400L; 051 toReturn.append(days).append("d "); 052 } 053 if (time >= 3600) { 054 int hours = (int) (time / 3600); 055 time -= hours * 3600L; 056 toReturn.append(hours).append("h "); 057 } 058 if (time >= 60) { 059 int minutes = (int) (time / 60); 060 time -= minutes * 60L; 061 toReturn.append(minutes).append("m "); 062 } 063 if (toReturn.length() == 0 || time > 0) { 064 toReturn.append(time).append("s "); 065 } 066 return toReturn.toString().trim(); 067 } 068 069 /** 070 * Parse a time string back into time 071 * 072 * @param string String to parse 073 * @return Parsed time 074 */ 075 @NonNegative 076 public static long timeToSec(@NonNull String string) { 077 if (MathMan.isInteger(string)) { 078 return Long.parseLong(string); 079 } 080 string = string.toLowerCase().trim().toLowerCase(); 081 if (string.equalsIgnoreCase("false")) { 082 return 0; 083 } 084 final String[] split = string.split(" "); 085 long time = 0; 086 for (final String value : split) { 087 final int numbers = Integer.parseInt(value.replaceAll("[^\\d]", "")); 088 String letters = value.replaceAll("[^a-z]", ""); 089 switch (letters) { 090 case "week": 091 case "weeks": 092 case "wks": 093 case "w": 094 095 time += 604800 * numbers; 096 case "days": 097 case "day": 098 case "d": 099 time += 86400 * numbers; 100 case "hour": 101 case "hr": 102 case "hrs": 103 case "hours": 104 case "h": 105 time += 3600 * numbers; 106 case "minutes": 107 case "minute": 108 case "mins": 109 case "min": 110 case "m": 111 time += 60 * numbers; 112 case "seconds": 113 case "second": 114 case "secs": 115 case "sec": 116 case "s": 117 time += numbers; 118 } 119 } 120 return time; 121 } 122 123}