File: TimeDifference.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (123 lines) | stat: -rw-r--r-- 2,710 bytes parent folder | download | duplicates (7)
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
package tim.prune.data;

import tim.prune.I18nManager;

/**
 * Class to represent a time difference, like the difference between two Timestamp objects,
 * and methods for representing and displaying them.
 */
public class TimeDifference
{
	private long _totalSeconds = 0L;
	private int _seconds = 0;
	private int _minutes = 0;
	private int _hours = 0;
	private String _description = null;


	/**
	 * Constructor using long
	 * @param inNumSeconds number of seconds time difference
	 */
	public TimeDifference(long inNumSeconds)
	{
		_totalSeconds = inNumSeconds;
		if (inNumSeconds < 0) {inNumSeconds = -inNumSeconds;}
		_hours = (int) (inNumSeconds / 60 / 60);
		_minutes = (int) (inNumSeconds / 60 - _hours * 60);
		_seconds = (int) (inNumSeconds % 60);
	}


	/**
	 * Constructor giving each field separately
	 * @param inHours number of hours
	 * @param inMinutes number of minutes
	 * @param inSeconds number of seconds
	 * @param inPositive true for positive time difference
	 */
	public TimeDifference(int inHours, int inMinutes, int inSeconds, boolean inPositive)
	{
		// Check for negative values?
		_hours = inHours;
		_minutes = inMinutes;
		_seconds = inSeconds;
		_totalSeconds = inHours * 3600L + inMinutes * 60L + inSeconds;
		if (!inPositive) {_totalSeconds = -_totalSeconds;}
	}


	/**
	 * @return total number of seconds time difference
	 */
	public long getTotalSeconds()
	{
		return _totalSeconds;
	}

	/**
	 * @return number of hours
	 */
	public int getNumHours()
	{
		return _hours;
	}

	/**
	 * @return number of minutes
	 */
	public int getNumMinutes()
	{
		return _minutes;
	}

	/**
	 * @return number of seconds
	 */
	public int getNumSeconds()
	{
		return _seconds;
	}

	/**
	 * @return true if time difference positive
	 */
	public boolean getIsPositive()
	{
		return _totalSeconds >= 0L;
	}


	/**
	 * Build a String to describe the time duration
	 * @return time as a string, days, hours, mins, secs as appropriate
	 */
	public String getDescription()
	{
		if (_description != null) {return _description;}
		StringBuffer buffer = new StringBuffer();
		boolean started = false;
		// hours
		if (_hours > 0)
		{
			buffer.append(_hours).append(' ').append(I18nManager.getText("display.range.time.hours"));
			started = true;
		}
		// minutes
		if (_minutes > 0)
		{
			if (started) {buffer.append(", ");}
			else {started = true;}
			buffer.append(_minutes).append(' ').append(I18nManager.getText("display.range.time.mins"));
		}
		// seconds
		if (_seconds > 0 || !started)
		{
			if (started) {buffer.append(", ");}
			buffer.append(_seconds).append(' ').append(I18nManager.getText("display.range.time.secs"));
		}
		_description = buffer.toString();
		return _description;
	}

}