System Utilities

15.1  Time

15.1.1  Real Time and Date

(current-seconds) returns the current time in seconds. This time is always an exact integer based on a platform-specific starting date (with a platform-specific minimum and maximum value).

The value of (current-seconds) increases as time passes (increasing by 1 for each second that passes). The current time in seconds can be compared with a time returned by file-or-directory-modify-seconds (see section 11.3.3).

(seconds->date secs-n) takes secs-n, a platform-specific time in seconds (an exact integer) returned by current-seconds or file-or-directory-modify-seconds, and returns an instance of the date structure type, as described below. If secs-n is too small or large, the exn:fail exception is raised.

The value returned by current-seconds or file-or-directory-modify-seconds is not portable among platforms. Convert a time in seconds using seconds->date when portability is needed.

The date structure type has the following fields:

The date structure type is transparent to all inspectors (see section 4.5).

See also Chapter 14 in PLT MzLib: Libraries Manual for additional date utilities.

15.1.2  Machine Time

(current-milliseconds) returns the current ``time'' in fixnum milliseconds (possibly negative). This time is based on a platform-specific starting date or on the machine's startup time. Since the result is a fixnum, the value increases only over a limited (though reasonably long) time.

(current-inexact-milliseconds) returns the current ``time'' in positive milliseconds, not necessarily an integer. This time is based on a platform-specific starting date or on the machine's startup time, but it never decreases (until the machine is turned off).

(current-process-milliseconds) returns the amount of processor time in fixnum milliseconds that has been consumed by the MzScheme process on the underlying operating system. (Under Unix and Mac OS X, this includes both user and system time.) The precision of the result is platform-specific, and since the result is a fixnum, the value increases only over a limited (though reasonably long) time.

(current-gc-milliseconds) returns the amount of processor time in fixnum milliseconds that has been consumed by MzScheme's garbage collection so far. This time is a portion of the time reported by (current-process-milliseconds).

15.1.3  Timing Execution

The time-apply procedure collects timing information for a procedure application:

The reliability of the timing numbers depends on the platform; see section 15.1.2 for more information on time accounting. If multiple MzScheme threads are running, then the reported time may include work performed by other threads.

The time syntactic form reports timing information directly to the current output port:

15.2  Operating System Processes

(subprocess stdout-output-port stdin-input-port stderr-output-port command-path arg-string ···) creates a new process in the underlying operating system to execute command-path asynchronously. The command-path argument is a path to a program executable, and the arg-strings are command-line arguments for the program. Under Unix and Mac OS X, command-line arguments are passed as byte strings using the current locale's encoding (see section 1.2.3).

Under Windows, the first arg-string can be 'exact, which triggers a Windows-specific hack: the second arg-string is used exactly as the command-line for the subprocess, and no additional arg-strings can be supplied. Otherwise, a command-line string is constructed from command-path and arg-string so that a typical Windows console application can parse it back to an array of arguments.53 If 'exact is provided on a non-Windows platform, the exn:fail:contract exception is raised.

Unless it is #f, stdout-output-port is used for the launched process's standard output, stdin-input-port is used for the process's standard input, and stderr-output-port is used for the process's standard error. All provided ports must be file-stream ports. Any of the ports can be #f, in which case a system pipe is created and returned by subprocess. For each port that is provided, no pipe is created and the corresponding returned value is #f.

The subprocess procedure returns four values:

Important: All ports returned from subprocess must be explicitly closed with close-input-port and close-output-port.

The returned ports are file-stream ports (see section 11.1.6), and they are placed into the management of the current custodian (see section 9.2). The exn:fail exception is raised when a low-level error prevents the spawning of a process or the creation of operating system pipes for process communication.

A subprocess value can be used to obtain further information about the process:

MzLib provides procedures for executing shell commands (as opposed to directly executing a program); see Chapter 36 in PLT MzLib: Libraries Manual for details.

15.3  Windows Actions

(shell-execute verb-string target-string parameters-string dir-path show-mode-symbol) performs the action specified by verb-string on target-string in Windows. For example,

