]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/data/TimeDifference.java
3fee2a1f912f19b0482c7185cf695e243c5e9241
[GpsPrune.git] / src / tim / prune / data / TimeDifference.java
1 package tim.prune.data;
2
3 import tim.prune.I18nManager;
4
5 /**
6  * Class to represent a time difference, like the difference between two Timestamp objects,
7  * and methods for representing and displaying them.
8  */
9 public class TimeDifference
10 {
11         private long _totalSeconds = 0L;
12         private int _seconds = 0;
13         private int _minutes = 0;
14         private int _hours = 0;
15         private String _description = null;
16
17
18         /**
19          * Constructor using long
20          * @param inNumSeconds number of seconds time difference
21          */
22         public TimeDifference(long inNumSeconds)
23         {
24                 _totalSeconds = inNumSeconds;
25                 if (inNumSeconds < 0) {inNumSeconds = -inNumSeconds;}
26                 _hours = (int) (inNumSeconds / 60 / 60);
27                 _minutes = (int) (inNumSeconds / 60 - _hours * 60);
28                 _seconds = (int) (inNumSeconds % 60);
29         }
30
31
32         /**
33          * Constructor giving each field separately
34          * @param inHours number of hours
35          * @param inMinutes number of minutes
36          * @param inSeconds number of seconds
37          * @param inPositive true for positive time difference
38          */
39         public TimeDifference(int inHours, int inMinutes, int inSeconds, boolean inPositive)
40         {
41                 // Check for negative values?
42                 _hours = inHours;
43                 _minutes = inMinutes;
44                 _seconds = inSeconds;
45                 _totalSeconds = inHours * 3600L + inMinutes * 60L + inSeconds;
46                 if (!inPositive) {_totalSeconds = -_totalSeconds;}
47         }
48
49
50         /**
51          * @return total number of seconds time difference
52          */
53         public long getTotalSeconds()
54         {
55                 return _totalSeconds;
56         }
57
58         /**
59          * @return number of hours
60          */
61         public int getNumHours()
62         {
63                 return _hours;
64         }
65
66         /**
67          * @return number of minutes
68          */
69         public int getNumMinutes()
70         {
71                 return _minutes;
72         }
73
74         /**
75          * @return number of seconds
76          */
77         public int getNumSeconds()
78         {
79                 return _seconds;
80         }
81
82         /**
83          * @return true if time difference positive
84          */
85         public boolean getIsPositive()
86         {
87                 return _totalSeconds >= 0L;
88         }
89
90
91         /**
92          * Build a String to describe the time duration
93          * @return time as a string, days, hours, mins, secs as appropriate
94          */
95         public String getDescription()
96         {
97                 if (_description != null) {return _description;}
98                 StringBuffer buffer = new StringBuffer();
99                 boolean started = false;
100                 // hours
101                 if (_hours > 0)
102                 {
103                         buffer.append(_hours).append(' ').append(I18nManager.getText("display.range.time.hours"));
104                         started = true;
105                 }
106                 // minutes
107                 if (_minutes > 0)
108                 {
109                         if (started) {buffer.append(", ");}
110                         else {started = true;}
111                         buffer.append(_minutes).append(' ').append(I18nManager.getText("display.range.time.mins"));
112                 }
113                 // seconds
114                 if (_seconds > 0 || !started)
115                 {
116                         if (started) {buffer.append(", ");}
117                         buffer.append(_seconds).append(' ').append(I18nManager.getText("display.range.time.secs"));
118                 }
119                 _description = buffer.toString();
120                 return _description;
121         }
122
123 }