Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
NetworkedGraphicsMV3500
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Savage
NetworkedGraphicsMV3500
Commits
f6a1bdbf
Commit
f6a1bdbf
authored
2 years ago
by
Brutzman, Don
Browse files
Options
Downloads
Patches
Plain Diff
improved java.time conversion routines
parent
fddb1b07
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/edu/nps/moves/dis7/utilities/DisTime.java
+114
-14
114 additions, 14 deletions
src/edu/nps/moves/dis7/utilities/DisTime.java
src/edu/nps/moves/dis7/utilities/DisTimeLog.txt
+24
-17
24 additions, 17 deletions
src/edu/nps/moves/dis7/utilities/DisTimeLog.txt
with
138 additions
and
31 deletions
src/edu/nps/moves/dis7/utilities/DisTime.java
+
114
−
14
View file @
f6a1bdbf
...
...
@@ -8,6 +8,8 @@ package edu.nps.moves.dis7.utilities;
import
edu.nps.moves.dis7.pdus.*
;
import
java.text.SimpleDateFormat
;
import
java.time.*
;
import
java.time.format.DateTimeFormatter
;
import
java.time.temporal.ChronoUnit
;
import
java.util.*
;
/**
...
...
@@ -171,6 +173,82 @@ public class DisTime
private
static
final
String
dateFormatPattern
=
"yyyy-mm-dd"
;
private
static
final
String
timeFormatPattern
=
"HH:mm:ss"
;
/** Enumerations for prepared time formatters */
public
enum
TimeFormatterType
{
/** time formatter output accuracy in seconds */
SECONDS
,
/** time formatter output accuracy in tenths of seconds */
TENTHSECONDS
,
/** time formatter output accuracy in hundredths of seconds */
HUNDREDTHSECONDS
,
/** time formatter output accuracy in milliseconds */
MILLISECONDS
,
/** time formatter output accuracy in microseconds */
MICROSECONDS
,
/** time formatter output accuracy in nanoseconds */
NANOSECONDS
;
}
/** Format time <code>HH:mm:ss</code>
* @see java.time.format.DateTimeFormatter */
public
static
final
DateTimeFormatter
timeFormatterSeconds
=
DateTimeFormatter
.
ofPattern
(
"HH:mm:ss"
);
/** Format time <code>HH:mm:ss.S</code>, default
* @see java.time.format.DateTimeFormatter */
public
static
final
DateTimeFormatter
timeFormatterTenthSeconds
=
DateTimeFormatter
.
ofPattern
(
"HH:mm:ss.S"
);
/** Format time <code>HH:mm:ss.SS</code>
* @see java.time.format.DateTimeFormatter */
public
static
final
DateTimeFormatter
timeFormatterHundredthSeconds
=
DateTimeFormatter
.
ofPattern
(
"HH:mm:ss.SS"
);
/** Format time <code>HH:mm:ss.SSS</code>
* @see java.time.format.DateTimeFormatter */
public
static
final
DateTimeFormatter
timeFormatterMilliSeconds
=
DateTimeFormatter
.
ofPattern
(
"HH:mm:ss.SSS"
);
/** Format time <code>HH:mm:ss.SSSSSS</code>
* @see java.time.format.DateTimeFormatter */
public
static
final
DateTimeFormatter
timeFormatterMicroSeconds
=
DateTimeFormatter
.
ofPattern
(
"HH:mm:ss.SSSSSS"
);
/** Format time <code>HH:mm:ss.SSSSSSSSS</code>
* @see java.time.format.DateTimeFormatter */
public
static
final
DateTimeFormatter
timeFormatterNanoSeconds
=
DateTimeFormatter
.
ofPattern
(
"HH:mm:ss.SSSSSSSSS"
);
private
static
DateTimeFormatter
timeFormatter
=
timeFormatterTenthSeconds
;
/**
* Get time format used for text logging
* @return current timeFormatter
*/
public
static
DateTimeFormatter
getTimeFormatter
()
{
return
timeFormatter
;
}
/**
* Set time format for text logging
* @param timeFormatterChoice enumeration for the new timeFormatter to set
*/
public
static
void
setTimeFormatter
(
DisTime
.
TimeFormatterType
timeFormatterChoice
)
{
switch
(
timeFormatterChoice
)
{
case
SECONDS:
timeFormatter
=
DisTime
.
timeFormatterSeconds
;
break
;
case
TENTHSECONDS:
timeFormatter
=
DisTime
.
timeFormatterTenthSeconds
;
break
;
case
HUNDREDTHSECONDS:
timeFormatter
=
DisTime
.
timeFormatterHundredthSeconds
;
break
;
case
MILLISECONDS:
timeFormatter
=
DisTime
.
timeFormatterMilliSeconds
;
break
;
case
MICROSECONDS:
timeFormatter
=
DisTime
.
timeFormatterMicroSeconds
;
break
;
case
NANOSECONDS:
timeFormatter
=
DisTime
.
timeFormatterNanoSeconds
;
break
;
// no others allowed
}
}
// public static DisTime disTime = null;
/**
...
...
@@ -373,40 +451,54 @@ public class DisTime
}
/**
* Convert timestamp value to Instant for time operations
s,
*
taking
into account epochLvc and TimeStampStyle (DIS absolute/relative, Unix or Year).
* Convert timestamp value to Instant for time operations
.
*
TODO take
into account epochLvc and TimeStampStyle (DIS absolute/relative, Unix or Year).
* TODO consider different formats for different timestampStyle values.
* @param timestamp value in
milli
seconds
* @param timestamp value in seconds
* @return corresponding Instant value (with 31-bit fidelity)
*/
public
static
Instant
convertToInstant
(
int
timestamp
)
public
static
Instant
convertToInstant
(
long
timestamp
)
{
return
Instant
.
now
();
// TODO
// https://stackoverflow.com/questions/57411881/create-java-datetime-instant-from-microseconds
return
Instant
.
EPOCH
.
plus
(
timestamp
,
ChronoUnit
.
SECONDS
);
}
/**
* Convert timestamp value to
Instant
for time operations
,
*
taking
into account epochLvc and TimeStampStyle (DIS absolute/relative, Unix or Year).
* Convert timestamp value to
LocalTime
for time operations
.
*
TODO take
into account epochLvc and TimeStampStyle (DIS absolute/relative, Unix or Year).
* TODO consider different formats for different timestampStyle values.
* @param timestamp value in
milli
seconds
* @param timestamp value in seconds
* @return corresponding Instant value (with 31-bit fidelity)
*/
public
static
Local
Date
Time
convertToLocal
Date
Time
(
int
timestamp
)
public
static
LocalTime
convertToLocalTime
(
long
timestamp
)
{
return
Local
Date
Time
.
now
();
// TODO
return
LocalTime
.
ofSecondOfDay
(
timestamp
);
}
/**
* Convert timestamp value to
Instant
for time operations
,
*
taking
into account epochLvc and TimeStampStyle (DIS absolute/relative, Unix or Year).
* Convert timestamp value to
LocalDateTime
for time operations
.
*
TODO take
into account epochLvc and TimeStampStyle (DIS absolute/relative, Unix or Year).
* TODO consider different formats for different timestampStyle values.
* @param timestamp value in
milli
seconds
* @return corresponding
Instant
value (with 31-bit fidelity)
* @param timestamp value in seconds
* @return corresponding
LocalDateTime
value (with 31-bit fidelity)
*/
public
static
LocalDateTime
convertToLocalDateTime
(
long
timestamp
)
{
ZoneOffset
zoneOffset
=
ZoneOffset
.
UTC
;
return
LocalDateTime
.
ofEpochSecond
(
timestamp
,
0
,
zoneOffset
);
}
/**
* TODO Convert timestamp value to ZonedDateTime for time operations.
* TODO take into account epochLvc and TimeStampStyle (DIS absolute/relative, Unix or Year).
* TODO consider different formats for different timestampStyle values.
* @param timestamp value in LocalDateTime
* @return corresponding Instant value (with 31-bit fidelity)
public static ZonedDateTime convertToZonedDateTime(int timestamp)
{
return ZonedDateTime.now(); // TODO
}
*/
/**
* Convert timestamp value to string for logging and diagnostics,
...
...
@@ -563,6 +655,7 @@ public class DisTime
}
catch
(
InterruptedException
ex
)
{}
}
/** Self-test for basic smoke testing */
private
void
selfTest
()
{
...
...
@@ -609,6 +702,13 @@ public class DisTime
System
.
out
.
println
(
"DisTime.getEpochLvc(), Instant.now(), duration = "
+
DisTime
.
getEpochLvc
()
+
", "
+
now
+
", "
+
java
.
time
.
Duration
.
between
(
getEpochLvc
(),
now
).
toMillis
()
+
" msec"
);
// System.out.println("DisTime.getCurrentDisTimeUnitsSinceTopOfHour() = " + convertToString(DisTime.getCurrentDisTimeUnitsSinceTopOfHour()) + " = " + DisTime.getCurrentDisTimeUnitsSinceTopOfHour());
long
timeSeconds
=
(
83
*
60
);
// 83 minutes * 60 seconds/minute = 00:01:23
System
.
out
.
println
(
"time checks:"
);
System
.
out
.
println
(
"timeSeconds (83 minutes * 60 seconds/minute) = "
+
timeSeconds
+
" seconds"
);
System
.
out
.
println
(
"DisTIme.convertToLocalTime(timeSeconds) = "
+
DisTime
.
convertToLocalTime
(
timeSeconds
).
format
(
DisTime
.
getTimeFormatter
()));
System
.
out
.
println
(
"DisTime.convertToLocalDateTime(timeSeconds) = "
+
DisTime
.
convertToLocalDateTime
(
timeSeconds
).
format
(
DisTime
.
getTimeFormatter
()));
System
.
out
.
println
(
"DisTime.convertToInstant(timeSeconds) = "
+
DisTime
.
convertToInstant
(
timeSeconds
));
}
/**
...
...
This diff is collapsed.
Click to expand it.
src/edu/nps/moves/dis7/utilities/DisTimeLog.txt
+
24
−
17
View file @
f6a1bdbf
ant -f C:\\x3d-github\\open
-
dis7-java -Dnb.internal.action.name=run.single -Djavac.includes=edu/nps/moves/dis7/utilities/DisTime.java -Drun.class=edu.nps.moves.dis7.utilities.DisTime run-single
ant -f C:\\x3d-github\\opendis7-java -Dnb.internal.action.name=run.single -Djavac.includes=edu/nps/moves/dis7/utilities/DisTime.java -Drun.class=edu.nps.moves.dis7.utilities.DisTime run-single
init:
Deleting: C:\x3d-github\open
-
dis7-java\build\built-jar.properties
Deleting: C:\x3d-github\opendis7-java\build\built-jar.properties
deps-jar:
Updating property file: C:\x3d-github\open-dis7-java\build\built-jar.properties
Compiling 1 source file to C:\x3d-github\open-dis7-java\build\classes
Updating property file: C:\x3d-github\opendis7-java\build\built-jar.properties
Compiling 1 source file to C:\x3d-github\opendis7-java\build\classes
warning: [options] bootstrap class path not set in conjunction with -source 8
1 warning
compile-single:
run-single:
*** DisTime.main() self test started...
=== legacy java.util.Date, calendar methods ===
DisTime.getTimestampStyle() = IEEE_ABSOLUTE
patterns yyyy-mm-dd HH:mm:ss
DisTime.getCurrentDisTimestamp() initialTimestamp = 19
69-47-20 22:47:26 = 3369014103 = -925953193
(unsigned vs signed output)
DisTime.getCurrentDisTimestamp() = 19
69-47-20 22:47:37 = 3369026033 = -92594126
3 (unsigned vs signed output)
DisTime.getCurrentDisAbsoluteTimestamp() = 19
69-47-20 22:47:38 = 336902603
3
DisTime.getCurrentDisRelativeTimestamp() = 19
69-47-20 22:47:38 = 336902603
2
DisTime.getCurrentDisTimeUnitsSinceTopOfHour() = 1970-
5
5-
2
0
0
3:
5
5:
1
3 =
168451301
6
DisTime.getCurrentDisTimestamp() initialTimestamp = 19
70-50-20 10:50:56 = 1709456635 = 1709456635
(unsigned vs signed output)
DisTime.getCurrentDisTimestamp() = 19
70-51-20 10:51:03 = 1709463793 = 170946379
3 (unsigned vs signed output)
DisTime.getCurrentDisAbsoluteTimestamp() = 19
70-51-20 10:51:03 = 170946379
3
DisTime.getCurrentDisRelativeTimestamp() = 19
70-51-20 10:51:03 = 170946379
2
DisTime.getCurrentDisTimeUnitsSinceTopOfHour() = 1970-
2
5-
1
0
1
3:
2
5:3
1
=
85473189
6
=== modern java.time methods ===
note that LocalDateTime is current time zone while default Instant is UTC with time zone Z appended
java.time.LocalDateTime.now(), Instant.now() = 2022-0
1
-2
9T11:47:03.8895348
00, 2022-0
1
-2
9T19:47:03.8895348
00Z
java.time.LocalDateTime.now(), Instant.now() = 2022-0
1
-2
9T11:47:03.8895348
00, 2022-0
1
-2
9T19:47:03.8895348
00Z
java.time.LocalDateTime.now(), Instant.now() = 2022-0
1
-2
9T11:47:03.9013301
00, 2022-0
1
-2
9T19:47:03.9013301
00Z
java.time.LocalDateTime.now(), Instant.now() = 2022-0
6
-2
2T21:23:52.8673365
00, 2022-0
6
-2
3T04:23:52.8683423
00Z
java.time.LocalDateTime.now(), Instant.now() = 2022-0
6
-2
2T21:23:52.8683423
00, 2022-0
6
-2
3T04:23:52.8683423
00Z
java.time.LocalDateTime.now(), Instant.now() = 2022-0
6
-2
2T21:23:52.8691427
00, 2022-0
6
-2
3T04:23:52.8691427
00Z
sleep for 1000 msec...
java.time.LocalDateTime.now(), Instant.now() = 2022-0
1
-2
9T11:47:04.9056423
00, 2022-0
1
-2
9T19:47:04.9056423
00Z
java.time.LocalDateTime.now(), Instant.now() = 2022-0
1
-2
9T11:47:04.9056423
00, 2022-0
1
-2
9T19:47:04.9056423
00Z
java.time.LocalDateTime.now(), Instant.now() = 2022-0
1
-2
9T11:47:04.906143
, 2022-0
1
-2
9T19:47:04.906143
Z
java.time.LocalDateTime.now(), Instant.now() = 2022-0
6
-2
2T21:23:53.8763647
00, 2022-0
6
-2
3T04:23:53.8767582
00Z
java.time.LocalDateTime.now(), Instant.now() = 2022-0
6
-2
2T21:23:53.8767582
00, 2022-0
6
-2
3T04:23:53.8767582
00Z
java.time.LocalDateTime.now(), Instant.now() = 2022-0
6
-2
2T21:23:53.876758200
, 2022-0
6
-2
3T04:23:53.876758200
Z
DisTime.hasEpochLvc() default = false
DisTime.setEpochLvc(Instant.now())...
DisTime.hasEpochLvc(), = true
...
...
@@ -32,8 +34,13 @@ clearEpochLvc()...
DisTime.hasEpochLvc() = false
DisTime.setEpochLvcNow()...
DisTime.hasEpochLvc(), = true
DisTime.getEpochLvc(), Instant.now(), duration = 2022-0
1
-2
9T19:47:04.9081502
00Z, 2022-0
1
-2
9T19:47:04.9081502
00Z, 0 msec
DisTime.getEpochLvc(), Instant.now(), duration = 2022-0
6
-2
3T04:23:53.8771646
00Z, 2022-0
6
-2
3T04:23:53.8771646
00Z, 0 msec
sleep for 1000 msec...
DisTime.getEpochLvc(), Instant.now(), duration = 2022-01-29T19:47:04.908150200Z, 2022-01-29T19:47:05.913576500Z, 1005 msec
DisTime.getEpochLvc(), Instant.now(), duration = 2022-06-23T04:23:53.877164600Z, 2022-06-23T04:23:54.879450100Z, 1002 msec
time checks:
timeSeconds (83 minutes * 60 seconds/minute) = 4980 seconds
DisTIme.convertToLocalTime(timeSeconds) = 01:23:00.0
DisTime.convertToLocalDateTime(timeSeconds) = 01:23:00.0
DisTime.convertToInstant(timeSeconds) = 1970-01-01T01:23:00Z
*** DisTime.main() self test complete.
BUILD SUCCESSFUL (total time: 3 seconds)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment