[SERVER-6190] $week operator does not conform to spec when first day of the year is a sunday Created: 24/Jun/12  Updated: 15/Aug/12  Resolved: 03/Jul/12

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: 2.2.0-rc0

Type: Improvement Priority: Major - P3
Reporter: Aaron Staple Assignee: Aaron Staple
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

Observed behavior: The documentation says that $week will be 0 for days before the first sunday of the year. However, if the first day of the year is a sunday $week is implemented to be 0 for days before the second sunday of the year.
Expected behavior: $week is 0 for days before the first sunday of the year, as described in the documentation.

Test:

c = db.c;
c.drop();
 
// Sunday Jan 1 1984                                                                     
sun = new Date( 1984, 0, 1 );
assert.eq( 1984, sun.getFullYear() );
assert.eq( 0, sun.getMonth() );
assert.eq( 1, sun.getDate() );
assert.eq( 0, sun.getDay() );
 
// Monday Jan 2 1984                                                                     
mon = new Date( 1984, 0, 2 );
assert.eq( 1984, mon.getFullYear() );
assert.eq( 0, mon.getMonth() );
assert.eq( 2, mon.getDate() );
assert.eq( 1, mon.getDay() );
 
c.save( {} );
 
// These dates are not before the first sunday of the year and are expected to be in week 1.
assert.eq( 1, c.aggregate( { $project:{ a:{ $week:[ sun ] } } } ).result[ 0 ].a );
assert.eq( 1, c.aggregate( { $project:{ a:{ $week:[ mon ] } } } ).result[ 0 ].a );



 Comments   
Comment by auto [ 03/Jul/12 ]

Author:

{u'date': u'2012-07-02T19:27:12-07:00', u'email': u'aaron@10gen.com', u'name': u'astaple'}

Message: SERVER-6190 Clarify $week documentation.
Branch: master
https://github.com/mongodb/docs/commit/8b8ff9e25881c444b5ab1112e7e4659ab483e6a7

Comment by auto [ 03/Jul/12 ]

Author:

{u'date': u'2012-07-02T16:00:13-07:00', u'name': u'Aaron', u'email': u'aaron@10gen.com'}

Message: SERVER-6190 Fix $week expression calculations.
Branch: master
https://github.com/mongodb/mongo/commit/9cd23cdee154e440a57eb78f1452b18dc196723c

Comment by Mathias Stearn [ 02/Jul/12 ]

Add a note to the docs that $week produces the same number as strftime() with "%U".

Generated at Thu Feb 08 03:10:59 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.