[tz] Adapting localtime.c

Carsten Larsen cs at innolan.dk
Mon Oct 10 16:00:06 UTC 2016


On Oct 9, 2016, at 20:03, Guy Harris wrote:
> On Oct 8, 2016, at 12:46 PM, Carsten Larsen <cs at innolan.dk> wrote:
>
>> AmigaOS uses an Epoch of January 1, 1978, 00:00:00 instead of January 1, 1970, 00:00:00 UTC. Time zone is not specified.
>>
>> The Epoch difference is easy to calculate.
>
> As "January 1, 1978, 00:00:00" is, as per your private response, January 1, 1978, 00:00:00 *local* time, then if you want to implement a UNIX-compatible version of time(), you need to convert the AmigaOS time to UTC, and then add 252460800 to the result.
>
> localtime(), in all implementations for UN*X, expects a UN*X-style time value to be passed to it; the tz implementation was originally an implementation for UN*X, so that's what it expects.
>
> So if you want to implement a time() that produces a result that can be passed to the tzcode implementation of localtime(), you need to implement a UN*X-compatible time().
>

The patch solves the time() returns *local time epoch*, *epoch offset*,
problem.


diff --git a/localtime.c b/localtime.c
index 6c00c45..313f846 100644
--- a/localtime.c
+++ b/localtime.c
@@ -2259,10 +2259,22 @@ posix2time(time_t t)
   /* Convert from the underlying system's time_t to the ersatz time_tz,
      which is called 'time_t' in this file.  */

+// Epoch is January 1, 1978 instead of January 1, 1970
+#define LOCAL_EPOCH_OFFSET 252460800
+
   time_t
   time(time_t *p)
   {
+  long zone;
     time_t r = sys_time(0);
+
+#ifdef LOCAL_EPOCH_OFFSET
+  if(lcl_is_set) {
+    zone = (daylight == 0 ? timezone : altzone);
+    time = (time_t)(tv.tv_secs + LOCAL_EPOCH_OFFSET + zone);
+  }
+#endif
+
     if (p)
       *p = r;
     return r;


It is probably not general enough to be included in the tzcode but maybe
it can inspire others.

Thank you for helping me out.

Kind regard
Carsten Larsen



More information about the tz mailing list