From cf49201fbb11a5009f55dc9da16ff9a9582fc95f Mon Sep 17 00:00:00 2001 From: Juju Adams Date: Sat, 22 Nov 2025 13:32:48 +0000 Subject: [PATCH 1/4] Adds `UnicConvertTimestamp()` --- Unic.yyp | 1 + .../UnicConvertTimestamp.gml | 129 ++++++++++++++++++ .../UnicConvertTimestamp.yy | 13 ++ 3 files changed, 143 insertions(+) create mode 100644 scripts/UnicConvertTimestamp/UnicConvertTimestamp.gml create mode 100644 scripts/UnicConvertTimestamp/UnicConvertTimestamp.yy diff --git a/Unic.yyp b/Unic.yyp index 86dd649..90e92bf 100644 --- a/Unic.yyp +++ b/Unic.yyp @@ -75,6 +75,7 @@ {"id":{"name":"UnicBlockGetRangesExt","path":"scripts/UnicBlockGetRangesExt/UnicBlockGetRangesExt.yy",},}, {"id":{"name":"UnicClock","path":"scripts/UnicClock/UnicClock.yy",},}, {"id":{"name":"UnicClockExt","path":"scripts/UnicClockExt/UnicClockExt.yy",},}, + {"id":{"name":"UnicConvertTimestamp","path":"scripts/UnicConvertTimestamp/UnicConvertTimestamp.yy",},}, {"id":{"name":"UnicCurrency","path":"scripts/UnicCurrency/UnicCurrency.yy",},}, {"id":{"name":"UnicDate","path":"scripts/UnicDate/UnicDate.yy",},}, {"id":{"name":"UnicDateExt","path":"scripts/UnicDateExt/UnicDateExt.yy",},}, diff --git a/scripts/UnicConvertTimestamp/UnicConvertTimestamp.gml b/scripts/UnicConvertTimestamp/UnicConvertTimestamp.gml new file mode 100644 index 0000000..593f4f9 --- /dev/null +++ b/scripts/UnicConvertTimestamp/UnicConvertTimestamp.gml @@ -0,0 +1,129 @@ +// Feather disable all + +/// Returns a string that contains the formatted date/time as per `https://hammertime.cyou/en`. +/// The input string should be something like `""` where the decimal string is the +/// time since the Unix epoch (1st January 1970) and the suffix is the format to use. The following +/// formats are supported: +/// +/// `""` = `"22/11/2025"` +/// `""` = `"22 November 2025"` +/// `""` = `"12:57 PM"` +/// `""` = `"12:57:00 PM"` +/// `""` = `"22 November 2025, 12:57 PM"` +/// `""` = `"Saturday, 22 November 2025, 12:57 PM"` +/// `""` = `"22/11/2025, 12:57 PM"` +/// `""` = `"22/11/2025. 12:57:00 PM"` +/// +/// The `:R` relative time format is not supported. If the input string fails to be parsed then +/// this function will return the value of the `fallback` parameter which defaults to an empty +/// string. +/// +/// @param timestampString +/// @param [fallback=""] +/// @param [localeCode] + +function UnicConvertTimestamp(_inTimestampString, _fallback = "", _localeCode) +{ + var _timestampString = _inTimestampString; + + var _stringLength = string_length(_timestampString); + if (string_char_at(_timestampString, 1) == "<") + { + if (string_char_at(_timestampString, _stringLength) != ">") + { + //__UnicError($"Failed to parse \"{_inTimestampString}\"\nFound \"<\" but no matching \">\""); + return _fallback; + } + + _timestampString = string_copy(_timestampString, 2, _stringLength-2); + _stringLength -= 2; + } + + if (string_copy(_timestampString, 1, 2) != "t:") + { + //__UnicError($"Failed to parse \"{_inTimestampString}\"\nCould not find \"t:\" prefix"); + return _fallback; + } + + _timestampString = string_delete(_timestampString, 1, 2); + _stringLength -= 2; + + var _colonCount = string_count(":", _timestampString); + if (_colonCount <= 0) + { + //__UnicError($"Failed to parse \"{_inTimestampString}\"\nCould not find \":\""); + return _fallback; + } + + if (_colonCount > 1) + { + //__UnicError($"Failed to parse \"{_inTimestampString}\"\nMultiple \":\" found"); + return _fallback; + } + + if (string_char_at(_timestampString, _stringLength-1) != ":") + { + //__UnicError($"Failed to parse \"{_inTimestampString}\"\n\":\" in unexpected position"); + return _fallback; + } + + var _timeString = string_copy(_timestampString, 1, _stringLength-2); + var _modeChar = string_char_at(_timestampString, _stringLength); + + if (string_digits(_timeString) != _timeString) + { + //__UnicError($"Failed to parse \"{_inTimestampString}\"\nNumber portion contains non-numeric characters"); + return _fallback; + } + + try + { + var _unixTime = real(_timeString); + } + catch(_error) + { + //show_debug_message(_error); + //__UnicError($"Failed to parse \"{_inTimestampString}\"\nFailed to convert number portion"); + return _fallback; + } + + var _dateTime = date_create_datetime(1970, 1, 1, 0, 0, _unixTime); + + if (_modeChar == "d") + { + return UnicDate(_dateTime, 0, _localeCode); + } + else if (_modeChar == "D") + { + return UnicDate(_dateTime, 2, _localeCode); + } + else if (_modeChar == "t") + { + return UnicClock(_dateTime, false, _localeCode); + } + else if (_modeChar == "T") + { + return UnicClock(_dateTime, true, _localeCode); + } + else if (_modeChar == "f") + { + return UnicDateTime(_dateTime, 2, false, _localeCode); + } + else if (_modeChar == "F") + { + return UnicDateTime(_dateTime, 3, false, _localeCode); + } + else if (_modeChar == "s") + { + return UnicDateTime(_dateTime, 0, false, _localeCode); + } + else if (_modeChar == "S") + { + return UnicDateTime(_dateTime, 0, true, _localeCode); + } + else + { + //__UnicError($"Failed to parse \"{_inTimestampString}\"\nMode \"{_modeChar}\" is not supported"); + return _fallback; + } +} \ No newline at end of file diff --git a/scripts/UnicConvertTimestamp/UnicConvertTimestamp.yy b/scripts/UnicConvertTimestamp/UnicConvertTimestamp.yy new file mode 100644 index 0000000..95bc69c --- /dev/null +++ b/scripts/UnicConvertTimestamp/UnicConvertTimestamp.yy @@ -0,0 +1,13 @@ +{ + "$GMScript":"v1", + "%Name":"UnicConvertTimestamp", + "isCompatibility":false, + "isDnD":false, + "name":"UnicConvertTimestamp", + "parent":{ + "name":"Formatters", + "path":"folders/Unic/Formatters.yy", + }, + "resourceType":"GMScript", + "resourceVersion":"2.0", +} \ No newline at end of file From 09a33f07b947a243cb5cff7ee64c370bbc266d26 Mon Sep 17 00:00:00 2001 From: Juju Adams Date: Sat, 22 Nov 2025 13:32:57 +0000 Subject: [PATCH 2/4] Expands test case --- objects/obj_unic_test/Create_0.gml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/objects/obj_unic_test/Create_0.gml b/objects/obj_unic_test/Create_0.gml index 5dad7b5..9806f8b 100644 --- a/objects/obj_unic_test/Create_0.gml +++ b/objects/obj_unic_test/Create_0.gml @@ -97,6 +97,15 @@ repeat(3) show_debug_message(UnicGetCharacters("ja")); show_debug_message(json_stringify(UnicBlockGetRanges(UnicGetCharacters("ja")), true)); +show_debug_message(UnicConvertTimestamp("")); +show_debug_message(UnicConvertTimestamp("")); +show_debug_message(UnicConvertTimestamp("")); +show_debug_message(UnicConvertTimestamp("")); +show_debug_message(UnicConvertTimestamp("")); +show_debug_message(UnicConvertTimestamp("")); +show_debug_message(UnicConvertTimestamp("")); +show_debug_message(UnicConvertTimestamp("")); + if (os_get_config() == "Unit_Test") { game_end(); } \ No newline at end of file From 94e0942a6cce9eaad0cc417e96d3a584dde63e1e Mon Sep 17 00:00:00 2001 From: Juju Adams Date: Sat, 22 Nov 2025 13:33:12 +0000 Subject: [PATCH 3/4] Fixes slight formatting error in error function --- scripts/__UnicError/__UnicError.gml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/__UnicError/__UnicError.gml b/scripts/__UnicError/__UnicError.gml index a1999eb..95c11b3 100644 --- a/scripts/__UnicError/__UnicError.gml +++ b/scripts/__UnicError/__UnicError.gml @@ -2,5 +2,5 @@ function __UnicError(_string) { - show_error($"\n Unic v{__UNIC_VERSION}:\n{_string}\n ", true); + show_error($" \nUnic v{__UNIC_VERSION}:\n{_string}\n ", true); } \ No newline at end of file From 6baaf664e0a6d531922f78e28c17fbffc67f6648 Mon Sep 17 00:00:00 2001 From: Juju Adams Date: Sat, 22 Nov 2025 13:40:37 +0000 Subject: [PATCH 4/4] Fixes incorrect weekday name for date formatter --- scripts/UnicDateExt/UnicDateExt.gml | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/scripts/UnicDateExt/UnicDateExt.gml b/scripts/UnicDateExt/UnicDateExt.gml index ddf621b..b22d5ea 100644 --- a/scripts/UnicDateExt/UnicDateExt.gml +++ b/scripts/UnicDateExt/UnicDateExt.gml @@ -76,9 +76,11 @@ function UnicDateExt(_year, _month, _day, _lengthFormat = 1, _localeCode = undef date_set_timezone(timezone_utc); var _datetime = date_create_datetime(_year, _month, 1, 1, 1, 1); - _year = max(floor(_year), 0); - _month = clamp(floor(_month), 1, 12); - _day = clamp(floor(_day), 1, date_days_in_month(_datetime)); + _year = max(floor(_year), 0); + _month = clamp(floor(_month), 1, 12); + _day = clamp(floor(_day), 1, date_days_in_month(_datetime)); + + var _weekday = date_get_weekday(_datetime); date_set_timezone(_oldTimezone); @@ -259,7 +261,7 @@ function UnicDateExt(_year, _month, _day, _lengthFormat = 1, _localeCode = undef __UnicError($"Date format for locale \"{_localeCode}\" unsupported (E)"); } - buffer_write(_buffer, buffer_text, UnicGetDayName(_day-1, 1, _localeCode)); + buffer_write(_buffer, buffer_text, UnicGetDayName(_weekday-1, 1, _localeCode)); } else { @@ -272,7 +274,7 @@ function UnicDateExt(_year, _month, _day, _lengthFormat = 1, _localeCode = undef __UnicError($"Date format for locale \"{_localeCode}\" unsupported (EE)"); } - buffer_write(_buffer, buffer_text, UnicGetDayName(_day-1, 1, _localeCode)); + buffer_write(_buffer, buffer_text, UnicGetDayName(_weekday-1, 1, _localeCode)); } else { @@ -280,13 +282,13 @@ function UnicDateExt(_year, _month, _day, _lengthFormat = 1, _localeCode = undef if (string_char_at(_format, _formatPos+1) != "E") { // `EEE` Abbreviated form - buffer_write(_buffer, buffer_text, UnicGetDayName(_day-1, 2, _localeCode)); + buffer_write(_buffer, buffer_text, UnicGetDayName(_weekday-1, 2, _localeCode)); } else { ++_formatPos; // `EEEE` Wide form - buffer_write(_buffer, buffer_text, UnicGetDayName(_day-1, 3, _localeCode)); + buffer_write(_buffer, buffer_text, UnicGetDayName(_weekday-1, 3, _localeCode)); } } } @@ -304,7 +306,7 @@ function UnicDateExt(_year, _month, _day, _lengthFormat = 1, _localeCode = undef __UnicError($"Date format for locale \"{_localeCode}\" unsupported (c)"); } - buffer_write(_buffer, buffer_text, UnicGetDayName(_day-1, 1, _localeCode)); + buffer_write(_buffer, buffer_text, UnicGetDayName(_weekday-1, 1, _localeCode)); } else { @@ -317,7 +319,7 @@ function UnicDateExt(_year, _month, _day, _lengthFormat = 1, _localeCode = undef __UnicError($"Date format for locale \"{_localeCode}\" unsupported (cc)"); } - buffer_write(_buffer, buffer_text, UnicGetDayName(_day-1, 1, _localeCode)); + buffer_write(_buffer, buffer_text, UnicGetDayName(_weekday-1, 1, _localeCode)); } else { @@ -325,13 +327,13 @@ function UnicDateExt(_year, _month, _day, _lengthFormat = 1, _localeCode = undef if (string_char_at(_format, _formatPos+1) != "c") { // `ccc` Abbreviated form - buffer_write(_buffer, buffer_text, UnicGetDayName(_day-1, 2, _localeCode)); + buffer_write(_buffer, buffer_text, UnicGetDayName(_weekday-1, 2, _localeCode)); } else { ++_formatPos; // `cccc` Wide form - buffer_write(_buffer, buffer_text, UnicGetDayName(_day-1, 3, _localeCode)); + buffer_write(_buffer, buffer_text, UnicGetDayName(_weekday-1, 3, _localeCode)); } } }