1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 21:22:40 +00:00
Arsse/lib/Misc/Date.php
J. King 5cd84c4ab4 Validate configuration parameters on import, and other changes
- Each parameter is checked for type and normalized
- Interval strings are converted to DateInterval objects
- Timeouts can be specified as interval strings
- Most intervals can be null to signify infinity
- Driver classes are checked that they implement the correct interface
- Short driver names may be used, and are used by default
- Helpful errors messages are printed in case of erroneous configuration

Exporting is currently broken; this will be fixed in an upcoming commit
2019-01-20 22:40:49 -05:00

41 lines
1.3 KiB
PHP

<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\Misc;
class Date {
public static function transform($date, string $outFormat = null, string $inFormat = null) {
$date = ValueInfo::normalize($date, ValueInfo::T_DATE, $inFormat);
if (!$date) {
return null;
}
$out = ValueInfo::normalize($date, ValueInfo::T_STRING, null, $outFormat);
if ($outFormat === "unix") {
$out = (int) $out;
} elseif ($outFormat === "float") {
$out = (float) $out;
}
return $out;
}
public static function normalize($date, string $inFormat = null) {
return ValueInfo::normalize($date, ValueInfo::T_DATE, $inFormat);
}
public static function add($interval, $date = "now") {
return self::modify("add", $interval, $date);
}
public static function sub($interval, $date = "now") {
return self::modify("sub", $interval, $date);
}
protected static function modify(string $func, $interval, $date) {
$date = self::normalize($date);
$interval = (!$interval instanceof \DateInterval) ? ValueInfo::normalize($interval, ValueInfo::T_INTERVAL) : $interval;
return $date ? $date->$func($interval) : null;
}
}