bugAVR C Runtime Library - Bugs: bug #44327, eu_dst() is broken


bug #44327: eu_dst() is broken

Submitted by:  None
Submitted on:  Fri 20 Feb 2015 09:27:48 PM UTC  
Category:  Library Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Header files
Status:  None Percent Complete:  0%
Assigned to:  None Originator Email:  -email is unavailable-
Open/Closed:  Open Release:  Any
Fixed Release:  None

Add a New Comment (Rich Markup)

You are not logged in

Please log in, so followups can be emailed to you.


Fri 20 Feb 2015 09:27:48 PM UTC, original submission:  

The function eu_dst(), defined in include/util/eu_dst.h, is intended to
implement the European daylight saving rules. There is an error at the
end of the function, where the comparison between the current day of the
month and the day of the last Sunday is reversed. The error is repeated
four times. This makes the function be almost always wrong in March and
October. There is also an off-by-one error earlier in the function:
whenever the last Sunday is the 25th, the function believes it's the

I noticed those errors while trying to benchmark my own implementation
of eu_dst() against the one provided by avr-libc. Then, rather than
fixing the broken function, I suggest replacing it with my

    int eu_dst(const time_t * timer, int32_t * z)
        uint32_t t = *timer;
        if ((uint8_t)(t >> 24) >= 194) t -= 3029443200U;
        t = (t + 655513200) / 604800 * 28;
        if ((uint16_t)(t % 1461) < 856) return 3600;
        else return 0;

This version is based on the fact that the European DST function is
almost periodic. Actually, it is as close to periodic as possible given
the constraint that the transitions are only allowed on Sundays at 01:00
UTC. Since this constraint is also periodic, this allows for a compact
arithmetic solution.

I tested my implementation for correctness on my PC against the system's
localtime(), for every full hour between the epoch (2000-01-01) and the
end of time (2136-02-07). I also tested it on an Arduino Uno
(ATmega328P) against a precomputed table of all the transition times,
both at the transition and the second right before.

In terms of size, this function compiles 52% smaller than the one
provided by eu_dst.h. If we account for the dependencies, it is 75%
smaller (204 vs. 826 bytes). It is also 3 times faster:
about 1250 — 1280 cycles versus 3300 – 4500.


Edgar Bonet.



(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

No files currently attached


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by the2masters
  • -email is unavailable- added by None (Submitted the item)



    Follows 1 latest change.

    Date Changed by Updated Field Previous Value => Replaced by
    2016-09-21 the2masters Carbon-Copy- => Added stefan@the2masters.de

    Back to the top

    Powered by Savane 3.5