Skip to content

Commit

Permalink
fix, properly convert Gregorian standard dates to ISO week dates
Browse files Browse the repository at this point in the history
  • Loading branch information
hroptatyr committed Apr 4, 2016
1 parent 3600939 commit e25822d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
3 changes: 3 additions & 0 deletions lib/date-core-strpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,9 @@ __strfd_card(

if (UNLIKELY(s.tai && d->flags.real_y_in_q)) {
y = d->q;
} else if (s.tai) {
/* oh we want ISO week dates */
y = dt_dconv(DT_YWD, that).ywd.y;
}
switch (s.abbr) {
case DT_SPMOD_LONG:
Expand Down
7 changes: 4 additions & 3 deletions lib/yd.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ __get_isowk_wd(unsigned int yd, dt_dow_t f01)
/* given the weekday the year starts with, F01, and the year-day YD
* return the iso week number */
static const int_fast8_t iso[] = {2, 1, 0, -1, -2, 4, 3, 2};
return (yd - iso[f01]) / GREG_DAYS_P_WEEK + 1;
return (GREG_DAYS_P_WEEK + yd - iso[f01]) / GREG_DAYS_P_WEEK;
}

DEFUN __attribute__((const, pure)) int
Expand All @@ -565,7 +565,7 @@ __yd_get_wcnt_abs(dt_yd_t d)
* the year started with */
int yd = d.d;
/* express yd as 7k + n relative to jan01 */
return (yd - 1) / 7 + 1;
return (GREG_DAYS_P_WEEK + yd - 1) / GREG_DAYS_P_WEEK;
}

DEFUN __attribute__((const, pure)) int
Expand All @@ -583,7 +583,8 @@ __yd_get_wcnt_iso(dt_yd_t d)
int wk;

/* express yd as 7k + n relative to jan01 */
if (UNLIKELY((wk = __get_isowk_wd(yd, (dt_dow_t)y01)) < 1)) {
wk = __get_isowk_wd(yd, (dt_dow_t)y01);
if (UNLIKELY(wk < 1)) {
/* get last years y01
* which is basically y01 - (365|366 % 7) */
if (LIKELY(!__leapp(--y))) {
Expand Down
7 changes: 4 additions & 3 deletions lib/ywd.c
Original file line number Diff line number Diff line change
Expand Up @@ -935,12 +935,13 @@ DEFUN void
__prep_strfd_ywd(struct strpd_s *tgt, dt_ywd_t d)
{
/* place ywd data of THIS into D for printing with FMT. */
if (d.c == 1 && d.w < __ywd_get_jan01_wday(d) && d.w) {
if (d.c == 1 && d.hang < 0 && d.w < __ywd_get_jan01_wday(d)) {
/* put gregorian year into y and real year into q */
tgt->y = d.y - 1;
tgt->q = d.y;
} else if (d.c >= 53) {
/* bit of service for the %Y printer */
} else if (d.c >= 53 && d.w >= __ywd_get_jan01_wday(d) +
1U/*coz 365 = 1 mod 7*/ + __leapp(d.y)) {
/* put gregorian year into y and real year into q */
tgt->y = d.y + 1;
tgt->q = d.y;
} else {
Expand Down

0 comments on commit e25822d

Please sign in to comment.