From d0b48367b23d8d6ca1418899d9f0a896504d7e8c Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Fri, 30 May 2014 20:55:39 +0200 Subject: [PATCH] Move weeks-/days of week-related methods to DateUtil --- com.woltlab.wcf/package.xml | 2 +- .../install/files/lib/util/DateUtil.class.php | 114 ++++++++++++++++++ 2 files changed, 115 insertions(+), 1 deletion(-) diff --git a/com.woltlab.wcf/package.xml b/com.woltlab.wcf/package.xml index 3438ed1a49..43018e0a03 100644 --- a/com.woltlab.wcf/package.xml +++ b/com.woltlab.wcf/package.xml @@ -5,7 +5,7 @@ 1 - 2.0.6 pl 1 + 2.0.7 2014-05-03 diff --git a/wcfsetup/install/files/lib/util/DateUtil.class.php b/wcfsetup/install/files/lib/util/DateUtil.class.php index 3dbb9cdab6..7e38c2da93 100644 --- a/wcfsetup/install/files/lib/util/DateUtil.class.php +++ b/wcfsetup/install/files/lib/util/DateUtil.class.php @@ -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 + */ + private static $weekDays = null; + + /** + * order of the week days (short textual representation) + * @var array + */ + 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 + */ + 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 + */ + 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() { } } -- 2.20.1