summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/util/FormatHelper.java
blob: 98b48df831a9300c9254c119bf0d6de871032f4c (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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();
		}
	}

}