Time Formatting Parsing in UnrealScript

Our first example demonstrates time formatting and parsing in UnrealScript. Here’s the full source code:

class TimeFormattingParsing extends Object;

var String CurrentTime;

function Init()
{
    local String FormattedTime;
    local String ParsedTime;
    local String CustomFormat;
    local String ParsedCustomTime;

    // Get the current system time
    CurrentTime = class'WorldInfo'.static.GetWorldInfo().TimeSeconds;

    // Format the time according to a standard format (similar to RFC3339)
    FormattedTime = FormatTimeStamp(CurrentTime);
    `log("Formatted time: " $ FormattedTime);

    // Parse a time string
    ParsedTime = ParseTimeStamp("2012-11-01T22:08:41");
    `log("Parsed time: " $ ParsedTime);

    // Custom time formatting
    CustomFormat = FormatTimeCustom(CurrentTime, "%H:%M:%S");
    `log("Custom formatted time: " $ CustomFormat);

    // Parse a custom formatted time string
    ParsedCustomTime = ParseTimeCustom("20:41:00", "%H:%M:%S");
    `log("Parsed custom time: " $ ParsedCustomTime);
}

// Helper function to format timestamp (simplified version)
static function String FormatTimeStamp(float TimeSeconds)
{
    local int Years, Days, Hours, Minutes, Seconds;
    
    class'WorldInfo'.static.GetWorldInfo().GetSystemTime(Years, Days, Hours, Minutes, Seconds);
    return Years $ "-" $ Right("0" $ Days, 2) $ "-" $ Right("0" $ Hours, 2) $ "T" $ 
           Right("0" $ Minutes, 2) $ ":" $ Right("0" $ Seconds, 2) $ "Z";
}

// Helper function to parse timestamp (simplified version)
static function String ParseTimeStamp(String TimeString)
{
    // In a real implementation, this would parse the string and return a proper time value
    return "Parsed time: " $ TimeString;
}

// Helper function for custom time formatting
static function String FormatTimeCustom(float TimeSeconds, String Format)
{
    local int Years, Days, Hours, Minutes, Seconds;
    
    class'WorldInfo'.static.GetWorldInfo().GetSystemTime(Years, Days, Hours, Minutes, Seconds);
    
    Format = Repl(Format, "%Y", Years);
    Format = Repl(Format, "%D", Right("0" $ Days, 2));
    Format = Repl(Format, "%H", Right("0" $ Hours, 2));
    Format = Repl(Format, "%M", Right("0" $ Minutes, 2));
    Format = Repl(Format, "%S", Right("0" $ Seconds, 2));
    
    return Format;
}

// Helper function for parsing custom formatted time
static function String ParseTimeCustom(String TimeString, String Format)
{
    // In a real implementation, this would parse the string according to the format
    return "Parsed custom time: " $ TimeString;
}

UnrealScript doesn’t have built-in time formatting and parsing functions as robust as Go’s. However, we can create similar functionality using the WorldInfo class and custom helper functions.

The Init() function demonstrates various time operations:

  1. We get the current system time using WorldInfo.TimeSeconds.
  2. We format the current time using a custom FormatTimeStamp() function, which is a simplified version of time formatting.
  3. We demonstrate parsing a time string with ParseTimeStamp().
  4. We show custom time formatting with FormatTimeCustom().
  5. Finally, we parse a custom formatted time string with ParseTimeCustom().

Note that the parsing functions in this example are placeholder implementations. In a real scenario, you would need to implement the actual parsing logic.

To use this class, you would typically create an instance of it and call the Init() function:

class MyGame extends GameInfo;

function InitGame(string Options, out string ErrorMessage)
{
    local TimeFormattingParsing TimeFP;

    super.InitGame(Options, ErrorMessage);

    TimeFP = new class'TimeFormattingParsing';
    TimeFP.Init();
}

This will output the formatted and parsed times to the log.

Remember that UnrealScript’s capabilities for time manipulation are more limited compared to more general-purpose languages, so some advanced time operations might require additional custom implementations or use of native code.