Skip to content

Commit

Permalink
Rust datetime fmt and updates to ICU4C and NodeJS (unicode-org#240)
Browse files Browse the repository at this point in the history
* Add plural rules to CPP

* Starting ICU4X datetime fmt

* DateTime format: Set default timezone explicitly

* Fix formatting

* Added ICU4X timezone computation - not working yet!

* DateTime generator updated to ISO formatted string

* DateTime updates for Node and ICU4C.

* Formatted src/datetimefmt.rs

* Updated using CustomTimeZone in ICU4X date time fmt

* Cargo clippified

* Remove unused function

* Update version to ~1.3
  • Loading branch information
sven-oly authored Jul 11, 2024
1 parent fbb30a6 commit 1bb7e44
Show file tree
Hide file tree
Showing 12 changed files with 836 additions and 353 deletions.
32 changes: 28 additions & 4 deletions executors/cpp/datetime_fmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <iostream>
#include <string>
#include <regex>
#include <cstring>

#include "unicode/utypes.h"
Expand Down Expand Up @@ -130,6 +131,10 @@ const string TestDatetimeFmt(json_object *json_in) {
timezone_str = json_object_get_string(option_item);
UnicodeString u_tz(timezone_str.c_str());
tz = TimeZone::createTimeZone(u_tz);
} else {
// Default is UTC
UnicodeString u_tz("UTC");
tz = TimeZone::createTimeZone(u_tz);
}
}

Expand Down Expand Up @@ -180,9 +185,10 @@ const string TestDatetimeFmt(json_object *json_in) {
return json_object_to_json_string(return_json);
}

if (tz) {
df->setTimeZone(*tz);
}
// !!! IS OFFSET ALREADY CONSIDERED?
// if (tz) {
// df->setTimeZone(*tz);
// }


// Use ISO string form of the date/time.
Expand All @@ -197,9 +203,26 @@ const string TestDatetimeFmt(json_object *json_in) {

string input_date_string = json_object_get_string(input_string_obj);

// SimpleDateFormat can't parse options or timezone offset
// First, remove options starting with "["
std:size_t pos = input_date_string.find("[");
if (pos >= 0) {
input_date_string = input_date_string.substr(0, pos);
}
// Now remove the explicit offset
pos = input_date_string.find("+");
if (pos >= 0) {
input_date_string = input_date_string.substr(0, pos);
}
pos = input_date_string.rfind("-");
if (pos >= 10) {
// DOn't clip in the date fields
input_date_string = input_date_string.substr(0, pos);
}
UnicodeString date_ustring(input_date_string.c_str());

SimpleDateFormat iso_date_fmt(u"y-M-d'T'h:m:s.SSSZ", und_locale, status);
// TODO: handles the offset +/-
SimpleDateFormat iso_date_fmt(u"y-M-d'T'h:m:s", und_locale, status);
if (U_FAILURE(status)) {
string error_name = u_errorName(status);
string error_message =
Expand All @@ -215,6 +238,7 @@ const string TestDatetimeFmt(json_object *json_in) {
}

// Get date from the parser if possible.

test_date_time = iso_date_fmt.parse(date_ustring, status);

if (U_FAILURE(status)) {
Expand Down
21 changes: 20 additions & 1 deletion executors/node/datetime_fmt.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,31 @@ module.exports = {
}
let return_json = {'label': label};

let timezone;
try {
timezone = test_options['time_zone'];
} catch {
timezone = options['timeZone'] = 'UTC';
}
// Get the date from input milliseconds.
// Prefer milliseconds
let iso_date;
let test_date;

// Parse the input string as a date.
if (json['input_string']) {
test_date = new Date(json['input_string']);
iso_date = json['input_string'];
// Remove anything starting with "["
let option_start = iso_date.indexOf('[');
let test_date_string;
if (option_start >= 0) {
// TODO: !! Get the timezone and calendar from the iso_date string.
test_date_string = iso_date.substring(0, option_start);
} else {
test_date_string = iso_date;
}
console.log('test_date_string %s', test_date_string);
test_date = new Date(test_date_string);
}

try {
Expand Down Expand Up @@ -59,6 +77,7 @@ module.exports = {

try {
const formatted_dt = dt_formatter.format(test_date);
return_json['actual_options'] = test_options;
return_json['result'] = formatted_dt;
} catch (error) {
return_json['unsupported'] = ': ' + error.message;
Expand Down
2 changes: 1 addition & 1 deletion executors/node/executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ rl.on('line', function(line) {

const jsonOut = JSON.stringify(outputLine);

if ('error' in outputLine) {
if ('error' in outputLine && !('unsupported' in outputLine)) {
// To get the attention of the driver
console.log("#!! ERROR in NODE call: test_type: " + test_type + ", " + JSON.stringify(outputLine));
}
Expand Down
Loading

0 comments on commit 1bb7e44

Please sign in to comment.