Switch from DateTime to DateTimeImmutable across codebase

This commit is contained in:
Alex Cabal 2024-04-13 14:05:14 -05:00
parent 92c647f2b1
commit e55fecaaa2
35 changed files with 102 additions and 99 deletions

View file

@ -2,7 +2,7 @@
<?
require_once('/standardebooks.org/web/lib/Core.php');
use Safe\DateTime;
use Safe\DateTimeImmutable;
use function Safe\getopt;
use function Safe\mkdir;
use function Safe\preg_replace;
@ -11,7 +11,7 @@ function SortByUpdatedDesc($a, $b){
return $b->Updated <=> $a->Updated;
}
function SaveFeed(Feed $feed, bool $force, ?string $label = null, ?string $labelSort = null, DateTime $now = null): void{
function SaveFeed(Feed $feed, bool $force, ?string $label = null, ?string $labelSort = null, DateTimeImmutable $now = null): void{
$updateAttrs = false;
if($force){
@ -31,7 +31,7 @@ function SaveFeed(Feed $feed, bool $force, ?string $label = null, ?string $label
}
}
function CreateOpdsCollectionFeed(string $name, string $url, string $description, array $collections, array $ebooks, DateTime $now, string $webRoot, OpdsNavigationFeed $opdsRoot, bool $force): void{
function CreateOpdsCollectionFeed(string $name, string $url, string $description, array $collections, array $ebooks, DateTimeImmutable $now, string $webRoot, OpdsNavigationFeed $opdsRoot, bool $force): void{
$collator = collator_create('en_US'); // Used for sorting letters with diacritics like in author names
usort($collections, function($a, $b) use($collator){ return $collator->compare($a['sortedname'], $b['sortedname']); });
@ -108,7 +108,7 @@ usort($allEbooks, 'SortByUpdatedDesc');
usort($newestEbooks, function($a, $b){ return $b->Created <=> $a->Created; });
$newestEbooks = array_slice($newestEbooks, 0, $ebooksPerNewestEbooksFeed);
$now = new DateTime();
$now = new DateTimeImmutable();
// Create OPDS feeds
$opdsRootEntries = [

View file

@ -16,7 +16,7 @@ use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Firefox\FirefoxDriver;
use Facebook\WebDriver\Firefox\FirefoxOptions;
use Safe\DateTime;
use Safe\DateTimeImmutable;
use function Safe\file_get_contents;
use function Safe\preg_replace;
use function Safe\putenv;
@ -44,7 +44,7 @@ $lastSeenTransactionId = null;
$firstTransactionId = null;
$transactionFilePath = '/tmp/last-fa-donation';
$transactionIds = [];
$now = new DateTime('now', new DateTimeZone('UTC'));
$now = new DateTimeImmutable();
$today = $now->format('n/j/Y');
$faItemsPerPage = 20; // How many items are on a full page of FA results?

View file

@ -9,7 +9,7 @@ use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Firefox\FirefoxDriver;
use Facebook\WebDriver\Firefox\FirefoxOptions;
use Safe\DateTime;
use Safe\DateTimeImmutable;
use function Safe\preg_match;
use function Safe\preg_replace;
use function Safe\putenv;
@ -31,8 +31,8 @@ $capabilities->setCapability(FirefoxOptions::CAPABILITY, $firefoxOptions);
$driver = null;
$log = new Log(DONATIONS_LOG_FILE_PATH);
$lastMonth = (new DateTime())->sub(new DateInterval('P45D')); // 45 days, a 15 day grace period before Patrons Circle members are dropped off
$lastYear = (new DateTime())->sub(new DateInterval('P1Y'));
$lastMonth = (new DateTimeImmutable())->sub(new DateInterval('P45D')); // 45 days, a 15 day grace period before Patrons Circle members are dropped off
$lastYear = (new DateTimeImmutable())->sub(new DateInterval('P1Y'));
$faUsername = get_cfg_var('se.secrets.fractured_atlas.username');
$faPassword = get_cfg_var('se.secrets.fractured_atlas.password');
@ -152,7 +152,7 @@ try{
$payment->User = null;
}
$payment->Created = DateTime::createFromFormat('n/j/Y', trim($detailsRow->findElement(WebDriverBy::xpath('//td[preceding-sibling::th[normalize-space(.) = "Created"]]'))->getText()));
$payment->Created = DateTimeImmutable::createFromFormat('n/j/Y', trim($detailsRow->findElement(WebDriverBy::xpath('//td[preceding-sibling::th[normalize-space(.) = "Created"]]'))->getText()));
$payment->IsRecurring = sizeof($headerRow->findElements(WebDriverBy::xpath('//td[contains(., "Recurring")]'))) > 0;
$payment->Amount = floatval(str_replace('$', '', trim($detailsRow->findElement(WebDriverBy::xpath('//td[preceding-sibling::th[normalize-space(.) = "Total Amount"]]'))->getText())));
$payment->Fee = floatval(str_replace('$', '', trim($detailsRow->findElement(WebDriverBy::xpath('//td[preceding-sibling::th[normalize-space(.) = "Fee"]]'))->getText())));

View file

@ -2,12 +2,14 @@
<?
require_once('/standardebooks.org/web/lib/Core.php');
use Safe\DateTimeImmutable;
// Get a list of payments that are within 1 year / 45 days of today, and deactivate Patrons Circle members
// who aren't in that list.
// We give a 15 day grace period to Patrons Circle members because sometimes FA can be delayed in charging.
$now = new DateTime();
$lastYear = new DateTime('-1 year');
$now = new DateTimeImmutable();
$lastYear = new DateTimeImmutable('-1 year');
$expiredPatrons = Db::Query('
SELECT * from Patrons
@ -38,7 +40,7 @@ if(sizeof($expiredPatrons) > 0){
preg_match_all('/<dc:date>(.+?)<\/dc:date>/iu', $metadata, $matches);
if(sizeof($matches) > 0){
$created = new DateTime($matches[1][0]);
$created = new DateTimeImmutable($matches[1][0]);
if($created >= $lastYear){
$ebooksThisYear++;
}