Repository: SerenityOS/serenity
File: Meta/Lagom/Tools/CodeGenerators/LibTimeZone/GenerateTimeZoneData.cpp (C++)

Code before:
    DaylightSavingsOffset const* last_offset = nullptr;

    auto preferred_rule = [&](auto* current_offset, auto& new_offset) {
        if (!current_offset)
            return &new_offset;

        if (time >= new_offset.time_in_effect(new_offset.year_from))
            return &new_offset;

        return current_offset;
    };

    for (size_t index = 0; index < dst_rules.size(); ++index) {
        auto const& dst_rule = dst_rules[index];

        if (last_offset == nullptr)
            last_offset = &dst_rule;
        else if (dst_rule.time_in_effect(dst_rule.year_to) > last_offset->time_in_effect(last_offset->year_to))
            last_offset = &dst_rule;

        if (dst_rule.offset == 0)
            standard_offset = preferred_rule(standard_offset, dst_rule);
        else
            daylight_offset = preferred_rule(daylight_offset, dst_rule);
    }

    if (time >= last_offset->year_to)
        daylight_offset = standard_offset;

    return { standard_offset, daylight_offset };
}

static Offset get_active_dst_offset(TimeZoneOffset const& time_zone_offset, AK::UnixDateTime time)
{
    auto offsets = find_dst_offsets(time_zone_offset, time);
    if (offsets[0] == offsets[1])
        return { offsets[0]->offset, InDST::No };

    auto standard_time_in_effect = offsets[0]->time_in_effect(time);
    auto daylight_time_in_effect = offsets[1]->time_in_effect(time);

    if (daylight_time_in_effect < standard_time_in_effect) {
        if ((time < daylight_time_in_effect) || (time >= standard_time_in_effect))
            return { offsets[0]->offset, InDST::No };
    } else {
        if ((time >= standard_time_in_effect) && (time < daylight_time_in_effect))
            return { offsets[0]->offset, InDST::No };
    }

    return { offsets[1]->offset, InDST::Yes };
}

Review comment (suggestion):
Is there an official source you can quote for this change of behavior? When possible, we usually add a quote or a link to back the change.

Code after:
    auto preferred_rule = [&](auto* current_offset, auto& new_offset) {
        if (!current_offset)
            return &new_offset;

        if (time >= new_offset.time_in_effect(new_offset.year_from))
            return &new_offset;

        return current_offset;
    };

    for (size_t index = 0; index < dst_rules.size(); ++index) {
        auto const& dst_rule = dst_rules[index];

        if (last_offset == nullptr)
            last_offset = &dst_rule;
        else if (dst_rule.time_in_effect(dst_rule.year_to) > last_offset->time_in_effect(last_offset->year_to))
            last_offset = &dst_rule;

        if (dst_rule.offset == 0)
            standard_offset = preferred_rule(standard_offset, dst_rule);
        else
            daylight_offset = preferred_rule(daylight_offset, dst_rule);
    }

    // If the most recent rule is outdated, fall back to the last standard time rule.
    if (time >= last_offset->year_to)
        daylight_offset = standard_offset;

    return { standard_offset, daylight_offset };
}

static Offset get_active_dst_offset(TimeZoneOffset const& time_zone_offset, AK::UnixDateTime time)
{
    auto offsets = find_dst_offsets(time_zone_offset, time);
    if (offsets[0] == offsets[1])
        return { offsets[0]->offset, InDST::No };

    auto standard_time_in_effect = offsets[0]->time_in_effect(time);
    auto daylight_time_in_effect = offsets[1]->time_in_effect(time);

    if (daylight_time_in_effect < standard_time_in_effect) {
        if ((time < daylight_time_in_effect) || (time >= standard_time_in_effect))
            return { offsets[0]->offset, InDST::No };
    } else {
        if ((time >= standard_time_in_effect) && (time < daylight_time_in_effect))
            return { offsets[0]->offset, InDST::No };
    }

    return { offsets[1]->offset, InDST::Yes };
}