Skip to content

Commit

Permalink
%lld => %"PRId64"
Browse files Browse the repository at this point in the history
use the right format printf specifier, long long or just long
fix t/asctime64.t.c, use the result of asctime_r_ret2
add -DHAS_TM_TM_GMTOFF -DHAS_TM_TM_ZONE to linux, using _DEFAULT_SOURCE
tap: cast year to year_t (int or long)
time64.h: dont want to include inttypes.h, so hack our own PRId64 define
  • Loading branch information
Reini Urban committed May 29, 2016
1 parent 2562f52 commit 1707b30
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 28 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ ifeq ($(UNAME_S),Linux)
# Under Linux/glibc you will need flag _BSD_SOURCE for names tm_gmtoff and tm_zone
# (instead of __tm_gmtoff and __tm_zone) in struct tm,
# and _POSIX_SOURCE (there are alternatives) for tzset().
CFLAGS += -D_BSD_SOURCE -D_POSIX_SOURCE
# _BSD_SOURCE is deprecated, use _DEFAULT_SOURCE instead
CFLAGS += -D_DEFAULT_SOURCE -D_POSIX_SOURCE -DHAS_TM_TM_GMTOFF -DHAS_TM_TM_ZONE
endif
ifneq ($(DEBUG),)
CFLAGS += -DTIME_64_DEBUG
Expand Down
2 changes: 1 addition & 1 deletion t/asctime64.t.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int main(void) {
date.tm_hour = 5;
asctime_r_ret2 = asctime64_r( &date, asctime_r_buf2 );
is_str( asctime_r_buf2, "Mon May 4 05:02:42 234567\n", "asctime64_r() again" );
is_str( asctime_r_ret1, "Mon May 4 03:02:42 234567\n", " return not static" );
is_str( asctime_r_ret2, "Mon May 4 05:02:42 234567\n", " return not static" );

/* Upon successful completion, asctime() shall return a pointer to the string.
If the function is unsuccessful, it shall return NULL. (ISO)
Expand Down
5 changes: 3 additions & 2 deletions t/gmtime64.t.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdio.h>
#include <inttypes.h>
#include "time64.h"
#include "t/tap.h"

Expand Down Expand Up @@ -29,12 +30,12 @@ int main(void) {
for( time = -6000000000LL; time < 6000000000LL; time += 712359 ) {
localtime64_r(&time, &want_date);
have_date = localtime64(&time);
sprintf(name, "localtime64(%lld)", time);
sprintf(name, "localtime64(%"PRId64")", time);
tm_is( have_date, &want_date, name );

gmtime64_r(&time, &want_date);
have_date = gmtime64(&time);
sprintf(name, "gmtime64(%lld)", time);
sprintf(name, "gmtime64(%"PRId64")", time);
tm_is( have_date, &want_date, name );
}

Expand Down
2 changes: 1 addition & 1 deletion t/gmtime_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ int main(void)

while(d.tm_year < 800) {
gmtime64_r(&time, &d);
printf("%lld %d %d %d %d %d %lld %d %d %d\n",
printf("%"PRId64" %d %d %d %d %d %"PRId64" %d %d %d\n",
time,
d.tm_sec,
d.tm_min,
Expand Down
2 changes: 1 addition & 1 deletion t/localtime_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ int main(void)

while(d.tm_year < 800) {
localtime64_r(&time, &d);
printf("%lld %d %d %d %d %d %lld %d %d %d\n",
printf("%"PRId64" %d %d %d %d %d %"PRId64" %d %d %d\n",
time,
d.tm_sec,
d.tm_min,
Expand Down
11 changes: 8 additions & 3 deletions t/mktime64.t.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ int mktime64_ok(Time64_T time) {
struct TM date;

localtime64_r(&time, &date);
return is_Int64( mktime64(&date), time, "mktime64(%lld)", time );
return is_Int64( mktime64(&date), time, "mktime64(%"PRId64")", time );
}

int main(void) {
Expand All @@ -33,7 +33,6 @@ int main(void) {
localtime64_r(&time, &date);
is_Int64( mktime64(&date), timelocal64(&date), "timelocal64 alias" );


/* Test that mktime64 accepts and corrects out of bound dates */
/* The original values of the tm_wday and tm_yday components of the
* structure are ignored, and the original values of the other components
Expand All @@ -50,6 +49,12 @@ int main(void) {
date.tm_mday = 37;
date.tm_wday = 9; /* deliberately wrong week day */
date.tm_yday = 487; /* and wrong year day */
#ifdef HAS_TM_TM_GMTOFF
/*date.tm_gmtoff = 0;*/
#endif
#ifdef HAS_TM_TM_ZONE
/*date.tm_zone = "UTC";*/
#endif

/* Upon successful completion, the values of the tm_wday and tm_yday
* components of the structure shall be set appropriately, and the other
Expand All @@ -59,7 +64,7 @@ int main(void) {
* tm_year are determined.
* http://www.opengroup.org/onlinepubs/009695399/functions/mktime.html
*/
is_Int64( mktime64(&date), time, "mktime64(%lld)", time );
is_Int64( mktime64(&date), time, "mktime64(%"PRId64")", time );
is_int( date.tm_mon, 2, "tm_mon corrected" );
is_int( date.tm_mday, 9, "tm_mday corrected" );
is_int( date.tm_yday, 67,"tm_yday corrected" );
Expand Down
6 changes: 3 additions & 3 deletions t/safe_year.t.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
static void year_to_TM(const Year year, struct TM *date) {
Time64_T time;

date->tm_year = year - 1900;
date->tm_year = (year_t)(year - 1900);
date->tm_mon = 0;
date->tm_mday = 1;
date->tm_hour = 0;
Expand All @@ -24,7 +24,7 @@ static void year_to_tm(const Year year, struct tm *ret) {
Time64_T time;
struct TM date;

date.tm_year = year - 1900;
date.tm_year = (year_t)(year - 1900);
date.tm_mon = 0;
date.tm_mday = 1;
date.tm_hour = 0;
Expand Down Expand Up @@ -60,7 +60,7 @@ static void test_safe_year(Year orig_year) {
is_Int64( (Year)orig_tm.tm_year, (Year)(orig_year - 1900), "year_to_tm(orig)" );
is_int( safe_tm.tm_year, year - 1900, "year_to_TM(safe)" );

ok(1, "orig_year: %lld, safe_year: %d", orig_year, year);
ok(1, "orig_year: %"PRId64", safe_year: %d", orig_year, year);
is_int( safe_tm.tm_wday, orig_tm.tm_wday, " tm_wday" );
is_int( IS_LEAP( year - 1900 ), IS_LEAP( orig_year - 1900 ), " ISLEAP()" );

Expand Down
8 changes: 4 additions & 4 deletions t/tap.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <inttypes.h>
#include "time64.h"

#include "tap.h"
Expand Down Expand Up @@ -105,8 +106,8 @@ int is_Int64(const Int64 have, const Int64 want, const char *name, ...) {
do_test( test, name, args );

if( !test ) {
diag("have: %lld", have);
diag("want: %lld", want);
diag("have: %"PRId64, have);
diag("want: %"PRId64, want);
}

va_end(args);
Expand Down Expand Up @@ -153,11 +154,10 @@ int tm_is(const struct TM *have, const struct TM *want, const char *name)
return pass;
}


struct TM make_tm( int sec, int min, int hour, int day, int mon, Year year ) {
struct TM date;

date.tm_year = year - 1900;
date.tm_year = (year_t)(year - 1900);
date.tm_mon = mon - 1;
date.tm_mday = day;
date.tm_hour = hour;
Expand Down
6 changes: 6 additions & 0 deletions t/tap.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#include <stdarg.h>
#include "time64.h"

#ifdef USE_TM64
#define year_t Year
#else
#define year_t int
#endif

int diag(const char*, ...);
void skip_all(const char *);
int do_test(const int, const char *, va_list);
Expand Down
7 changes: 4 additions & 3 deletions t/year_limit.t.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "time64.h"
#include <stdio.h>
#include <inttypes.h>
#include "time64.h"
#include "t/tap.h"

int main(void)
Expand All @@ -17,12 +18,12 @@ int main(void)
int expected_mday = 19;
#endif

printf("# time: %lld\n", time);
printf("# time: %"PRId64"\n", time);
gmtime64_r(&time, &gtime);
printf("# sizeof time_t: %ld\n", sizeof(time_t));
printf("# sizeof long long: %ld\n", sizeof(Time64_T));
printf("# sizeof tm.tm_year: %ld\n", sizeof(gtime.tm_year));
printf("# %04lld.%02d.%02d %02d:%02d:%02d\n",
printf("# %04"PRId64".%02d.%02d %02d:%02d:%02d\n",
(Year)(gtime.tm_year + 1900),
gtime.tm_mon + 1,
gtime.tm_mday,
Expand Down
16 changes: 8 additions & 8 deletions time64.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ Time64_T timegm64(const struct TM *date) {
orig_year -= cycles * 400;
days += cycles * days_in_gregorian_cycle;
}
TIME64_TRACE3("# timegm/ cycles: %lld, days: %lld, orig_year: %lld\n", cycles, days, orig_year);
TIME64_TRACE3("# timegm/ cycles: %"PRId64", days: %"PRId64", orig_year: %"PRId64"\n", cycles, days, orig_year);

if( orig_year > 70 ) {
year = 70;
Expand Down Expand Up @@ -332,7 +332,7 @@ static Year cycle_offset(Year year)
exceptions = year_diff / 100;
exceptions -= year_diff / 400;

TIME64_TRACE3("# year: %lld, exceptions: %lld, year_diff: %lld\n",
TIME64_TRACE3("# year: %"PRId64", exceptions: %"PRId64", year_diff: %"PRId64"\n",
year, exceptions, year_diff);

return exceptions * 16;
Expand Down Expand Up @@ -391,7 +391,7 @@ static int safe_year(const Year year)
else
assert(0);

TIME64_TRACE3("# year: %lld, year_cycle: %lld, safe_year: %d\n",
TIME64_TRACE3("# year: %"PRId64", year_cycle: %"PRId64", safe_year: %d\n",
year, year_cycle, safe_year);

assert(safe_year <= MAX_SAFE_YEAR && safe_year >= MIN_SAFE_YEAR);
Expand Down Expand Up @@ -550,8 +550,8 @@ Time64_T mktime64(struct TM *input_date) {
/* Have to make the year safe in date else it won't fit in safe_date */
date = *input_date;
date.tm_year = safe_year(year) - 1900;
copy_TM64_to_tm(&date, &safe_date);

copy_TM64_to_tm(&date, &safe_date);
time = (Time64_T)mktime(&safe_date);

/* Correct the user's possibly out of bound input date */
Expand Down Expand Up @@ -714,7 +714,7 @@ struct TM *localtime64_r (const Time64_T *time, struct TM *local_tm)
if( SHOULD_USE_SYSTEM_LOCALTIME(*time) ) {
safe_time = (time_t)*time;

TIME64_TRACE1("Using system localtime for %lld\n", *time);
TIME64_TRACE1("Using system localtime for %"PRId64"\n", *time);

LOCALTIME_R(&safe_time, &safe_date);

Expand All @@ -725,7 +725,7 @@ struct TM *localtime64_r (const Time64_T *time, struct TM *local_tm)
}

if( gmtime64_r(time, &gm_tm) == NULL ) {
TIME64_TRACE1("gmtime64_r returned null for %lld\n", *time);
TIME64_TRACE1("gmtime64_r returned null for %"PRId64"\n", *time);
return NULL;
}

Expand All @@ -735,7 +735,7 @@ struct TM *localtime64_r (const Time64_T *time, struct TM *local_tm)
gm_tm.tm_year < (1970 - 1900)
)
{
TIME64_TRACE1("Mapping tm_year %lld to safe_year\n", (Year)gm_tm.tm_year);
TIME64_TRACE1("Mapping tm_year %"PRId64" to safe_year\n", (Year)gm_tm.tm_year);
gm_tm.tm_year = safe_year((Year)(gm_tm.tm_year + 1900)) - 1900;
}

Expand All @@ -754,7 +754,7 @@ struct TM *localtime64_r (const Time64_T *time, struct TM *local_tm)
# endif

if( local_tm->tm_year != orig_year ) {
TIME64_TRACE2("tm_year overflow: tm_year %lld, orig_year %lld\n",
TIME64_TRACE2("tm_year overflow: tm_year %"PRId64", orig_year %"PRId64"\n",
(Year)local_tm->tm_year, (Year)orig_year);

#ifdef EOVERFLOW
Expand Down
9 changes: 8 additions & 1 deletion time64.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ typedef INT_64_T Int64;
typedef Int64 Time64_T;
typedef Int64 Year;

#ifndef PRId64
# if (__WORDSIZE == 64) && !defined(__APPLE__)
# define PRId64 "ld"
# else
# define PRId64 "lld"
# endif
#endif

/* A copy of the tm struct but with a 64 bit year */
struct TM64 {
Expand Down Expand Up @@ -76,7 +83,7 @@ Time64_T timelocal64 (struct TM *);

/* Use a different asctime format depending on how big the year is */
#ifdef USE_TM64
#define TM64_ASCTIME_FORMAT "%.3s %.3s%3d %.2d:%.2d:%.2d %lld\n"
#define TM64_ASCTIME_FORMAT "%.3s %.3s%3d %.2d:%.2d:%.2d %"PRId64"\n"
#else
#define TM64_ASCTIME_FORMAT "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n"
#endif
Expand Down

0 comments on commit 1707b30

Please sign in to comment.