Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Unic.yyp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions objects/obj_unic_test/Create_0.gml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ repeat(3)
show_debug_message(UnicGetCharacters("ja"));
show_debug_message(json_stringify(UnicBlockGetRanges(UnicGetCharacters("ja")), true));

show_debug_message(UnicConvertTimestamp("<t:1763816220:d>"));
show_debug_message(UnicConvertTimestamp("<t:1763816220:D>"));
show_debug_message(UnicConvertTimestamp("<t:1763816220:t>"));
show_debug_message(UnicConvertTimestamp("<t:1763816220:T>"));
show_debug_message(UnicConvertTimestamp("<t:1763816220:f>"));
show_debug_message(UnicConvertTimestamp("<t:1763816220:F>"));
show_debug_message(UnicConvertTimestamp("<t:1763816220:s>"));
show_debug_message(UnicConvertTimestamp("<t:1763816220:S>"));

if (os_get_config() == "Unit_Test") {
game_end();
}
129 changes: 129 additions & 0 deletions scripts/UnicConvertTimestamp/UnicConvertTimestamp.gml
Original file line number Diff line number Diff line change
@@ -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 `"<t:1763816220:d>"` 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:
///
/// `"<t:1763816220:d>"` = `"22/11/2025"`
/// `"<t:1763816220:D>"` = `"22 November 2025"`
/// `"<t:1763816220:t>"` = `"12:57 PM"`
/// `"<t:1763816220:T>"` = `"12:57:00 PM"`
/// `"<t:1763816220:f>"` = `"22 November 2025, 12:57 PM"`
/// `"<t:1763816220:F>"` = `"Saturday, 22 November 2025, 12:57 PM"`
/// `"<t:1763816220:s>"` = `"22/11/2025, 12:57 PM"`
/// `"<t:1763816220:S>"` = `"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;
}
}
13 changes: 13 additions & 0 deletions scripts/UnicConvertTimestamp/UnicConvertTimestamp.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 13 additions & 11 deletions scripts/UnicDateExt/UnicDateExt.gml
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
{
Expand All @@ -272,21 +274,21 @@ 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
{
++_formatPos;
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));
}
}
}
Expand All @@ -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
{
Expand All @@ -317,21 +319,21 @@ 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
{
++_formatPos;
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));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/__UnicError/__UnicError.gml
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}