Skip to content

Refactor date/time handling to use ImpressCMS IPF standards - #9

Draft
fiammybe with Copilot wants to merge 2 commits into
masterfrom
copilot/refactor-date-time-handling
Draft

Refactor date/time handling to use ImpressCMS IPF standards#9
fiammybe with Copilot wants to merge 2 commits into
masterfrom
copilot/refactor-date-time-handling

Conversation

Copilot AI commented Mar 19, 2026

Copy link
Copy Markdown

PHP's native date() is used throughout the module for formatting, bypassing ImpressCMS timezone support, locale-aware calendar handling, and numeral system conversion. This replaces those calls with formatTimestamp() and icms_conv_nr2local() across all affected files.

Changes

  • src/event.php / src/calendar.php: Replace date('Y', ...) / date('F', ...) with formatTimestamp() for year/month extraction used in sorted event grouping
  • src/class/EventHandler.php: Refactor prepareEventForDisplay — replace all date() calls with formatTimestamp() for comparisons and formatting; wrap day/year numerics with icms_conv_nr2local() for non-Arabic numeral locale support
  • src/blocks/events_upcoming.php: Replace date($dateformat, $date) with formatTimestamp($date, 'custom', $dateformat) in both events_upcoming_show and events_upcoming_menu_show
// Before
$year  = date('Y', $eventObj->getVar('date', 'e'));
$month = date('F', $eventObj->getVar('date', 'e'));
$date  = date($dateformat, $date);
$event['formatted_date'] = date('j', $start_date) . '-' . date('j', $end_date) . ' ' . date('F', $start_date);

// After
$year  = formatTimestamp($eventObj->getVar('date', 'e'), 'Y');
$month = formatTimestamp($eventObj->getVar('date', 'e'), 'F');
$date  = formatTimestamp($date, 'custom', $dateformat);
$event['formatted_date'] = icms_conv_nr2local(formatTimestamp($start_date, 'j'))
    . '-' . icms_conv_nr2local(formatTimestamp($end_date, 'j'))
    . ' ' . formatTimestamp($start_date, 'F');
Original prompt

Refactor the date and time handling in the fiammybe/events repository to use ImpressCMS IPF (Item Persistence Framework) standards.

Goals:

  1. Replace PHP date() with formatTimestamp():

    • In src/event.php, src/calendar.php, src/class/EventHandler.php, and src/blocks/events_upcoming.php, replace calls to date() with formatTimestamp() to ensure localization and timezone support.
    • Example: date('Y', $timestamp) should become formatTimestamp($timestamp, 'Y').
    • Example: date('F', $timestamp) should become formatTimestamp($timestamp, 'F').
  2. Refactor prepareEventForDisplay in src/class/EventHandler.php:

    • Update the logic that calculates formatted_date to use formatTimestamp() for all date components (day, month, year).
    • Ensure that month names are localized via the core instead of using PHP's internal English-only strings.
  3. Update Block Handling in src/blocks/events_upcoming.php:

    • Use formatTimestamp($timestamp, 'custom', $dateformat) instead of date($dateformat, $timestamp).
  4. Incorporate Localization:

    • Use icms_conv_nr2local() where appropriate for numeric outputs like day numbers or years to support non-Arabic numeral locales.

Technical Context:

  • Repository: fiammybe/events
  • Base Branch: master
  • New Branch: datetime-ipf

This refactor will simplify the code, improve internationalization (i18n), and make the module more robust across different ImpressCMS installations.

The following is the prior conversation context from the user's chat exploration (may be truncated):

User: Identify date and time handling in this module that is not using ImpressCMS IPF standards and can be refactored to use those standards, simplyfying the code and making it less error-prone

Assistant: In the fiammybe/events repository, date and time handling frequently uses standard PHP functions like date(), time(), and manual string formatting instead of adhering to ImpressCMS IPF (Item Persistence Framework) standards.

Refactoring these to use ImpressCMS core functions like formatTimestamp() and icms_conv_nr2local() would improve localization (supporting different calendars and timezones), simplify the code, and make it more consistent with the rest of the platform.

Identified Areas for Refactoring

1. Use of date() instead of formatTimestamp()

Throughout the module, the PHP date() function is used with hardcoded format strings. In ImpressCMS, formatTimestamp() should be used to ensure that user-defined date formats and timezones are respected.

  • src/event.php (Lines 177-178):

    $year = date('Y', $eventObj->getVar('date', 'e'));
    $month = date('F', $eventObj->getVar('date', 'e'));

    Refactor to: Use formatTimestamp($timestamp, 'Y') and formatTimestamp($timestamp, 'F').

  • src/class/EventHandler.php (Lines 37-56):
    The prepareEventForDisplay method manually compares months and days using date('n', ...) and date('j', ...).
    Refactor to: Use formatTimestamp() with appropriate format characters (e.g., 'n' for month number, 'j' for day).

  • src/blocks/events_upcoming.php (Lines 109-111):

    $date = $event->getVar('date', 'e');
    $dateformat = icms_getConfig('date_format', 'events');
    $date = date($dateformat, $date);

    Refactor to: formatTimestamp($event->getVar('date', 'e'), 'custom', $dateformat).

2. Manual Date Logic for Display

In src/class/EventHandler.php, there is complex logic to format "1-3 January" vs "1 January - 3 February".

		// Format date. Check the month of each. Are they the same?
		if (date('n', $start_date) == date('n', $end_date))
		{
            if (date('d', $start_date) == date('d', $end_date))
            {
                $event['formatted_date'] = date('j F y', $start_date);
            }
            else
            {
			// If so, format using the start month once, eg. 1-3 January
			$event['formatted_date'] = date('j', $start_date) . '-' . date ('j', $end_date) . ' ' 
					. date('F', $start_date);
		    }
        }

Refactor to: While ImpressCMS doesn't have a direct "date range" formatter, using formatTimestamp for each component ensures that month names are localized (e.g., using _CAL_JANUARY definitions automatically via the core).

3. Localization and Number Conversion

The module does not appear to use icms_conv_nr2local() for numeric outputs in dates or lists.

  • Recommendation: When outputting day numbers or years, wrap them in icms_conv_nr2local() to support locales that use non-Arabic numerals (e.g., Persian/Jalali).

4. Database Queries and Current Time

In several places, time() is used directly in SQL queries or criteria.

  • **`src/blocks/events_upcomin...

This pull request was created from Copilot chat.


📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.

…mestamp, icms_conv_nr2local)

Co-authored-by: fiammybe <3736946+fiammybe@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor date and time handling to use ImpressCMS IPF standards Refactor date/time handling to use ImpressCMS IPF standards Mar 19, 2026
Copilot AI requested a review from fiammybe March 19, 2026 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants