Epoch in UnrealScript
Our program demonstrates how to get the number of seconds, milliseconds, or nanoseconds since the Unix epoch in UnrealScript. Here’s the full source code:
class EpochExample extends Object;
var DateTime CurrentTime;
function Init()
{
CurrentTime = class'WorldInfo'.static.GetWorldInfo().TimeSeconds;
`log("Current time:" @ CurrentTime);
`log("Seconds since epoch:" @ CurrentTime.ToUnixTimestamp());
`log("Milliseconds since epoch:" @ CurrentTime.ToUnixTimestamp() * 1000);
`log("Nanoseconds since epoch:" @ CurrentTime.ToUnixTimestamp() * 1000000000);
// Convert back from Unix timestamp
local DateTime ConvertedTime;
ConvertedTime = class'DateTime'.static.FromUnixTimestamp(CurrentTime.ToUnixTimestamp());
`log("Converted time:" @ ConvertedTime);
}
DefaultProperties
{
}
In UnrealScript, we don’t have direct equivalents to Go’s time.Now()
, Unix()
, UnixMilli()
, or UnixNano()
functions. Instead, we use the DateTime
class and the WorldInfo.TimeSeconds
property to work with time.
To run this code, you would typically place it in a script file within your Unreal Engine project and then compile and run the project.
The Init()
function serves as our main entry point. Here’s what each part does:
- We get the current time using
WorldInfo.TimeSeconds
. - We print the current time.
- We use
ToUnixTimestamp()
to get seconds since the Unix epoch. - We multiply the result by 1000 or 1000000000 to get milliseconds or nanoseconds.
- We demonstrate converting back from a Unix timestamp using
FromUnixTimestamp()
.
Note that UnrealScript doesn’t provide nanosecond precision, so the nanosecond calculation is an approximation.
UnrealScript doesn’t have a direct equivalent to Go’s ability to run scripts from the command line. Instead, this code would typically be part of a larger UnrealScript project within the Unreal Engine environment.
Next, we might look at how to format and parse time strings in UnrealScript.