Time Formatting Parsing in Scilab
Our first example demonstrates time formatting and parsing in Scilab. Here’s the full source code:
// Import required libraries
getd('.');
// Define a function to print values
function p(x)
disp(x)
endfunction
// Get current time
t = getdate();
// Format time according to ISO 8601 (similar to RFC3339)
p(msprintf('%04d-%02d-%02dT%02d:%02d:%02d+00:00', t(1), t(2), t(6), t(7), t(8), t(9)));
// Parse a time string
t1 = strtod('2012-11-01T22:08:41+00:00', 'iso8601');
p(t1);
// Custom time formatting
p(msprintf('%d:%02dPM', modulo(t(7), 12), t(8)));
p(msprintf('%s %s %2d %02d:%02d:%02d %04d', weekday(t), gettext(month(t)), t(6), t(7), t(8), t(9), t(1)));
p(msprintf('%04d-%02d-%02dT%02d:%02d:%02d.%03d+00:00', t(1), t(2), t(6), t(7), t(8), t(9), t(10)));
// Parse a custom time format
form = '%d %d PM';
t2 = msscanf('8 41 PM', form);
p(t2);
// Numeric representation using extracted components
mprintf('%04d-%02d-%02dT%02d:%02d:%02d-00:00\n', t(1), t(2), t(6), t(7), t(8), t(9));
// Parse will return an error on malformed input
try
t3 = msscanf('8:41PM', '%s %s %2d %02d:%02d:%02d %04d');
catch
p('Error parsing time string');
end
Scilab supports time formatting and parsing, although it uses different functions and methods compared to other languages. Here’s an explanation of the code:
We define a helper function
p
to print values, similar to the original example.We use
getdate()
to get the current time, which returns an array of time components.For formatting, we use
msprintf()
with appropriate format specifiers to create custom time strings.To parse time strings, we use
strtod()
with the ‘iso8601’ option for standard formats, ormsscanf()
for custom formats.Scilab doesn’t have built-in constants for standard time formats, so we manually construct the format strings.
For error handling, we use a try-catch block to catch parsing errors.
To run the program, save it as a .sce
file and execute it using the Scilab interpreter:
$ scilab -f time_formatting_parsing.sce
This will output the formatted and parsed time values, demonstrating Scilab’s capabilities for time manipulation.
Note that Scilab’s time handling functions are somewhat different from other languages, but they provide similar functionality for formatting and parsing time values.