Week number from a date

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Week number from a date

Pharo Smalltalk Users mailing list
Hi guys,

Is there a way to have the week number when a date is given?

Ex: 2016/01/13 gives 2, 2016/01/19 gives 3, and so on

TIA
Davide



--
View this message in context: http://forum.world.st/Week-number-from-a-date-tp4894484.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: Week number from a date

cedreek
Not sure it exists (I didn’t find it but it may exists)… so I did a quick try to code a new method in the class Week.

There is already index but this method returns the index of the current month.

I did something like below (not tested much):

Week>>yearIndex

^ (self dayOfYear / 7 + 1) asInteger


====
DateAndTime now asWeek yearIndex    returns 19

Maybe it would be better to create an equivalent method (weekYearIndex) in Date (or eventually Timespan).

hth,

Cédrik

Reply | Threaded
Open this post in threaded view
|

Re: Week number from a date

Sven Van Caekenberghe-2
https://en.wikipedia.org/wiki/Week#Week_numbering

of course, not all countries have the same definition of week.
furthermore, week number does not seem to be so simple.
 

> On 12 May 2016, at 18:17, Cédrick Béler <[hidden email]> wrote:
>
> Not sure it exists (I didn’t find it but it may exists)… so I did a quick try to code a new method in the class Week.
>
> There is already index but this method returns the index of the current month.
>
> I did something like below (not tested much):
>
> Week>>yearIndex
>
> ^ (self dayOfYear / 7 + 1) asInteger
>
>
> ====
> DateAndTime now asWeek yearIndex    returns 19
>
> Maybe it would be better to create an equivalent method (weekYearIndex) in Date (or eventually Timespan).
>
> hth,
>
> Cédrik
>


Reply | Threaded
Open this post in threaded view
|

Re: Week number from a date

cedreek
In reply to this post by cedreek
What I did is wrong. It needs to add the first month days offset :)
Reply | Threaded
Open this post in threaded view
|

Re: Week number from a date

Davide Varvello
In reply to this post by Sven Van Caekenberghe-2
Yep :-)
Davide

Sven Van Caekenberghe-2 wrote
https://en.wikipedia.org/wiki/Week#Week_numbering

of course, not all countries have the same definition of week.
furthermore, week number does not seem to be so simple.
 
> On 12 May 2016, at 18:17, Cédrick Béler <[hidden email]> wrote:
>
> Not sure it exists (I didn’t find it but it may exists)… so I did a quick try to code a new method in the class Week.
>
> There is already index but this method returns the index of the current month.
>
> I did something like below (not tested much):
>
> Week>>yearIndex
>
> ^ (self dayOfYear / 7 + 1) asInteger
>
>
> ====
> DateAndTime now asWeek yearIndex    returns 19
>
> Maybe it would be better to create an equivalent method (weekYearIndex) in Date (or eventually Timespan).
>
> hth,
>
> Cédrik
>
Reply | Threaded
Open this post in threaded view
|

Re: Week number from a date

Pharo Smalltalk Users mailing list
In reply to this post by Sven Van Caekenberghe-2
Yep :-)
Davide


Sven Van Caekenberghe-2 wrote
> https://en.wikipedia.org/wiki/Week#Week_numbering
>
> of course, not all countries have the same definition of week.
> furthermore, week number does not seem to be so simple.
>  
>> On 12 May 2016, at 18:17, Cédrick Béler &lt;

> cdrick65@

> &gt; wrote:
>>
>> Not sure it exists (I didn’t find it but it may exists)… so I did a quick
>> try to code a new method in the class Week.
>>
>> There is already index but this method returns the index of the current
>> month.
>>
>> I did something like below (not tested much):
>>
>> Week>>yearIndex
>>
>> ^ (self dayOfYear / 7 + 1) asInteger
>>
>>
>> ====
>> DateAndTime now asWeek yearIndex    returns 19
>>
>> Maybe it would be better to create an equivalent method (weekYearIndex)
>> in Date (or eventually Timespan).
>>
>> hth,
>>
>> Cédrik
>>





--
View this message in context: http://forum.world.st/Week-number-from-a-date-tp4894486p4894535.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: Week number from a date

Ben Coman
In reply to this post by Pharo Smalltalk Users mailing list
On Thu, May 12, 2016 at 9:47 PM, Davide Varvello via Pharo-users
<[hidden email]> wrote:

>
>
> ---------- Forwarded message ----------
> From: Davide Varvello <[hidden email]>
> To: [hidden email]
> Cc:
> Date: Thu, 12 May 2016 06:10:12 -0700 (PDT)
> Subject: Week number from a date
> Hi guys,
>
> Is there a way to have the week number when a date is given?
>
> Ex: 2016/01/13 gives 2, 2016/01/19 gives 3, and so on

It seems the answer is no.  But to expand on how I determined that, in
the interest of teaching how to fish...

