Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed missing offset from daylight savings time #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

twirth5
Copy link

@twirth5 twirth5 commented Apr 6, 2022

When the humhub calender is imported into outlook for windows, all entries that are defined after the 27th of march (change from standard time to daylight savings time) are off by +1 hour.

Our quick fix was, to hard code the timezone into the withEvents() method of VCalendar, so that the correct header is added to the exported ics.

When the humhub calender is imported into outlook for windows, all entries that are defined after the 27th of march (change from standard time to daylight savings time) are off by +1 hour.

Our quick fix was, to hard code the timezone into the withEvents() method of VCalendar, so that the correct header is added to the exported ics.
@ArchBlood
Copy link

@luke-, instead of this approach how about implementing this into the calendar module.

Current

    /**
     * @param CalendarEventIF[] $items
     * @return VCalendar
     */
    public static function withEvents($items, $tz = null)
    {
        $instance = (new static());
        $instance->addTimeZone($tz);

        if(!is_array($items)) {
            $items = [$items];
        }

        foreach ($items as $item)
        {
            if(is_array($item)) {
                $item = new CalendarEventIFWrapper(['options' => $item]);
            }
            $instance->addVEvent($item);
        }


        return  $instance;
    }

After

/**
 * @param CalendarEventIF[] $items
 * @return VCalendar
 */
public static function withEvents($items, $tz = null)
{
    $instance = (new static());
    
    // Retrieve default timezone from Yii::$app->settings
    $defaultTimeZone = Yii::$app->settings->get('defaultTimeZone');

    $instance->addTimeZone($defaultTimeZone);

    if (!is_array($items)) {
        $items = [$items];
    }

    foreach ($items as $item) {
        if (is_array($item)) {
            $item = new CalendarEventIFWrapper(['options' => $item]);
        }

        // Assuming $item has a start time property
        $itemStartTime = $item->getStartTime(); // Get the start time of the event

        // Create a DateTime object with the provided start time
        $startDateTime = new DateTime($itemStartTime);

        // Set the time zone to the default timezone retrieved from Yii::$app->settings
        $startDateTime->setTimezone(new DateTimeZone($defaultTimeZone));

        // Add one hour considering daylight saving time
        $startDateTime->modify('+1 hour');

        // Set the modified start time back to the event
        $item->setStartTime($startDateTime->format('Y-m-d H:i:s'));

        $instance->addVEvent($item);
    }

    return $instance;
}

We could also dig deeper for PHP 8.0 and use something like this;

public static function withEvents(array $items, $tz = null): VCalendar
{
    $instance = new static();

    $defaultTimeZone = Yii::$app->settings->get('defaultTimeZone');

    $instance->addTimeZone($defaultTimeZone);

    foreach ($items as $item) {
        $item = is_array($item) ? new CalendarEventIFWrapper(['options' => $item]) : $item;

        $itemStartTime = $item->getStartTime();

        $startDateTime = new DateTime($itemStartTime);

        $startDateTime->setTimezone(new DateTimeZone($defaultTimeZone))
                      ->modify('+1 hour');

        $item->setStartTime($startDateTime->format('Y-m-d H:i:s'));

        $instance->addVEvent($item);
    }

    return $instance;
}

@luke- luke- assigned yurabakhtin and unassigned yurabakhtin Jan 5, 2024
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.

3 participants