-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime.java
More file actions
128 lines (98 loc) · 3 KB
/
Time.java
File metadata and controls
128 lines (98 loc) · 3 KB
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
/*******************************************************************************
* Copyright (c) 2013 Yannic Remmet, Maximilian Berger.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v2.1
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
* Contributors:
* Yannic Remmet - initial API and implementation
* Maximilian Berger - initial API and implementation
******************************************************************************/
package util;
import java.security.InvalidParameterException;
import com.google.common.collect.ImmutableSet;
public class Time {
private int nanoseconds;
private int microseconds;
private int milliseconds;
private int seconds;
private int minutes;
private int hours;
public static final ImmutableSet<String> validTimeUnits = ImmutableSet.of("nanoseconds","ns","microseconds","us","milliseconds","ms","seconds","s","minutes","m","hours","h","days","d");
/**@param unit Valid: nanoseconds,ns,microseconds,us,milliseconds,ms,seconds,s,minutes,m,hours,h,days,d*/
public Time(long time, String unit){
if(!Time.validTimeUnits.contains(unit)){
throw new InvalidParameterException("Wrong time unit!");
}else{
switch (unit){
case "nanoseconds":
case "ns":
nanoseconds = ((int) (time-((time = (time / 1000))*1000)));
case "microseconds" :
case "us":
microseconds = (int) (time-((time = (time / 1000))*1000));
case "milliseconds":
case "ms":
milliseconds = (int) (time-((time = (time / 1000))*1000));
case "seconds":
case "s":
seconds = (int) (time-((time = (time / 60))*60));
case "minutes":
case "m":
minutes = (int) (time-((time = (time / 60))*60));
case "hours":
case "h":
hours = (int) (time-((time = (time / 24))*24));
case "days":
case "d":
hours = (int) (time*24);
}
}
}
public Time(long time){ //in milliseconds
this(time,"ms");
}
public Time sub(Time other){
//TODO Implement
return null;
}
public Time add(Time other){
//TODO Implement
return null;
}
public int getNanoseconds() {
return nanoseconds;
}
public int getMicroseconds() {
return microseconds;
}
public int getMilliseconds() {
return milliseconds;
}
public int getSeconds() {
return seconds;
}
public int getMinutes() {
return minutes;
}
public int getHours() {
return hours;
}
public int getCompleteMilliseconds(){
int result = milliseconds; //always round down
result += seconds * 1000;
result += minutes * 60000;
result += hours * 60000000;
return result;
}
public String toString(){
return String.format("%d Hours, %d Minutes, %d Seconds, %d Milliseconds, %d Microseconds, %d Nanoseconds",hours,minutes,seconds,milliseconds,microseconds,nanoseconds);
}
public int toFrames(){
return (getCompleteMilliseconds() / 26);
}
public static int millisecondsFromFrames(int frames){
return frames*26;
}
}