1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-23 08:44:54 +00:00
Arsse/lib/Misc/Date.php
J. King 27caf147df Changes to Date helper class
- Changed 'transform' method to use ValueInfo throughout. This fixes a number of obscure bugs
- Changed the 'add' and 'sub' methods to default to "now" rather than null. This means null passes through rather than being interpreted as the current time, to be consistent with other date tools
- Also changed the 'add' and 'sub' methods so that they operate correctly with invalid date strings
- Added tests for the class; improves #66
- Modified TTRSS tests because the "iso8601" format string in ValueInfo is different from Date's older format
2018-01-02 16:53:38 -05:00

40 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(string $interval, $date = "now") {
return self::modify("add", $interval, $date);
}
public static function sub(string $interval, $date = "now") {
return self::modify("sub", $interval, $date);
}
protected static function modify(string $func, string $interval, $date) {
$date = self::normalize($date);
return $date ? $date->$func(new \DateInterval($interval)) : null;
}
}