(shell-execute #f "http://www.plt-scheme.org" "" (current-directory) 'sw_shownormal)

opens the PLT Scheme home page in a browser window. For platforms other than Windows, the exn:fail:unsupported exception is raised.

The verb-string can be #f, in which case the operating system will use a default verb. Common verbs include "open", "edit", "find", "explore", and "print".

The target-string is the target for the action, usually a filename path. The file could be executable, or it could be a file with a recognized extension that can be handled by an installed application.

The parameters-string argument is passed on to the system to perform the action. For example, in the case of opening an executable, the parameters-string is used as the command line (after the executable name).

The dir-path is used as the current directory when performing the action.

The show-mode-symbol sets the display mode for a Window affected by the action. It must be one of the following symbols; the description of each symbol's meaning is taken from the Windows API documentation.

If the action fails, the exn:fail exception is raised. If the action succeeds, the result is #f. In future versions of MzScheme, the result may be a subprocess value (see section 15.2) if the operating system did returns a process handle (but if a subprocess value is returned, its process ID will be 0 instead of the real process ID).

15.4  Operating System Environment Variables

(getenv name-string) gets the value of an operating system environment variable. The name-string argument cannot contain a null character; if an environment variable named by name-string exists, its value is returned (as a string); otherwise, #f is returned.

(putenv name-string value-string) sets the value of an operating system environment variable. The name-string and value-string arguments are strings that cannot contain a null character; the environment variable named by name-string is set to value-string. The return value is #t if the assignment succeeds, #f otherwise.

15.5  Runtime Information

(system-type [mode]) returns information about the operating system, build mode, or machine for a running MzScheme. The mode argument must be either 'os (the default), 'link, or 'machine. In 'os mode, the possible symbol results are:

In 'link mode, the possible symbol results are:

(Future ports of MzScheme may expand the list of system and link symbol results.) In 'machine mode, then the result is a string, which contains further details about the current machine in a platform-specific format.

(system-language+country) returns a string to identify the current user's language and country. Under Unix and Mac OS X, the string is five characters: two lowercase ASCII letters for the language, an underscore, and two uppercase ASCII letters for the country. Under Windows, the string can be arbitrarily long, but the language and country are in English (all ASCII letters or spaces) separated by an underscore. Under Unix, the result is determined by checking the LC_ALL, LC_TYPE, and LANG environment variables, in that order (and the result is used if the environment variable's value starts with two lowercase ASCII letters, an underscore, and two uppercase ASCII letters, followed by either nothing or a period). Under Windows and Mac OS X, the result is determined by system calls.

(system-library-subpath [variant?]) returns a relative directory path string. This string can be used to build paths to system-specific files. For example, when MzScheme is running under Solaris on a Sparc architecture, the subpath is "sparc-solaris", while the subpath for Windows on an Intel architecture is "win32\\i386". The subpath also distinguishes among MzScheme variants (e.g., the ``3m'' variant with more precise garbage collection) by extending the ``normal'' variant path with a subdirectory. If variant? is #f, then the returned path is for the ``normal'' variant.

(version) returns an immutable string indicating the currently executing version of MzScheme.

(banner) returns an immutable string for MzScheme's start-up banner text (or the banner text for an embedding program, such as MrEd). The banner string ends with a newline.

(vector-set-performance-stats! mutable-vector [thread]) sets elements in mutable-vector to report current performance statistics. If thread is specified, a particular set of thread-specific statistics are reported, otherwise a different set of global statics are reported.

For global statistics, up to 8 elements are set in the vector, starting from the beginning. (In future versions of MzScheme, additional elements will be set.) If mutable-vector has n elements where n < 8, then the n elements are set to the first n performance-statistics values. The reported statistics values are as follows, in the order that they are set within mutable-vector:

For thread-specific statistics, up to 4 elements are set in the vector:


52 The value produced for the time-zone-offset field tends to be sensitive to the value of the "TZ" environment variable, especially on Unix platforms. Consult the system documentation (usually under tzset) for details.

53 For information on the Windows command-line conventions, search for ``command line parsing'' at http://msdn.microsoft.com/.