Date.st
changeset 16913 d504bc963add
parent 16839 7e3621d2361b
child 16927 38c1c35a9892
--- a/Date.st	Sat Nov 01 17:50:15 2014 +0100
+++ b/Date.st	Wed Nov 05 17:49:21 2014 +0100
@@ -281,7 +281,7 @@
 newDay:day month:month year:year
     "return a new Date, given the day, month and year.
      For your convenience, month may be either an integer 
-     or the months name as a string. 
+     or the month's name as a string. 
 
      WARNING: semantics changed: 0..99 is no longer treated as 1900..1999,
               but as 0..99 now.
@@ -360,11 +360,41 @@
      Date newDay:365 year:1994
      Date newDay:366 year:1994
      Date newDay:0 year:1994
+
+     Date newDay:271 year:2008
+     Date newDay:270 year:2008
+     Date newDay:271 year:2007
     "
 
     "Modified: 1.7.1996 / 14:22:24 / cg"
 !
 
+newDayInWeek:dayInWeek week:week year:yearArg
+    "return a new Date, given the year, the week (1..) and the day in week (1..7).
+     See http://en.wikipedia.org/wiki/ISO_week_date"
+
+    |weekDayOfJan4 dayInYear year|
+
+    year := yearArg.
+    weekDayOfJan4 := (Date newDay:4 month:1 year:year) dayInWeek.
+    dayInYear := (week * 7) + dayInWeek - (weekDayOfJan4 + 3).
+    dayInYear < 1 ifTrue:[
+        dayInYear := dayInYear + (Date daysInYear:year-1).
+        year := year - 1.
+    ].
+    dayInYear > (Date daysInYear:year) ifTrue:[
+        dayInYear := dayInYear - (Date daysInYear:year).
+        year := year + 1.
+    ].
+    ^ Date newDay:dayInYear year:year.
+
+    "
+     Date newDayInWeek:6 week:39 year:2008     
+     Date newDayInWeek:1 week:40 year:2014     
+     Date newDayInWeek:1 week:44 year:2014     
+    "
+!
+
 readFrom:aStringOrStream onError:exceptionBlock
     "return a new Date, reading a printed representation from aStream.
      Notice, that this is not the storeString format and 
@@ -824,9 +854,80 @@
 
 !Date class methodsFor:'general queries'!
 
