Time Formatting Parsing in ActionScript

ActionScript doesn’t have built-in time formatting and parsing capabilities as robust as Go’s. However, we can achieve similar functionality using the Date class and custom functions. Here’s an example of how to format and parse dates in ActionScript:

package {
    import flash.display.Sprite;
    import flash.utils.getTimer;

    public class TimeFormattingParsing extends Sprite {
        public function TimeFormattingParsing() {
            var p:Function = trace;

            // Get current time
            var now:Date = new Date();
            
            // Format the date according to ISO 8601 (similar to RFC3339)
            p(formatISO8601(now));

            // Parse a date string
            var parsedDate:Date = parseISO8601("2012-11-01T22:08:41+00:00");
            p(parsedDate);

            // Custom date formatting
            p(formatDate(now, "h:mmA"));
            p(formatDate(now, "EEE MMM d HH:mm:ss yyyy"));
            
            // Custom date parsing
            var customParsedDate:Date = parseCustomFormat("8:41 PM", "h:mm A");
            p(customParsedDate);

            // Numeric representation
            p(formatDate(now, "yyyy-MM-dd'T'HH:mm:ss-00:00"));

            // Error handling for parsing
            try {
                parseCustomFormat("8:41PM", "EEE MMM d HH:mm:ss yyyy");
            } catch (e:Error) {
                p("Parsing error: " + e.message);
            }
        }

        private function formatISO8601(date:Date):String {
            var offset:Number = date.timezoneOffset;
            var sign:String = (offset <= 0) ? "+" : "-";
            offset = Math.abs(offset);
            var hours:String = padZero(Math.floor(offset / 60));
            var minutes:String = padZero(offset % 60);
            
            return date.getUTCFullYear() + "-" +
                   padZero(date.getUTCMonth() + 1) + "-" +
                   padZero(date.getUTCDate()) + "T" +
                   padZero(date.getUTCHours()) + ":" +
                   padZero(date.getUTCMinutes()) + ":" +
                   padZero(date.getUTCSeconds()) +
                   sign + hours + ":" + minutes;
        }

        private function parseISO8601(dateString:String):Date {
            var regex:RegExp = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})([+-])(\d{2}):(\d{2})/;
            var parts:Array = regex.exec(dateString);
            
            if (parts) {
                var date:Date = new Date(
                    parseInt(parts[1]), parseInt(parts[2]) - 1, parseInt(parts[3]),
                    parseInt(parts[4]), parseInt(parts[5]), parseInt(parts[6])
                );
                
                var offset:Number = (parseInt(parts[8]) * 60 + parseInt(parts[9])) * (parts[7] == "+" ? -1 : 1);
                date.time += offset * 60 * 1000;
                
                return date;
            }
            
            return null;
        }

        private function formatDate(date:Date, format:String):String {
            var result:String = format;
            result = result.replace(/yyyy/g, date.fullYear.toString());
            result = result.replace(/MM/g, padZero(date.month + 1));
            result = result.replace(/dd/g, padZero(date.date));
            result = result.replace(/HH/g, padZero(date.hours));
            result = result.replace(/mm/g, padZero(date.minutes));
            result = result.replace(/ss/g, padZero(date.seconds));
            result = result.replace(/h/g, (date.hours % 12 || 12).toString());
            result = result.replace(/A/g, date.hours < 12 ? "AM" : "PM");
            result = result.replace(/EEE/g, ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][date.day]);
            return result;
        }

        private function parseCustomFormat(dateString:String, format:String):Date {
            // This is a simplified parser and doesn't handle all cases
            var date:Date = new Date();
            var formatParts:Array = format.split(/[^a-zA-Z]/);
            var dateParts:Array = dateString.split(/[^0-9a-zA-Z]/);
            
            for (var i:int = 0; i < formatParts.length; i++) {
                switch (formatParts[i]) {
                    case "yyyy": date.fullYear = parseInt(dateParts[i]); break;
                    case "MM": date.month = parseInt(dateParts[i]) - 1; break;
                    case "dd": date.date = parseInt(dateParts[i]); break;
                    case "HH": date.hours = parseInt(dateParts[i]); break;
                    case "h": 
                        var hours:int = parseInt(dateParts[i]);
                        date.hours = (hours == 12) ? 0 : hours; 
                        break;
                    case "mm": date.minutes = parseInt(dateParts[i]); break;
                    case "ss": date.seconds = parseInt(dateParts[i]); break;
                    case "A": 
                        if (dateParts[i] == "PM" && date.hours < 12) date.hours += 12;
                        if (dateParts[i] == "AM" && date.hours == 12) date.hours = 0;
                        break;
                }
            }
            
            return date;
        }

        private function padZero(num:int):String {
            return (num < 10 ? "0" : "") + num;
        }
    }
}

This ActionScript example provides similar functionality to the original code:

  1. We define custom functions for formatting and parsing dates, as ActionScript doesn’t have built-in methods for these operations.

  2. The formatISO8601 function creates a string representation of a date in a format similar to RFC3339.

  3. The parseISO8601 function parses a date string in ISO 8601 format.

  4. We use custom formatDate and parseCustomFormat functions to handle various date formats.

  5. Error handling is demonstrated when trying to parse an invalid date format.

  6. The numeric representation is achieved using the custom formatDate function.

Note that ActionScript’s date handling capabilities are not as extensive as those in some other languages, so this example provides a simplified version of the functionality. In a real-world scenario, you might want to use a more robust date/time library for ActionScript to handle complex formatting and parsing tasks.