summaryrefslogtreecommitdiffstats
path: root/src/com/gitblit/wicket/GitBlitWebSession.java
blob: 0a15466e50322115de288469c78e98332e904030 (plain)
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
package com.gitblit.wicket;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import org.apache.wicket.Request;
import org.apache.wicket.Session;
import org.apache.wicket.protocol.http.WebSession;
import org.apache.wicket.protocol.http.request.WebClientInfo;

import com.gitblit.StoredSettings;


public final class GitBlitWebSession extends WebSession {

	private static final long serialVersionUID = 1L;

	protected TimeZone timezone = null;

	public GitBlitWebSession(Request request) {
		super(request);
	}

	public void invalidate() {
		super.invalidate();
	}

	public TimeZone getTimezone() {
		if (timezone == null) {
			timezone = ((WebClientInfo) getClientInfo()).getProperties().getTimeZone();
		}
		// use server timezone if we can't determine the client timezone
		if (timezone == null) {
			timezone = TimeZone.getDefault();
		}
		return timezone;
	}

	public String formatTime(Date date) {
		DateFormat df = new SimpleDateFormat(StoredSettings.getString("timestampFormat", "h:mm a"));
		df.setTimeZone(getTimezone());
		return df.format(date);
	}

	public String formatDate(Date date) {
		DateFormat df = new SimpleDateFormat(StoredSettings.getString("datestampShortFormat", "MM/dd/yy"));
		df.setTimeZone(getTimezone());
		return df.format(date);
	}

	public String formatDateLong(Date date) {
		DateFormat df = new SimpleDateFormat(StoredSettings.getString("datestampLongFormat", "EEEE, MMMM d, yyyy"));
		df.setTimeZone(getTimezone());
		return df.format(date);
	}

	public String formatDateTime(Date date) {
		DateFormat df = new SimpleDateFormat(StoredSettings.getString("datetimestampShortFormat", "MM/dd/yy h:mm a"));
		df.setTimeZone(getTimezone());
		return df.format(date);
	}

	public String formatDateTimeLong(Date date) {
		DateFormat df = new SimpleDateFormat(StoredSettings.getString("datetimestampLongFormat", "EEEE, MMMM d, yyyy h:mm a z"));
		df.setTimeZone(getTimezone());
		return df.format(date);
	}

	public static GitBlitWebSession get() {
		return (GitBlitWebSession) Session.get();
	}
}