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:

  1. We get the current time using WorldInfo.TimeSeconds.
  2. We print the current time.
  3. We use ToUnixTimestamp() to get seconds since the Unix epoch.
  4. We multiply the result by 1000 or 1000000000 to get milliseconds or nanoseconds.
  5. 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.