+abbreviatedDayNamesForLanguage:languageOrNilForDefault
+    "return a collection of short month-names for a given language or the 
+     current language if nil is given.
+     The returned strings depend on the resource translation file to be present for the 
+     given language (i.e. libbasic/resources(<lang>.rs).
+     If not, english names are returned"
+
+    |langDayAbbrevs lang|
+
+    (DayAbbrevs isNil or:[EnvironmentChange]) ifTrue:[
+        self initNames
+    ].
+    lang := languageOrNilForDefault notNil ifTrue:[languageOrNilForDefault] ifFalse:[Smalltalk language].
+    langDayAbbrevs := DayAbbrevs at:lang ifAbsent:nil.
+
+    "/ If language is not found, try to initialize it from the resources and try again
+    langDayAbbrevs isNil ifTrue:[ 
+        self initNamesForLanguage: lang.
+        "/ If language initialization failed, take english dayAbbrevs
+        langDayAbbrevs := DayAbbrevs at:lang ifAbsent:[DayAbbrevs at:#en.].
+    ].
+    ^ langDayAbbrevs
+
+    "
+     self abbreviatedDayNamesForLanguage:#en   
+     self abbreviatedDayNamesForLanguage:#de   
+     self abbreviatedDayNamesForLanguage:#fr   
+     self abbreviatedDayNamesForLanguage:#es   
+     self abbreviatedDayNamesForLanguage:#zulu 
+
+     self abbreviatedDayNamesForLanguage:nil   
+    "
+!
+
+abbreviatedMonthNamesForLanguage:languageOrNilForDefault
+    "return a collection of short month-names for a given language or the 
+     current language if nil is given.
+     The returned strings depend on the resource translation file to be present for the 
+     given language (i.e. libbasic/resources(<lang>.rs).
+     If not, english names are returned"
+
+    |langMonthAbbrevs lang|
+
+    (MonthAbbrevs isNil or:[EnvironmentChange]) ifTrue:[
+        self initNames
+    ].
+    lang := languageOrNilForDefault notNil ifTrue:[languageOrNilForDefault] ifFalse:[Smalltalk language].
+    langMonthAbbrevs := MonthAbbrevs at:lang ifAbsent:nil.
+
+    "/ If language is not found, try to initialize it from the resources and try again
+    langMonthAbbrevs isNil ifTrue:[ 
+        self initNamesForLanguage: lang.
+        "/ If language initialization failed, take english monthAbbrevs
+        langMonthAbbrevs := MonthAbbrevs at:lang ifAbsent:[MonthAbbrevs at:#en.].
+    ].
+    ^ langMonthAbbrevs
+
+    "
+     self abbreviatedMonthNamesForLanguage:#en
+     self abbreviatedMonthNamesForLanguage:#de
+     self abbreviatedMonthNamesForLanguage:#fr
+     self abbreviatedMonthNamesForLanguage:#es
+     self abbreviatedMonthNamesForLanguage:#zulu
+
+     self abbreviatedMonthNamesForLanguage:nil
+    "
+
+    "Modified (format): / 18-07-2011 / 09:34:20 / cg"
+!
+
 abbreviatedNameOfDay:dayIndex
-    "given a day index (1..7), return the abbreviated name
-     of the day"
+    "given a day index (1..7), 
+     return the abbreviated name of the day 
+     for the current language setting"
 
     ^ self abbreviatedNameOfDay:dayIndex language:nil
 
@@ -835,22 +936,27 @@
     "
 !
 
-abbreviatedNameOfDay:dayIndex language:lang
-    "given a day index (1..7), return the abbreviated name
-     of the day.
-     For nil, Smalltalk language is used,
-     for unknown languages, english is used." 
-
-    ^ (self dayAbbrevsForLanguage:lang) at:dayIndex
+abbreviatedNameOfDay:dayIndex language:langOrNilForDefault
+    "given a day index (1..7), 
+     return the abbreviated name of the day.
+     For nil, the current default language us used.
+     For unknown languages, english is used." 
+
+    ^ (self abbreviatedDayNamesForLanguage:langOrNilForDefault) at:dayIndex
 
     "
-     Date abbreviatedNameOfDay:4 language:#en
+     Date abbreviatedNameOfDay:4 language:#en    
+     Date abbreviatedNameOfDay:4 language:#de    
+     Date abbreviatedNameOfDay:4 language:#zulu  
+     Date abbreviatedNameOfDay:4 language:nil    
     "
 !
 
 abbreviatedNameOfMonth:monthIndex
-    "given a month index (1..12), return the abbreviated name
-     of the month"
+    "given a month index (1..12), 
+     return the abbreviated name of the month
+     for the current language setting"
+
 
     ^ self abbreviatedNameOfMonth:monthIndex language:nil
 
@@ -860,17 +966,21 @@
     "
 !
 
-abbreviatedNameOfMonth:monthIndex language:lang
-    "given a month index (1..12), return the abbreviated name
-     of the month.
-     For nil, Smalltalk language is used,
-     for unknown languages, english is used." 
-
-    ^ (self abbreviatedMonthNamesForLanguage:lang) at:monthIndex
+abbreviatedNameOfMonth:monthIndex language:langOrNilForDefault
+    "given a month index (1..12), 
+     return the abbreviated name of the month.
+     For nil, the current default language us used.
+     For unknown languages, english is used." 
+
+    ^ (self abbreviatedMonthNamesForLanguage:langOrNilForDefault) at:monthIndex
 
     "
-     Date abbreviatedNameOfMonth:11 language:#en
-     Date abbreviatedNameOfMonth:12 language:#en
+     Date abbreviatedNameOfMonth:11 language:#en     
+     Date abbreviatedNameOfMonth:12 language:#en     
+     Date abbreviatedNameOfMonth:12 language:#de     
+     Date abbreviatedNameOfMonth:12 language:#fr     
+     Date abbreviatedNameOfMonth:12 language:#zulu   
+     Date abbreviatedNameOfMonth:12 language:nil     
     "
 !
 
@@ -884,8 +994,44 @@
     "
 !
 
+dayNamesForLanguage:languageOrNilForDefault
+    "return a collection of day-names for a given language or the 
+     current language if nil is given.
+     The returned strings depend on the resource translation file to be present for the 
+     given language (i.e. libbasic/resources(<lang>.rs).
+     If not, english names are returned"
+
+    |langDayNames lang|
+
+    (DayNames isNil or:[EnvironmentChange]) ifTrue:[
+        self initNames
+    ].
+    lang := languageOrNilForDefault notNil ifTrue:[languageOrNilForDefault] ifFalse:[Smalltalk language].
+    langDayNames := DayNames at:lang ifAbsent:nil.
+
+    "/ If language is not found, try to initialize it from the resources and try again
+    langDayNames isNil ifTrue:[ 
+        self initNamesForLanguage: lang.
+        "/ If language initialization failed, take english dayNames
+        langDayNames := DayNames at:lang ifAbsent:[DayNames at:#en].
+    ].
+    ^ langDayNames
+
+    "
+     self dayNamesForLanguage:#en
+     self dayNamesForLanguage:#de
+     self dayNamesForLanguage:#fr   
+     self dayNamesForLanguage:#es   
+
+     self dayNamesForLanguage:#zulu 
+     self dayNamesForLanguage:nil   
+    "
+
+    "Modified (format): / 18-07-2011 / 09:34:08 / cg"
+!
+
 dayOfFirstWeekInYear:aYear
-    "for a given year, return the day corresponding to that years monday of week-01.
+    "for a given year, return the day corresponding to that year's monday of week-01.
      The 1st week is the one, in which the first thursday is found;
      and the 1st day of that week is the preceeding monday 
      (that means: that the returned day might be a day of the previous year)"
@@ -1200,6 +1346,42 @@
     "
 !
 
+monthNamesForLanguage:languageOrNilForDefault
+    "return a collection of month-names for a given language or the 
+     current language if nil is given.
+     The returned strings depend on the resource translation file to be present for the 
+     given language (i.e. libbasic/resources(<lang>.rs).
+     If not, english names are returned"
+
+    |langMonthNames lang|
+
+    (MonthNames isNil or:[EnvironmentChange]) ifTrue:[
+        self initNames
+    ].
+    lang := languageOrNilForDefault notNil ifTrue:[languageOrNilForDefault] ifFalse:[Smalltalk language].
+    langMonthNames := MonthNames at:lang ifAbsent:nil.
+
+    "/ If language is not found, try to initialize it from the resources and try again
+    langMonthNames isNil ifTrue:[ 
+        self initNamesForLanguage: lang.
+        "/ If language initialization failed, take english monthNames
+        langMonthNames := MonthNames at:lang ifAbsent:[MonthNames at:#en.].
+    ].
+    ^ langMonthNames
+
+    "
+     self monthNamesForLanguage:#en  
+     self monthNamesForLanguage:#de  
+     self monthNamesForLanguage:#fr   
+     self monthNamesForLanguage:#es   
+
+     self monthNamesForLanguage:#zulu 
+     self monthNamesForLanguage:nil   
+    "
+
+    "Modified: / 18-07-2011 / 09:34:32 / cg"
+!
+
 nameOfDay:dayIndex
     "given a day index (1..7), return the name of the day" 
 
@@ -1323,6 +1505,14 @@
      Date weekInYearOf:(Date newDay:1 month:1 year:2002)    
      Date weekInYearOf:(Date newDay:2 month:1 year:2002)      
      Date weekInYearOf:(Date newDay:7 month:1 year:2002)      
+     Date weekInYearOf:(Date newDay:26 month:9 year:2008) 
+     Date weekInYearOf:(Date newDay:27 month:9 year:2008)
+     Date weekInYearOf:(Date newDay:4 month:1 year:2008)
+
+     Date weekInYearOf:(Date newDay:1 month:1 year:2010) 
+     Date weekInYearOf:(Date newDay:2 month:1 year:2010) 
+     Date weekInYearOf:(Date newDay:3 month:1 year:2010) 
+     Date weekInYearOf:(Date newDay:4 month:1 year:2010) 
     "
 !
 
@@ -1392,87 +1582,28 @@
 
 !Date class methodsFor:'private'!
 
-abbreviatedMonthNamesForLanguage:languageOrNilForDefault
-    |langMonthAbbrevs lang|
-
-    (MonthAbbrevs isNil or:[EnvironmentChange]) ifTrue:[
-        self initNames
-    ].
-    lang := languageOrNilForDefault notNil ifTrue:[languageOrNilForDefault] ifFalse:[Smalltalk language].
-    langMonthAbbrevs := MonthAbbrevs at:lang ifAbsent:nil.
-
-    "/ If language is not found, try to initialize it from the resources and try again
-    langMonthAbbrevs isNil ifTrue:[ 
-        self initNamesForLanguage: lang.
-        "/ If language initialization failed, take english monthAbbrevs
-        langMonthAbbrevs := MonthAbbrevs at:lang ifAbsent:[MonthAbbrevs at:#en.].
-    ].
-    ^ langMonthAbbrevs
-
-    "
-     self abbreviatedMonthNamesForLanguage:#en
-     self abbreviatedMonthNamesForLanguage:#de
-     self abbreviatedMonthNamesForLanguage:#fr
-     self abbreviatedMonthNamesForLanguage:#es
-    "
-
-    "Modified (format): / 18-07-2011 / 09:34:20 / cg"
-!
-
 dayAbbrevsForLanguage:languageOrNilForDefault
-    |langDayAbbrevs lang|
-
-    (DayAbbrevs isNil or:[EnvironmentChange]) ifTrue:[
-        self initNames
-    ].
-    lang := languageOrNilForDefault notNil ifTrue:[languageOrNilForDefault] ifFalse:[Smalltalk language].
-    langDayAbbrevs := DayAbbrevs at:lang ifAbsent:nil.
-
-    "/ If language is not found, try to initialize it from the resources and try again
-    langDayAbbrevs isNil ifTrue:[ 
-        self initNamesForLanguage: lang.
-        "/ If language initialization failed, take english dayAbbrevs
-        langDayAbbrevs := DayAbbrevs at:lang ifAbsent:[DayAbbrevs at:#en.].
-    ].
-    ^ langDayAbbrevs
+    <resource: #obsolete>
+    "return a collection of short day-names for a given language or the 
+     current language if nil is given.
+     The returned strings depend on the resource translation file to be present for the 
+     given language (i.e. libbasic/resources(<lang>.rs).
+     If not, english names are returned"
+
+    ^ self abbreviatedDayNamesForLanguage:languageOrNilForDefault
 
     "
      self dayAbbrevsForLanguage:#en
      self dayAbbrevsForLanguage:#de
      self dayAbbrevsForLanguage:#fr
      self dayAbbrevsForLanguage:#es
+     self dayAbbrevsForLanguage:nil
+     self dayAbbrevsForLanguage:#zulu
     "
 
     "Modified (format): / 18-07-2011 / 09:34:14 / cg"
 !
 
-dayNamesForLanguage:languageOrNilForDefault
-    |langDayNames lang|
-
-    (DayNames isNil or:[EnvironmentChange]) ifTrue:[
-        self initNames
-    ].
-    lang := languageOrNilForDefault notNil ifTrue:[languageOrNilForDefault] ifFalse:[Smalltalk language].
-    langDayNames := DayNames at:lang ifAbsent:nil.
-
-    "/ If language is not found, try to initialize it from the resources and try again
-    langDayNames isNil ifTrue:[ 
-        self initNamesForLanguage: lang.
-        "/ If language initialization failed, take english dayNames
-        langDayNames := DayNames at:lang ifAbsent:[DayNames at:#en].
-    ].
-    ^ langDayNames
-
-    "
-     self dayNamesForLanguage:#en
-     self dayNamesForLanguage:#de
-     self dayNamesForLanguage:#fr
-     self dayNamesForLanguage:#es
-    "
-
-    "Modified (format): / 18-07-2011 / 09:34:08 / cg"
-!
-
 daysInMonthIndex: monthIndex forYear: yearInteger
     "return the number of days in month monthIndex of
      year yearInteger (modified GNU).
@@ -1504,10 +1635,10 @@
 !
 
 daysUntilMonthIndex: monthIndex forYear: yearInteger
-    "return the number of days in month monthIndex of
+    "return the number of days from 1st January up to month monthIndex of
      year yearInteger (modified GNU).
      Return 0 for invalid month index.
-     This is the internal version of daysInMonth:forYear:"
+     This is the internal version of dayInMonth:forYear:"
 
     |days|
 
@@ -1538,26 +1669,6 @@
      Date daysUntilMonthIndex:3 forYear:1980
      Date daysUntilMonthIndex:3 forYear:1981
     "
-!
-
-monthNamesForLanguage:languageOrNilForDefault
-    |langMonthNames lang|
-
-    (MonthNames isNil or:[EnvironmentChange]) ifTrue:[
-        self initNames
-    ].
-    lang := languageOrNilForDefault notNil ifTrue:[languageOrNilForDefault] ifFalse:[Smalltalk language].
-    langMonthNames := MonthNames at:lang ifAbsent:nil.
-
-    "/ If language is not found, try to initialize it from the resources and try again
-    langMonthNames isNil ifTrue:[ 
-        self initNamesForLanguage: lang.
-        "/ If language initialization failed, take english monthNames
-        langMonthNames := MonthNames at:lang ifAbsent:[MonthNames at:#en.].
-    ].
-    ^ langMonthNames
-
-    "Modified: / 18-07-2011 / 09:34:32 / cg"
 ! !
 
 !Date class methodsFor:'private-encoding/decoding'!
@@ -2068,7 +2179,7 @@
 !Date methodsFor:'accessing'!
 
 abbreviatedDayName
-    "return the short week-day of the receiver as a string.
+    "return the short week-day-name of the receiver as a string.
      The returned string depends on the language setting.
      Expect things like 'mon', 'tue' ..."
 
@@ -2080,8 +2191,26 @@
     "
 !
 
+abbreviatedDayNameForLanguage:lang
+    "return the short week-day-name of the receiver as a string.
+     The returned string depends on the resource translation file to be present for the 
+     given language (i.e. libbasic/resources(<lang>.rs).
+     If not, the english name is returned.
+     Expect things like 'mon', 'tue' ..."
+
+    ^ self class abbreviatedNameOfDay:(self dayInWeek) language:lang
+
+    "
+     Date today abbreviatedDayNameForLanguage:#en      
+     Date today abbreviatedDayNameForLanguage:#de      
+     Date today abbreviatedDayNameForLanguage:#zulu 
+
+     (Date newDay:15 month:4 year:1959) abbreviatedDayName
+    "
+!
+
 abbreviatedMonthName
-    "return the month of the receiver as a string.
+    "return the short month name of the receiver as a string.
      The returned string depends on the language setting.
      Expect things like 'jan', 'feb' ..."
 
@@ -2094,8 +2223,10 @@
 !
 
 abbreviatedMonthNameForLanguage:lang
-    "return the month of the receiver as a string.
-     The returned string depends on the language setting.
+    "return the short month-name of the receiver as a string in a given language.
+     The returned string depends on the resource translation file to be present for the 
+     given language (i.e. libbasic/resources(<lang>.rs).
+     If not, the english name is returned.
      Expect things like 'jan', 'feb' ..."
 
     ^ self class abbreviatedNameOfMonth:(self month) language:lang
@@ -2103,6 +2234,7 @@
     "
      Date today abbreviatedMonthNameForLanguage:#en 
      Date today abbreviatedMonthNameForLanguage:#de 
+     Date today abbreviatedMonthNameForLanguage:#zulu 
     "
 !
 
@@ -2123,8 +2255,10 @@
     "
      (Date newDay: 5 month: 8 year: 1962) asDays  -> should be 22496
      (Date newDay: 1 month: 1 year: 1901) asDays  -> 0
-     Date today asDays
-     Date fromDays:(Date today asDays + 7)
+     (Date newDay: 31 month: 12 year: 1900) asDays -> -1
+     (Date newDay: 1 month: 1 year: 1800) asDays  -> -36889
+     Date today asDays    
+     Date fromDays:(Date today asDays + 7) 
     "
 !
 
@@ -2319,15 +2453,20 @@
 !
 
 monthNameForLanguage:languageOrNil
-    "return the month of the receiver as a string.
-     The returned string depends on the language setting.
+    "return the month-name of the receiver as a string in a given language.
+     The returned string depends on the resource translation file to be present for the 
+     given language (i.e. libbasic/resources(<lang>.rs).
+     If not, the english name is returned.
      Expect things like 'january', 'february' ..."
 
     ^ self class nameOfMonth:(self month) language:languageOrNil
 
     "
-     Date today monthName
-     Date today monthNameForLanguage:#en
+     Date today monthName                 
+
+     Date today monthNameForLanguage:#en  
+     Date today monthNameForLanguage:#de  
+     Date today monthNameForLanguage:#zulu
     "
 !
 
@@ -2355,7 +2494,7 @@
 
 weekday
     "return the week-day of the receiver as a string.
-     The returned string depends on the language setting.
+     The returned string depends on the current language setting.
      Expect things like 'monday', 'tuesday' ...
      For ST-80 compatibility"
 
@@ -2367,6 +2506,26 @@
     "
 !
 
+weekdayForLanguage:lang
+    "return the week-day-name of the receiver as a string in a given language.
+     The returned string depends on the resource translation file to be present for the 
+     given language (i.e. libbasic/resources(<lang>.rs).
+     If not, the english name is returned.
+     Expect things like 'monday', 'tuesday'..."
+
+    ^ self class nameOfDay:(self dayInWeek) language:lang
+
+    "
+     Date today weekdayForLanguage:#de      -> Mittwoch
+     Date today weekdayForLanguage:#en      -> wednesday
+     Date today weekdayForLanguage:#zulu    -> wednesday       
+
+     (Date newDay:15 month:4 year:1959) weekdayForLanguage:#de  
+     (Date newDay:15 month:4 year:1959) weekdayForLanguage:#en  
+     (Date newDay:15 month:4 year:1959) weekdayForLanguage:#zulu
+    "
+!
+
 year
     "return the year (1..12) of the receiver"
 
@@ -3162,11 +3321,11 @@
 !Date class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Date.st,v 1.153 2014-09-12 15:37:40 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Date.st,v 1.154 2014-11-05 16:49:21 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/Date.st,v 1.153 2014-09-12 15:37:40 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Date.st,v 1.154 2014-11-05 16:49:21 cg Exp $'
 ! !