Skip to content

Commit

Permalink
New formatting option to give rounded output (instead of truncated).
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill-Gray committed Jan 1, 2018
1 parent 83580d4 commit e8e7e8b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions date.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ long double DLL_FUNC get_time_from_stringl( long double initial_t2k,
#define FULL_CTIME_DAY_OF_WEEK_FIRST 0x40000
#define FULL_CTIME_DAY_OF_WEEK_LAST 0x80000
#define FULL_CTIME_NO_SPACES 0x100000
#define FULL_CTIME_ROUNDING 0x200000

/* Some convenience combos of the above flags: */
#define FULL_CTIME_YMD (FULL_CTIME_YEAR_FIRST | FULL_CTIME_MONTH_DAY)
Expand Down
20 changes: 13 additions & 7 deletions miscell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,23 @@ void DLL_FUNC full_ctimel( char *buff, long double t2k, const int format)
long units, i;
const int leading_zeroes = (format & FULL_CTIME_LEADING_ZEROES);
long year, int_t2k, day_of_week;
long double add_on = .05 / seconds_per_day;
long double add_on = 1.;
long double remains;

if( output_format == FULL_CTIME_FORMAT_SECONDS)
units = seconds_per_day;
else if( output_format == FULL_CTIME_FORMAT_HH_MM)
units = minutes_per_day;
else if( output_format == FULL_CTIME_FORMAT_HH)
units = hours_per_day;
else /* output in days */
units = 1;
for( i = precision; i; i--)
add_on /= 10.;
if( format & FULL_CTIME_ROUNDING)
add_on *= 0.5 / (double)units;
else
add_on *= 0.05 / seconds_per_day;
t2k += add_on;

if( output_format == FULL_CTIME_FORMAT_YEAR)
Expand Down Expand Up @@ -392,12 +404,6 @@ void DLL_FUNC full_ctimel( char *buff, long double t2k, const int format)
*buff++ = ' ';
}

if( output_format == FULL_CTIME_FORMAT_SECONDS)
units = seconds_per_day;
else if( output_format == FULL_CTIME_FORMAT_HH_MM)
units = minutes_per_day;
else /* output_format == FULL_CTIME_FORMAT_HH */
units = hours_per_day;
remains *= (double)units;
i = (long)remains;
if( i == units) /* keep things from rounding up incorrectly */
Expand Down

0 comments on commit e8e7e8b

Please sign in to comment.