summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/util/FormatHelper.java
blob: 26c7071a2d7ad9f9b041a7658441941ec43e2263 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package org.openslx.dozmod.util;

import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.openslx.bwlp.thrift.iface.OperatingSystem;
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.
	 * 
	 * @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 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);
	}

	/**
	 * 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);
		}
	}

}