NAME

WeBWorK::Utils - useful utilities used by other WeBWorK modules.

FUNCTIONS

Filesystem interaction

createDirectory($dirName, $permission, $numgid)

Creates a directory with the given name, permission bits, and group ID.

@matches = listFilesRecusive($dir, $match_qr, $prune_qr, $match_full, $prune_full)

Traverses the directory tree rooted at $dir, returning a list of files, named pipes, and sockets matching the regular expression $match_qr. Directories matching the regular expression $prune_qr are not visited.

$match_full and $prune_full are boolean values that indicate whether $match_qr and $prune_qr, respectively, should be applied to the bare directory entry (false) or to the path to the directory entry relative to $dir.

@matches is a list of paths relative to $dir.

path_is_subdir($path, $dir, $allow_relative)

Ensures that $path refers to a location "inside" $dir. If $allow_relative is true and $path is not absoulte, it is assumed to be relative to $dir.

The method of checking is rather rudimentary at the moment. First, upreferences ("..") are disallowed, in $path, then it is checked to make sure that some prefix of it matches $dir.

If either of these checks fails, a false value is returned. Otherwise, a true value is returned.

Date/time processing

$dateTime = parseDateTime($string, $display_tz)

Parses $string as a datetime. If $display_tz is given, $string is assumed to be in that timezone. Otherwise, the server's timezone is used. The result, $dateTime, is an integer UNIX datetime (epoch) in the server's timezone.

$string = formatDateTime($dateTime, $display_tz, $format_string, $locale)

Formats the UNIX datetime $dateTime in the custom format provided by $format_string. If $format_string is not provided, the standard WeBWorK datetime format is used. $dateTime is assumed to be in the server's time zone. If $display_tz is given, the datetime is converted from the server's timezone to the timezone specified. If $format_string is a method of the $dt->locale instance, then format_cldr is used, and otherwise strftime is used. The available patterns for $format_string can be found in the documentation for the perl DateTime package under the heading of strftime Patterns. The available methods for the $dt->locale instance are documented at https://metacpan.org/pod/DateTime::Locale::FromData. $dateTime is assumed to be in the server's time zone. If $display_tz is given, the datetime is converted from the server's timezone to the timezone specified. If $locale is provided, the string returned will be in the format of that locale, which is useful for automatically translating things like days of the week and month names. If $locale is not provided, perl defaults to en_US.

$string = textDateTime($string_or_dateTime)

Accepts a UNIX datetime or a formatted string, returns a formatted string.

$dateTIme = intDateTime($string_or_dateTime)

Accepts a UNIX datetime or a formatted string, returns a UNIX datetime.

verify_timezone($display_tz)

If $display_tz is not a legal time zone then replace it with America/New_York and issue warning.

$timeinsec = timeToSec($time)

Makes a stab at converting a time (with a possible unit) into a number of seconds.

before($time, $now)

True if $now is less than $time. If $now is not specified, the value of time() is used.

after($time, $now)

True if $now is greater than $time. If $now is not specified, the value of time() is used.

between($start, $end, $now)

True if $now is greater than or equal to $start and less than or equal to $end. If $now is not specified, the value of time() is used.