package org.openslx.dozmod.util;
import java.util.Date;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.openslx.bwlp.thrift.iface.Location;
import org.openslx.bwlp.thrift.iface.NetShareAuth;
import org.openslx.bwlp.thrift.iface.OperatingSystem;
import org.openslx.bwlp.thrift.iface.Organization;
import org.openslx.bwlp.thrift.iface.UserInfo;
public class FormatHelper {
private static final char THIN_SP = '\u202F';
private static final DateTimeFormatter shortDateFormatter = DateTimeFormat.forPattern("dd.MM.yy HH:mm");
private static final DateTimeFormatter longDateFormatter = DateTimeFormat.forPattern("dd.MM.yyyy HH:mm:ss");
/**
* Convert unix timestamp to short date format.
*
* @param timestamp unix time
* @return dd.MM.yy HH:mm
*/
public static String shortDate(long timestamp) {
if (timestamp == 0)
return "???";
return shortDateFormatter.print(timestamp * 1000l);
}
/**
* Convert {@link Date} to short date format.
*
* @param timestamp unix time
* @return dd.MM.yy HH:mm
*/
public static String shortDate(Date date) {
return shortDate(date.getTime() / 1000);
}
/**
* Convert unix timestamp to long date.
*
* @param timestamp unix time
* @return dd.MM.yyyy HH:mm:ss
*/
public static String longDate(long timestamp) {
if (timestamp == 0)
return "???";
return longDateFormatter.print(timestamp * 1000l);
}
public static String longDate(Date date) {
return longDate(date.getTime() / 1000);
}
/**
* Format the given user's name.
*
* @param user a {@link UserInfo} instance
* @return "LastName, FirstName"
*/
public static String userName(UserInfo user) {
if (user == null)
return "<null>";
return user.getLastName() + ", " + user.getFirstName();
}
/**
* Format the given OS's name.
*
* @param OS a {@link OperatingSystem} instance
* @return "LastName, FirstName"
*/
public static String osName(OperatingSystem os) {
if (os == null)
return "Unknown";
return os.getOsName();
}
/**
* Format bytes using suitable unit prefix.
*
* @param bytes unformatted byte count
* @param si true to use SI mode (1000B = 1KB), false for normal mode
* (1024B = 1KiB)
* @return Formatted string, i.e. <code>54.6 GiB</code>
*/
public static String bytes(long bytes, boolean si) {
final int unit = si ? 1000 : 1024;
if (bytes < unit)
return String.valueOf(bytes) + THIN_SP + "B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPEZY" : "KMGTPEZY").charAt(exp - 1) + (si ? "" : "i");
return String.format("%.1f" + THIN_SP + "%sB", bytes / Math.pow(unit, exp), pre);
}
public static String formatMilliseconds(long ms, boolean withSeconds) {
String ret = "";
long seconds = ms / 1000;
if (seconds >= 86400 * 365) { // More than a year...
long years = seconds / (86400 * 365);
ret = Long.toString(years) + THIN_SP;
if (years == 1) {
ret += "Jahr";
} else {
ret += "Jahre";
}
seconds -= years * 86400 * 365;
}
if (seconds >= 86400) {
long days = seconds / 86400;
if (!ret.isEmpty()) {
ret += ", ";
}
ret += Long.toString(days) + THIN_SP;
if (days == 1) {
ret += "Tag";
} else {
ret += "Tage";
}
seconds -= days * 86400;
}
if (!ret.isEmpty()) {
ret += ", ";
}
if (withSeconds) {
return ret + String.format("%02d:%02d:%02d", seconds / 3600, (seconds % 3600) / 60, seconds % 60);
} else {
return ret + String.format("%02d:%02d", seconds / 3600, (seconds % 3600) / 60);
}
}
/**
* Returns the number of days left til the given end timestamp
*
* @param timestamp to calculate the days until
* @return user-ready String containing the amount of days left if <14 days and > 24h,
* the number of hours left if <24. If end is already passed, then just
* the same as longDate
*/
public static String daysTil(final long timestamp) {
long minutesLeft = (timestamp * 1000 - System.currentTimeMillis()) / (1000 * 60);
if (minutesLeft <= 0)
return "-";
if (minutesLeft < 60)
return new String(minutesLeft + " Minute(n)");
long hoursLeft = minutesLeft / 60;
if (hoursLeft < 24)
return new String(hoursLeft + " Stunde(n)");
long daysLeft = hoursLeft / 24;
if (daysLeft < 14)
return new String(daysLeft + " Tag(e)");
return longDate(timestamp);
}
/**
* Format the given OS's name.
*
* @param OS a {@link OperatingSystem} instance
* @return "LastName, FirstName"
*/
public static String locName(Location location) {
if (location == null)
return "Unknown";
return location.getLocationName();
}
/**
* Format the given organisation name.
*
* @param Organisation a {@link Organisation} instance
* @return Display name for that organisation, string "unknown" otherwise
*/
public static String orgName(Organization org) {
if (org == null)
return "Unknown";
return org.getDisplayName();
}
/**
* Format the given network share authentication type.
*
* @param NetShareAuth a {@link NetShareAuth} instance
* @return Display name for that authentication type, string "unknown" otherwise
*/
public static String netShareAuthName(NetShareAuth auth) {
if (auth == null)
return "Unknown";
switch (auth) {
case LOGIN_USER:
return "Angemeldeter Nutzer";
case OTHER_USER:
return "Spezifischer Nutzer";
default:
return auth.toString();
}
}
}