fix(legacy): now macro should use station timezone (#3143)

### Description

The `now{}` macro for criteria filtering used UTC rather than station
timezone leading to confusing results.

**This is a new feature**:

No

**I have updated the documentation to reflect these changes**:

Yes

### Testing Notes

**What I did:**

I named a track the current date and hour, and used the date macro to
try to select it. It failed, I then implemented this fix and saw that
the track was selected.

**How you can replicate my testing:**

See what I did.

### **Links**

None
This commit is contained in:
dakriy 2025-03-18 06:12:01 -07:00 committed by GitHub
parent e861a1a491
commit c5548632e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 4 deletions

View File

@ -37,7 +37,7 @@ Smart blocks are automatically filled with media files from the LibreTime librar
To create a smart block, click the **Smartblocks** button on the left sidebar, and select **New** from the toolbar. Like a playlist, smart blocks can have a title and description, which you can edit. This helps you find relevant smart blocks in searches.
Fill out the smart block's **Name**, **Search Criteria**, and **Limit to** sections. The search criteria can be any one of LibreTime's metadata categories, such as **Title**, **Creator** or **Genre**. The modifier depends on whether the metadata in question contains letters or numbers. For example, **Title** has modifiers including _contains_ and _starts with_, whereas the modifiers for **BPM** include _is greater than_ and _is in the range_.
To filter tracks using today's date information, use the `now{}` macro. Format characters are listed in the [php documentation](https://www.php.net/manual/en/datetime.format.php). For example, to filter to tracks with a **Title** that ends in `Instrumental Jan 2024` where `Jan 2024` is the current month and year, add a criteria for **Title** with a modifier of **ends with** and a value of `Instrumental now{M Y}`.
To filter tracks using today's date information, use the `now{}` macro. Format characters are listed in the [php documentation](https://www.php.net/manual/en/datetime.format.php). For example, to filter to tracks with a **Title** that ends in `Instrumental Jan 2024` where `Jan 2024` is the current month and year, add a criteria for **Title** with a modifier of **ends with** and a value of `Instrumental now{M Y}`. The macro uses the configured station timezone to resolve dates and times.
If you have a large number of files which meet the criteria that you specify, you may wish to limit the duration of the smart block using the **Limit to** field, so that it fits within the show you have in mind. Select **hours**, **minutes** or **items** from the drop-down menu, and click the **Generate** button again, if it's a static smart block. Then click the **Save** button.

View File

@ -1480,15 +1480,17 @@ SQL;
return $storedCrit;
}
private function resolveDate($value)
private function resolveDate($value, $timeZone)
{
if (!is_string($value)) {
return $value;
}
$dt = new DateTime('now', new DateTimeZone($timeZone));
return preg_replace_callback(
'/now{(.*?)}/',
fn ($matches) => date($matches[1]),
fn ($matches) => $dt->format($matches[1]),
$value
);
}
@ -1502,6 +1504,7 @@ SQL;
$qry->useFkOwnerQuery('subj', 'left join');
$allCriteria = BlockCriteria::criteriaMap();
$timeZone = Application_Model_Preference::GetDefaultTimezone();
// Logging::info($storedCrit);
if (isset($storedCrit['crit'])) {
@ -1560,7 +1563,7 @@ SQL;
$spCriteriaExtra = $criteria['extra'];
}
$spCriteriaValue = $this->resolveDate($spCriteriaValue);
$spCriteriaValue = $this->resolveDate($spCriteriaValue, $timeZone);
if ($spCriteriaModifier == CriteriaModifier::STARTS_WITH) {
$spCriteriaValue = "{$spCriteriaValue}%";