| Deutsch English Français Italiano |
|
<vqsfc3$2g8c7$2@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!weretis.net!feeder9.news.weretis.net!news.quux.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: pozz <pozzugno@gmail.com>
Newsgroups: comp.arch.embedded
Subject: Re: 32 bits time_t and Y2038 issue
Date: Wed, 12 Mar 2025 18:13:39 +0100
Organization: A noiseless patient Spider
Lines: 142
Message-ID: <vqsfc3$2g8c7$2@dont-email.me>
References: <vqpkf9$1sbsa$1@dont-email.me> <vqpoi3$226ih$1@dont-email.me>
<vqqd1l$26qs8$1@dont-email.me> <vqrkd5$2hnm3$1@dont-email.me>
<vqsacn$2g8c7$1@dont-email.me> <vqsdcv$2mp5u$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 12 Mar 2025 18:13:40 +0100 (CET)
Injection-Info: dont-email.me; posting-host="41a992101a24e7f83d269d554023444b";
logging-data="2630023"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/2e2WnJUcKSOr9AOEieikE54T1Ix7ovNc="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:0yLJLjRqb8qgo5kU6oSinPuY5oo=
In-Reply-To: <vqsdcv$2mp5u$1@dont-email.me>
Content-Language: it
Bytes: 6655
Il 12/03/2025 17:39, David Brown ha scritto:
> On 12/03/2025 16:48, pozz wrote:
>> Il 12/03/2025 10:33, David Brown ha scritto:
>
>>> For all of this, the big question is /why/ you are doing it. What
>>> are you doing with your times? Where are you getting them? Are you
>>> actually doing this in a sensible way because they suit your
>>> application, or are you just using these types and structures because
>>> they are part of the standard C library - which is not good enough
>>> for your needs here?
>>
>> When the user wants to set the current date and time, I fill a struct
>> tm with user values. Next I call mktime() to calculate time_t that is
>> been incrementing every second.
>>
>> When I need to show the current date and time to the user, I call
>> localtime() to convert time_t in struct tm. And I have day of the week
>> too.
>>
>> Consider that mktime() and localtime() take into account timezone,
>> that is important for me. In Italy we have daylight savings time with
>> not so simple rules. Standard time functions work well with timezones.
>>
>>
>>> Maybe you are going about it all the wrong way. If you need to be
>>> able to display and set the current time and date, and to be able to
>>> conveniently measure time differences for alarms, repetitive tasks,
>>> etc., then you probably don't need any correlation between your
>>> monotonic seconds counter and your time/date tracker. All you need
>>> to do is add one second to each, every second. I don't know the
>>> details of your application (obviously), but often no conversion is
>>> needed either way.
>>
>> I'm talking about *wall* clock only. Internally I have a time_t
>> variable that is incremented every second. But I need to show it to
>> the user and I can't show the seconds from the epoch.
>>
>
> The sane way to do this - the way it has been done for decades on small
> embedded systems - is to track both a human-legible date/time structure
> (ignore standard struct tm - make your own) /and/ to track a monotonic
> seconds counter (or milliseconds counter, or minutes counter - whatever
> you need). Increment both of them every second. Both operations are
> very simple - far easier than any conversions.
If I got your point, adding one second to struct mytm isn't reduced to a
++ on one of its member. I should write something similar to this:
if (mytm.tm_sec < 59) {
mytm.tm_sec += 1;
} else {
mytm.tm_sec = 0;
if (mytm.tm_min < 59) {
mytm.tm_min += 1;
} else {
mytm.tm_min = 0;
if (mytm.tm_hour < 23) {
mytm.tm_hour += 1;
} else {
mytm.tm_hour = 0;
if (mytm.tm_mday < days_in_month(mytm.tm_mon, mytm.tm_year)) {
mytm.tm_mday += 1;
} else {
mytm.tm_mday = 1;
if (mytm.tm_mon < 12) {
mytm.tm_mon += 1;
} else {
mytm.tm_mon = 0;
mytm.tm_year += 1;
}
}
}
}
}
However taking into account dst is much more complex. The rule is the
last sunday of March and last sunday of October (if I'm not wrong).
All can be coded manually from the scratch, but there are standard
functions just to avoid reinventing the wheel.
Tomorrow I could install my device in another country in the world and
it could be easy to change the timezone with standard function.
> Adding or subtracting an hour on occasion is also simple.
Yes, but the problem is *when*. You need to know the rules and you need
to implement them. localtime() just works.
> If your system is connected to the internet, then occasionally pick up
> the current wall-clock time (and unix epoch, if you like) from a server,
> along with the time of the next daylight savings change.
What do you mean with "next daylight savings change"? I'm using NTP
(specifically SNTP from a public server) and I'm able to retrive the
current UTC time in seconds from Unix epoch.
I just take this value and overwrite my internal counter.
In other application, I retrive the current time from incoming SMS. In
this case I have a local broken down time.
> If it is not
> connected, then the user is going to have to make adjustments to the
> time and date occasionally anyway, as there is always drift
Drifts? By using a 32.768kHz quartz to generate a 1 Hz clock that
increases the internal counter avoid any drifts.
- they can
> do the daylight saving change at the same time as they change their
> analogue clocks, their cooker clock, and everything else that is not
> connected.
I think you can take into account dst even if the device is not connected.
I bet Windows is able to show the correct time (with dst changes) even
if the PC is not connected.
>>>>> Or you can get the sources for a modern version of newlib, and pull
>>>>> the routines from there.
>>>>
>>>> It's a very complex code. time functions are written for whatever
>>>> timezone is set at runtime (TZ env variable), so their complexity
>>>> are higher.
>>>>
>>>
>>> So find a simpler standard C library implementation. Try the
>>> avrlibc, for example.
>>>
>>> But I have no doubt at all that you can make all this yourself easily
>>> enough.
>>
>> I think timezone rules are not so simple to implement.
>>
>
> You don't need them. That makes them simple.