Time in Scilab

Our program demonstrates various time operations in Scilab. Here’s the full source code:

// We'll start by getting the current time.
now = getdate();
disp(now);

// You can build a date vector by providing the
// year, month, day, etc.
then = [2009, 11, 17, 20, 34, 58];
disp(then);

// You can extract the various components of the date
// value as expected.
disp(then(1)); // Year
disp(then(2)); // Month
disp(then(3)); // Day
disp(then(4)); // Hour
disp(then(5)); // Minute
disp(then(6)); // Second

// The day of the week is also available (1 = Monday, 7 = Sunday).
disp(weekday(then));

// These functions compare two dates, testing if the
// first occurs before, after, or at the same time
// as the second, respectively.
disp(datenum(then) < datenum(now));
disp(datenum(then) > datenum(now));
disp(datenum(then) == datenum(now));

// The diff function returns the difference between two dates in days.
diff = datenum(now) - datenum(then);
disp(diff);

// We can compute the length of the duration in
// various units.
disp(diff * 24); // Hours
disp(diff * 24 * 60); // Minutes
disp(diff * 24 * 60 * 60); // Seconds

// You can use datenum and datevec to advance a date by a given
// number of days, or with a negative value to move backwards.
disp(datevec(datenum(then) + diff));
disp(datevec(datenum(then) - diff));

To run the program, save it as time_example.sce and execute it in Scilab:

--> exec('time_example.sce', -1)

This will display the results of various time operations.

Note that Scilab’s date and time functions work differently from those in other languages. Scilab uses a vector representation for dates, where each element represents a component of the date (year, month, day, hour, minute, second). The datenum function converts a date vector to a serial date number, which can be used for date arithmetic.

Also, Scilab doesn’t have a built-in concept of time zones or locations, so all dates are assumed to be in the local time zone.

Next, we’ll look at other fundamental concepts in Scilab.