Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,27 @@
String duration(int seconds) {
StringBuilder string = new StringBuilder();
if (seconds >= 604_800 /* 1 wk */) {
string.append("%,d wk".formatted(seconds / 604_800));
seconds %= 604_800;
}
if (seconds >= 86_400 /* 1 d */) {
if (!string.isEmpty()) string.append(", ");
string.append("%d d".formatted(seconds / 86_400));
seconds %= 86_400;
}
if (seconds >= 3600 /* 1 hr */) {
if (!string.isEmpty()) string.append(", ");
string.append("%d hr".formatted(seconds / 3600));
seconds %= 3600;
}
if (seconds >= 60 /* 1 min */) {
if (!string.isEmpty()) string.append(", ");
string.append("%d min".formatted(seconds / 60));
seconds %= 60;
}
if (seconds > 0) {
if (!string.isEmpty()) string.append(", ");
string.append("%d sec".formatted(seconds));
}
return string.toString();
}

View file

@ -0,0 +1,31 @@
public class CompoundDuration {
public static void main(String[] args) {
compound(7259);
compound(86400);
compound(6000_000);
}
private static void compound(long seconds) {
StringBuilder sb = new StringBuilder();
seconds = addUnit(sb, seconds, 604800, " wk, ");
seconds = addUnit(sb, seconds, 86400, " d, ");
seconds = addUnit(sb, seconds, 3600, " hr, ");
seconds = addUnit(sb, seconds, 60, " min, ");
addUnit(sb, seconds, 1, " sec, ");
sb.setLength(sb.length() > 2 ? sb.length() - 2 : 0);
System.out.println(sb);
}
private static long addUnit(StringBuilder sb, long sec, long unit, String s) {
long n;
if ((n = sec / unit) > 0) {
sb.append(n).append(s);
sec %= (n * unit);
}
return sec;
}
}