Time Formatting Parsing in OpenSCAD

Our first example demonstrates time formatting and parsing in OpenSCAD. While OpenSCAD doesn’t have built-in time manipulation functions like some other languages, we can simulate these operations using custom functions and string manipulation.

// Custom function to get current time (simulated)
function current_time() = [2023, 6, 15, 10, 30, 0];

// Custom function to format time
function format_time(time, format) =
    format == "RFC3339" ?
        str(time[0], "-", time[1] < 10 ? "0" : "", time[1], "-", time[2] < 10 ? "0" : "", time[2],
            "T", time[3] < 10 ? "0" : "", time[3], ":", time[4] < 10 ? "0" : "", time[4], ":", time[5] < 10 ? "0" : "", time[5], "Z") :
    format == "3:04PM" ?
        str(time[3] > 12 ? time[3] - 12 : time[3], ":", time[4] < 10 ? "0" : "", time[4], time[3] >= 12 ? "PM" : "AM") :
    "Unsupported format";

// Custom function to parse time (simplified)
function parse_time(time_str, format) =
    format == "RFC3339" ?
        let(
            parts = [for(p = strsplit(time_str, "T")) each strsplit(p, "-:")],
            year = toInt(parts[0]),
            month = toInt(parts[1]),
            day = toInt(parts[2]),
            hour = toInt(parts[3]),
            minute = toInt(parts[4]),
            second = toInt(parts[5])
        ) [year, month, day, hour, minute, second] :
    [0, 0, 0, 0, 0, 0];  // Return a default time for unsupported formats

// Main script
function main() =
let(
    t = current_time(),
    formatted_rfc3339 = format_time(t, "RFC3339"),
    formatted_time = format_time(t, "3:04PM"),
    parsed_time = parse_time("2012-11-01T22:08:41Z", "RFC3339")
) [
    str("Current time (RFC3339): ", formatted_rfc3339),
    str("Current time (3:04PM): ", formatted_time),
    str("Parsed time: ", parsed_time)
];

// Execute main function and output results
results = main();
for (result = results) {
    echo(result);
}

In this OpenSCAD script, we’ve simulated time formatting and parsing operations:

  1. We define a current_time() function that returns a simulated current time.

  2. The format_time() function takes a time array and a format string, and returns the formatted time as a string. We’ve implemented two formats: “RFC3339” and “3:04PM”.

  3. The parse_time() function takes a time string and a format, and attempts to parse it into a time array. We’ve implemented a simple parser for the “RFC3339” format.

  4. In the main() function, we demonstrate using these functions to format the current time and parse a given time string.

  5. Finally, we execute the main() function and output the results using echo().

When you run this script in OpenSCAD, it will output the formatted and parsed times to the console. Note that OpenSCAD doesn’t have built-in time manipulation functions, so this is a simplified simulation of time operations.

Remember that OpenSCAD is primarily designed for creating 3D models, so these string operations and time manipulations are not its strong suit. In a real-world scenario, you might handle time operations in a more suitable language and use OpenSCAD purely for the 3D modeling aspects of your project.