Skip to content

Commit

Permalink
Fix support for %Ec in strftime (#22057)
Browse files Browse the repository at this point in the history
Fixes: #22050
  • Loading branch information
sbc100 committed Jun 4, 2024
1 parent 56497e1 commit 223d61e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
58 changes: 29 additions & 29 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,6 @@ addToLibrary({
// expand format
var EXPANSION_RULES_1 = {
'%c': '%a %b %d %H:%M:%S %Y', // Replaced by the locale's appropriate date and time representation - e.g., Mon Aug 3 14:02:01 2013
'%D': '%m/%d/%y', // Equivalent to %m / %d / %y
'%F': '%Y-%m-%d', // Equivalent to %Y - %m - %d
'%h': '%b', // Equivalent to %b
Expand Down Expand Up @@ -787,6 +786,7 @@ addToLibrary({
'%Ow': '%w', // Replaced by the number of the weekday (Sunday=0) using the locale's alternative numeric symbols.
'%OW': '%W', // Replaced by the week number of the year (Monday as the first day of the week) using the locale's alternative numeric symbols.
'%Oy': '%y', // Replaced by the year (offset from %C ) using the locale's alternative numeric symbols.
'%c': '%a %b %d %H:%M:%S %Y', // Replaced by the locale's appropriate date and time representation - e.g., Mon Aug 3 14:02:01 2013
};
for (var rule in EXPANSION_RULES_1) {
pattern = pattern.replace(new RegExp(rule, 'g'), EXPANSION_RULES_1[rule]);
Expand Down Expand Up @@ -822,41 +822,41 @@ addToLibrary({
}
function getFirstWeekStartDate(janFourth) {
switch (janFourth.getDay()) {
case 0: // Sunday
return new Date(janFourth.getFullYear()-1, 11, 29);
case 1: // Monday
return janFourth;
case 2: // Tuesday
return new Date(janFourth.getFullYear(), 0, 3);
case 3: // Wednesday
return new Date(janFourth.getFullYear(), 0, 2);
case 4: // Thursday
return new Date(janFourth.getFullYear(), 0, 1);
case 5: // Friday
return new Date(janFourth.getFullYear()-1, 11, 31);
case 6: // Saturday
return new Date(janFourth.getFullYear()-1, 11, 30);
}
switch (janFourth.getDay()) {
case 0: // Sunday
return new Date(janFourth.getFullYear()-1, 11, 29);
case 1: // Monday
return janFourth;
case 2: // Tuesday
return new Date(janFourth.getFullYear(), 0, 3);
case 3: // Wednesday
return new Date(janFourth.getFullYear(), 0, 2);
case 4: // Thursday
return new Date(janFourth.getFullYear(), 0, 1);
case 5: // Friday
return new Date(janFourth.getFullYear()-1, 11, 31);
case 6: // Saturday
return new Date(janFourth.getFullYear()-1, 11, 30);
}
}
function getWeekBasedYear(date) {
var thisDate = addDays(new Date(date.tm_year+1900, 0, 1), date.tm_yday);
var thisDate = addDays(new Date(date.tm_year+1900, 0, 1), date.tm_yday);
var janFourthThisYear = new Date(thisDate.getFullYear(), 0, 4);
var janFourthNextYear = new Date(thisDate.getFullYear()+1, 0, 4);
var janFourthThisYear = new Date(thisDate.getFullYear(), 0, 4);
var janFourthNextYear = new Date(thisDate.getFullYear()+1, 0, 4);
var firstWeekStartThisYear = getFirstWeekStartDate(janFourthThisYear);
var firstWeekStartNextYear = getFirstWeekStartDate(janFourthNextYear);
var firstWeekStartThisYear = getFirstWeekStartDate(janFourthThisYear);
var firstWeekStartNextYear = getFirstWeekStartDate(janFourthNextYear);
if (compareByDay(firstWeekStartThisYear, thisDate) <= 0) {
// this date is after the start of the first week of this year
if (compareByDay(firstWeekStartNextYear, thisDate) <= 0) {
return thisDate.getFullYear()+1;
}
return thisDate.getFullYear();
if (compareByDay(firstWeekStartThisYear, thisDate) <= 0) {
// this date is after the start of the first week of this year
if (compareByDay(firstWeekStartNextYear, thisDate) <= 0) {
return thisDate.getFullYear()+1;
}
return thisDate.getFullYear()-1;
return thisDate.getFullYear();
}
return thisDate.getFullYear()-1;
}
var EXPANSION_RULES_2 = {
Expand Down
6 changes: 6 additions & 0 deletions test/core/test_strftime.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,5 +301,11 @@ int main() {
size = strftime(s, sizeof(s), "%Y-%m-%d %G %V %w", &tm);
TEST(!cmp(s, "2018-12-17 2018 51 1"), "strftime test #37b", s);

size = strftime(s, sizeof(s), "%c", &tm);
TEST(!cmp(s, "Mon Dec 17 00:00:00 2018"), "strftime test #36", s);

size = strftime(s, sizeof(s), "%Ec", &tm);
TEST(!cmp(s, "Mon Dec 17 00:00:00 2018"), "strftime test #36a", s);

return 0;
}
2 changes: 2 additions & 0 deletions test/core/test_strftime.out
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,5 @@ strftime test #36a: 1
strftime test #37: 1
strftime test #37a: 1
strftime test #37b: 1
strftime test #36: 1
strftime test #36a: 1

0 comments on commit 223d61e

Please sign in to comment.