Move weeks-/days of week-related methods to DateUtil
authorMatthias Schmidt <gravatronics@live.com>
Fri, 30 May 2014 18:55:39 +0000 (20:55 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Fri, 30 May 2014 18:55:39 +0000 (20:55 +0200)
com.woltlab.wcf/package.xml
wcfsetup/install/files/lib/util/DateUtil.class.php

index 3438ed1a49607c83d294d40810c4652eea041554..43018e0a035a6a8b16884531dba7ad84f1eb1327 100644 (file)
@@ -5,7 +5,7 @@
                <packagedescription><![CDATA[Free web-framework, designed and developed for complex community applications.]]></packagedescription>
                <packagedescription language="de"><![CDATA[Freies Web-Framework, das für komplexe Community-Anwendungen entworfen und entwickelt wurde.]]></packagedescription>
                <isapplication>1</isapplication>
-               <version>2.0.6 pl 1</version> <!-- codename: maelstrom -->
+               <version>2.0.7</version> <!-- codename: maelstrom -->
                <date>2014-05-03</date>
        </packageinformation>
        
index 3dbb9cdab6b1070c153e08a60470abe6f94af36f..7e38c2da93646b11168997b763af3b54b30884aa 100644 (file)
@@ -130,6 +130,26 @@ final class DateUtil {
                'Pacific/Apia', // (UTC+13:00) Samoa
        );
        
+       /**
+        * first day of the week
+        * 0=sunday
+        * 1=monday
+        * @var integer
+        */
+       private static $firstDayOfTheWeek = null;
+       
+       /**
+        * order of the week days
+        * @var array<string>
+        */
+       private static $weekDays = null;
+       
+       /**
+        * order of the week days (short textual representation)
+        * @var array<string>
+        */
+       private static $shortWeekDays = null;
+       
        /**
         * Returns a formatted date.
         * 
@@ -366,5 +386,99 @@ final class DateUtil {
                }
        }
        
+       /**
+        * Returns the first day of the week.
+        * 
+        * @return      integer
+        */
+       public static function getFirstDayOfTheWeek() {
+               if (self::$firstDayOfTheWeek === null) {
+                       self::$firstDayOfTheWeek = intval(WCF::getLanguage()->get('wcf.date.firstDayOfTheWeek'));
+                       if (self::$firstDayOfTheWeek != 1 && self::$firstDayOfTheWeek != 0) self::$firstDayOfTheWeek = 0;
+               }
+               
+               return self::$firstDayOfTheWeek;
+       }
+       
+       /**
+        * Returns the order of the week days.
+        * 
+        * @return      array<string>
+        */
+       public static function getWeekDays() {
+               if (self::$weekDays === null) {
+                       if (self::getFirstDayOfTheWeek() == 1) {
+                               self::$weekDays = array(
+                                       1 => 'monday',
+                                       2 => 'tuesday',
+                                       3 => 'wednesday',
+                                       4 => 'thursday',
+                                       5 => 'friday',
+                                       6 => 'saturday',
+                                       0 => 'sunday'
+                               );
+                       }
+                       else {
+                               self::$weekDays = array(
+                                       0 => 'sunday',
+                                       1 => 'monday',
+                                       2 => 'tuesday',
+                                       3 => 'wednesday',
+                                       4 => 'thursday',
+                                       5 => 'friday',
+                                       6 => 'saturday'
+                               );
+                       }
+               }
+               
+               return self::$weekDays;
+       }
+       
+       /**
+        * Returns the order of the week days (short textual representation).
+        * 
+        * @return      array<string>
+        */
+       public static function getShortWeekDays() {
+               if (self::$shortWeekDays === null) {
+                       if (self::getFirstDayOfTheWeek() == 1) {
+                               self::$shortWeekDays = array(
+                                       1 => 'mon',
+                                       2 => 'tue',
+                                       3 => 'wed',
+                                       4 => 'thu',
+                                       5 => 'fri',
+                                       6 => 'sat',
+                                       0 => 'sun'
+                               );
+                       }
+                       else {
+                               self::$shortWeekDays = array(
+                                       0 => 'sun',
+                                       1 => 'mon',
+                                       2 => 'tue',
+                                       3 => 'wed',
+                                       4 => 'thu',
+                                       5 => 'fri',
+                                       6 => 'sat'
+                               );
+                       }
+               }
+               
+               return self::$shortWeekDays;
+       }
+       
+       /**
+        * Returns the number of weeks in the given year.
+        * 
+        * @param       integer         $year
+        * @return      integer
+        */
+       public static function getWeeksInYear($year) {
+               $date = new \DateTime();
+               $date->setISODate($year, 53, self::getFirstDayOfTheWeek());
+               return ($date->format('W') == 53 ? 53 : 52);
+       }
+       
        private function __construct() { }
 }