Replaced _systime with clock_gettime.

We want to add the capability to not only get the time but also set the time, but there's no "setter" for the "getter" time().

The first ones that come into mind are gettimeofday() and settimeofday(). However, they take a struct timezone argument that doesn't make sense - even the man pages says "The use of the timezone structure is obsolete; the tz argument should normally be specified as NULL." And POSIX says "Applications should use the clock_gettime() function instead of the obsolescent gettimeofday() function."

The ...timeofday() functions work with microseconds while the clock_...time() functions work with nanoseconds. Given that we expect our targets to support only 1/10 of seconds the microseconds look preferable at first sight. However, already microseconds require the cc65 data type 'long' so it's not such a relevant difference to nanoseconds. Additionally clock_getres() seems useful.

In order to avoid code duplication clock_gettime() takes over the role of the actual time getter from _systime(). So time() now calls clock_gettime() instead of _systime().

For some reason beyond my understanding _systime() was mentioned in time.h. _systime() worked exactly like e.g. _sysremove() and those _sys...() functions are all considered internal. The only reason I could see would be a performance gain of bypassing the time() wrapper. However, all known _systime() implementations internally called mktime(). And mktime() is implemented in C using an iterative algorithm so I really can't see what would be left to gain here. From that perspective I decided to just remove _systime().
This commit is contained in:
Oliver Schmidt
2018-08-15 15:59:11 +02:00
parent 55a07c1dcd
commit 842c151edd
15 changed files with 295 additions and 270 deletions

View File

@@ -52,9 +52,10 @@ typedef unsigned size_t;
typedef unsigned long time_t;
typedef unsigned long clock_t;
typedef unsigned char clockid_t;
/* Structure for broken down time */
struct tm {
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
@@ -66,6 +67,12 @@ struct tm {
int tm_isdst;
};
/* Structure for seconds and nanoseconds */
struct timespec {
time_t tv_sec;
long tv_nsec;
};
/* Timezone representation, default is UTC */
extern struct _timezone {
char daylight; /* True if daylight savings time active */
@@ -116,16 +123,10 @@ extern clock_t _clk_tck (void);
# define CLK_TCK _clk_tck()
# define CLOCKS_PER_SEC _clk_tck()
#endif
#define CLOCK_REALTIME 0
time_t _systime (void);
/* Similar to time(), but:
** - Is not ISO C
** - Does not take the additional pointer
** - Does not set errno when returning -1
*/
/* ISO C function prototypes */
char* __fastcall__ asctime (const struct tm* timep);
clock_t clock (void);
@@ -138,6 +139,13 @@ time_t __fastcall__ time (time_t* t);
/* POSIX function prototypes */
int __fastcall__ clock_getres (clockid_t clock_id, struct timespec *res);
int __fastcall__ clock_gettime (clockid_t clock_id, struct timespec *tp);
int __fastcall__ clock_settime (clockid_t clock_id, const struct timespec *tp);
/* End of time.h */
#endif