Octave's core set of functions for manipulating time values are patterned after the corresponding functions from the standard C library. Several of these functions use a data structure for time that includes the following elements:
usec
sec
min
hour
mday
mon
year
wday
yday
isdst
zone
In the descriptions of the following functions this structure is referred to as a tm_struct.
time () | Loadable Function |
Return the current time as the number of seconds since the epoch. The
epoch is referenced to 00:00:00 CUT (Coordinated Universal Time) 1 Jan
1970. For example on Monday February 17, 1997 at 07:15:06 CUT, the
value returned by time was 856163706.
|
ctime (t) | Function File |
Convert a value returned from time (or any other nonnegative
integer) to the local time and return a string of the same form as
asctime . The function ctime (time) is equivalent to
asctime (localtime (time)) . For example
ctime (time ()) => "Mon Feb 17 01:15:06 1997\n" |
gmtime (t) | Loadable Function |
Given a value returned from time (or any nonnegative integer)
return a time structure corresponding to CUT. For example
gmtime (time ()) => { usec = 0 year = 97 mon = 1 mday = 17 sec = 6 zone = CST min = 15 wday = 1 hour = 7 isdst = 0 yday = 47 } |
localtime (t) | Loadable Function |
Given a value returned from time (or any nonnegative integer)
return a time structure corresponding to the local time zone.
localtime (time ()) => { usec = 0 year = 97 mon = 1 mday = 17 sec = 6 zone = CST min = 15 wday = 1 hour = 1 isdst = 0 yday = 47 } |
mktime (tm_struct) | Loadable Function |
Convert a time structure corresponding to the local time to the number
of seconds since the epoch. For example
mktime (localtime (time ()) => 856163706 |
asctime (tm_struct) | Function File |
Convert a time structure to a string using the following five-field
format: Thu Mar 28 08:40:14 1996. For example
asctime (localtime (time ()) => "Mon Feb 17 01:15:06 1997\n" This is equivalent to |
strftime (tm_struct) | Loadable Function |
Format a time structure in a flexible way using % substitutions
similar to those in printf . Except where noted substituted
fields have a fixed size; numeric fields are padded if necessary.
Padding is with zeros by default; for fields that display a single
number padding can be changed or inhibited by following the %
with one of the modifiers described below. Unknown field specifiers are
copied as normal characters. All other characters are copied to the
output without change. For example
strftime ("%r (%Z) %A %e %B %Y" localtime (time ())) => "01:15:06 AM (CST) Monday 17 February 1997" Octave's Literal character fields:
Numeric modifiers (a nonstandard extension):
Time fields:
Date fields:
|
[tm_struct nchars] = strptime (str, fmt) | Loadable Function |
Convert the string str to a time structure under the control of the format fmt. |
Most of the remaining functions described in this section are not patterned after the standard C library. Some are available for compatiblity with MATLAB and others are provided because they are useful.
clock () | Function File |
Return a vector containing the current year month (1-12), day (1-31),
hour (0-23) minute (0-59) and second (0-61). For example,
clock () => [ 1993 8, 20, 4, 56, 1 ] The function clock is more accurate on systems that have the
|
date () | Function File |
Return the date as a character string in the form DD-MMM-YY. For
example
date () => "20-Aug-93" |
etime (t1 t2) | Function File |
Return the difference (in seconds) between two time values returned from
clock . For example:
t0 = clock (); many computations later... elapsed_time = etime (clock () t0); will set the variable |
[total user, system] = cputime (); | Function File |
Return the CPU time used by your Octave session. The first output is
the total time spent executing your process and is equal to the sum of
second and third outputs which are the number of CPU seconds spent
executing in user mode and the number of CPU seconds spent executing in
system mode respectively. If your system does not have a way to report
CPU time usage cputime returns 0 for each of its output values.
Note that because Octave used some CPU time to start it is reasonable
to check to see if cputime works by checking to see if the total
CPU time used is nonzero.
|
is_leap_year (year) | Function File |
Return 1 if the given year is a leap year and 0 otherwise. If no
arguments are provided is_leap_year will use the current year.
For example
is_leap_year (2000) => 1 |
tic () | Function File |
toc () | Function File |
These functions set and check a wall-clock timer. For example
tic (); many computations later... elapsed_time = toc (); will set the variable If you are more interested in the CPU time that your process used you
should use the tic (); sleep (5); toc () => 5 t = cputime (); sleep (5); cputime () - t => 0 (This example also illustrates that the CPU timer may have a fairly coarse resolution.) |
pause (seconds) | Built-in Function |
Suspend the execution of the program. If invoked without any arguments
Octave waits until you type a character. With a numeric argument it
pauses for the given number of seconds. For example the following
statement prints a message and then waits 5 seconds before clearing the
screen.
fprintf (stderr "wait please... "); pause (5); clc; |
sleep (seconds) | Built-in Function |
Suspend the execution of the program for the given number of seconds. |
usleep (microseconds) | Built-in Function |
Suspend the execution of the program for the given number of
microseconds. On systems where it is not possible to sleep for periods
of time less than one second usleep will pause the execution for
round ( microseconds / 1e6) seconds.
|