1. Open World > Tools > Finder
2. Select examples
3. Enter....
          Date readFromString: '2016/01/13' . 2016
4. Click <Search>
This returns one result, the message #year.

5. Next trying....      Date readFromString: '2016/01/13' . 3
This returns no result.

Think of the search term as an array of two objects separated by the
period.  The first object is the receiver of the message.  The last
object is the result. Any additional objects in between are message
parameters.  Try this one....    #(5 6 7 8) . 2 . #(5 6)


On Fri, May 13, 2016 at 12:24 AM, Sven Van Caekenberghe <[hidden email]> wrote:
> https://en.wikipedia.org/wiki/Week#Week_numbering
>
> of course, not all countries have the same definition of week.
> furthermore, week number does not seem to be so simple.

Interesting to know.  But I see this already seems hard coded...

Date nameOfDay: 1  "--> #Sunday"

Timespan subclass: #Date
    poolDictionaries: 'ChronologyConstants'

ChronologyConstants class >> initialize
     DayNames := #(Sunday Monday Tuesday Wednesday Thursday Friday Saturday).

Also seems to have changed at some point, with the historical
nameOfDay recorded in the Terse Guide [1] circa 2010 shown to be
different.  Perhaps this is something we should make a regional
preference.  Was there any ongoing works on other regional
preferences?

[1] http://wiki.squeak.org/squeak/5699

cheers -ben

Reply | Threaded
Open this post in threaded view
|

Re: Week number from a date

Davide Varvello
Thanks Ben
Davide
Reply | Threaded
Open this post in threaded view
|

Re: Week number from a date

Pharo Smalltalk Users mailing list
In reply to this post by Ben Coman
Thanks Ben
Davide



--
View this message in context: http://forum.world.st/Week-number-from-a-date-tp4894486p4894576.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: Week number from a date

cedreek
In reply to this post by Ben Coman
Yes I agree. We may have a preference to set the week day start (fistDayOfWeek ?).


After that we could agree on the fact the first week is the first complete week (otherwise it can be a preference).

Then we need to find the first day index of the first fistDayOfWeek.
(Date year: self year day: 1) weekdayIndex    
-> gives the index according to the preference (for now Sunday -> 1)

Then it’s quite straightforward to get the week number.

Just have to check if the current date is in the last week of the previous year (returns 52 or 53).
I did something but not sure yet (but no preference).

There are been some works on time representation (Chronos if I remember well ? but cannot load in Pharo 5, depends on OSProcess).

An interesting read [1].

Cheers,

Cédrik

ps: http://stephane.ducasse.free.fr/Teaching/CoursAnnecy/0506-M1-COO/A%20New%20Object-Oriented%20Model%20of%20the%20Gregorian%20Calendar.pdf 

Interesting to know. But I see this already seems hard coded...

Date nameOfDay: 1 "--> #Sunday"

Timespan subclass: #Date
poolDictionaries: 'ChronologyConstants'

ChronologyConstants class >> initialize
DayNames := #(Sunday Monday Tuesday Wednesday Thursday Friday Saturday).

Also seems to have changed at some point, with the historical
nameOfDay recorded in the Terse Guide [1] circa 2010 shown to be
different. Perhaps this is something we should make a regional
preference. Was there any ongoing works on other regional
preferences?

Reply | Threaded
Open this post in threaded view
|

Re: Week number from a date

Julián Maestri-2
Chalten might help, i remember it had representations for months of years (January of 1990), it might have something similar for weeks.

On 13 May 2016 at 05:40, Cédrick Béler <[hidden email]> wrote:
Yes I agree. We may have a preference to set the week day start (fistDayOfWeek ?).


After that we could agree on the fact the first week is the first complete week (otherwise it can be a preference).

Then we need to find the first day index of the first fistDayOfWeek.
(Date year: self year day: 1) weekdayIndex    
-> gives the index according to the preference (for now Sunday -> 1)

Then it’s quite straightforward to get the week number.

Just have to check if the current date is in the last week of the previous year (returns 52 or 53).
I did something but not sure yet (but no preference).

There are been some works on time representation (Chronos if I remember well ? but cannot load in Pharo 5, depends on OSProcess).

An interesting read [1].

Cheers,

Cédrik

ps: http://stephane.ducasse.free.fr/Teaching/CoursAnnecy/0506-M1-COO/A%20New%20Object-Oriented%20Model%20of%20the%20Gregorian%20Calendar.pdf 

Interesting to know. But I see this already seems hard coded...

Date nameOfDay: 1 "--> #Sunday"

Timespan subclass: #Date
poolDictionaries: 'ChronologyConstants'

ChronologyConstants class >> initialize
DayNames := #(Sunday Monday Tuesday Wednesday Thursday Friday Saturday).

Also seems to have changed at some point, with the historical
nameOfDay recorded in the Terse Guide [1] circa 2010 shown to be
different. Perhaps this is something we should make a regional
preference. Was there any ongoing works on other regional
preferences?