-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added date and time conversion * Added LocalDateTime conversion capabilities.
- Loading branch information
1 parent
922e664
commit cbf3e41
Showing
9 changed files
with
1,174 additions
and
1,020 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
simulation/src/main/java/gov/hhs/aspr/ms/gcm/nucleus/SimulationTimeConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package gov.hhs.aspr.ms.gcm.nucleus; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.ZoneOffset; | ||
|
||
public final class SimulationTimeConverter { | ||
|
||
private final LocalDateTime baseDateTime; | ||
private final double baseSecondsSinceEpoch; | ||
private final double billion = 1_000_000_000.0; | ||
private final double billionith = 1.0/billion; | ||
|
||
public SimulationTimeConverter(LocalDateTime baseDateTime){ | ||
this.baseDateTime = baseDateTime; | ||
baseSecondsSinceEpoch = getSecondsSinceEpoch(baseDateTime); | ||
} | ||
|
||
/* | ||
* Returns the number of seconds since the epoch | ||
*/ | ||
private double getSecondsSinceEpoch(LocalDateTime dateTime) { | ||
double result = dateTime.getNano(); | ||
result *= billionith; | ||
result += dateTime.toEpochSecond(ZoneOffset.UTC); | ||
return result; | ||
} | ||
|
||
public double getSimulationTime(LocalDateTime dateTime) { | ||
return (getSecondsSinceEpoch(dateTime) - baseSecondsSinceEpoch)/86400; | ||
} | ||
|
||
public LocalDateTime getLocalDateTime(double simulationTime) { | ||
double t = getSecondsSinceEpoch(baseDateTime); | ||
t += simulationTime*86400; | ||
long epochSeconds = (long)t; | ||
int nanos = (int)((t-epochSeconds)*1_000_000_000.0); | ||
LocalDateTime result = LocalDateTime.ofEpochSecond(epochSeconds,nanos, ZoneOffset.UTC); | ||
return result; | ||
} | ||
|
||
|
||
|
||
} |
Oops, something went wrong.