Overview

Namespaces

  • Peach
    • DF
    • DT
    • Http
      • Body
      • Header
    • Markup
    • Util

Classes

  • Peach\DF\Base64Codec
  • Peach\DF\CodecChain
  • Peach\DF\JsonCodec
  • Peach\DF\SerializationCodec
  • Peach\DF\Utf8Codec
  • Peach\DT\AbstractTime
  • Peach\DT\Clock
  • Peach\DT\Date
  • Peach\DT\Datetime
  • Peach\DT\DefaultClock
  • Peach\DT\FixedClock
  • Peach\DT\FormatWrapper
  • Peach\DT\HttpDateFormat
  • Peach\DT\OffsetClock
  • Peach\DT\ShiftFormat
  • Peach\DT\SimpleFormat
  • Peach\DT\TimeEquator
  • Peach\DT\Timestamp
  • Peach\DT\TimeWrapper
  • Peach\DT\UnixTimeFormat
  • Peach\DT\Util
  • Peach\DT\W3cDatetimeFormat
  • Peach\Http\Body
  • Peach\Http\Body\CodecRenderer
  • Peach\Http\Body\StringRenderer
  • Peach\Http\DefaultEndpoint
  • Peach\Http\Header\CookieItem
  • Peach\Http\Header\CookieOptions
  • Peach\Http\Header\HttpDate
  • Peach\Http\Header\NoField
  • Peach\Http\Header\QualityValues
  • Peach\Http\Header\Raw
  • Peach\Http\Header\SetCookie
  • Peach\Http\Header\Status
  • Peach\Http\Request
  • Peach\Http\Response
  • Peach\Http\Util
  • Peach\Markup\AbstractHelper
  • Peach\Markup\AbstractRenderer
  • Peach\Markup\BaseHelper
  • Peach\Markup\BreakControlWrapper
  • Peach\Markup\Builder
  • Peach\Markup\Code
  • Peach\Markup\Comment
  • Peach\Markup\ContainerElement
  • Peach\Markup\Context
  • Peach\Markup\DebugBuilder
  • Peach\Markup\DebugContext
  • Peach\Markup\DefaultBreakControl
  • Peach\Markup\DefaultBuilder
  • Peach\Markup\DefaultContext
  • Peach\Markup\Element
  • Peach\Markup\EmptyElement
  • Peach\Markup\HelperObject
  • Peach\Markup\HtmlHelper
  • Peach\Markup\Indent
  • Peach\Markup\MinimalBreakControl
  • Peach\Markup\NameBreakControl
  • Peach\Markup\NameValidator
  • Peach\Markup\NodeList
  • Peach\Markup\None
  • Peach\Markup\SgmlRenderer
  • Peach\Markup\Text
  • Peach\Markup\XmlRenderer
  • Peach\Util\AbstractMapEntry
  • Peach\Util\ArrayMap
  • Peach\Util\ArrayMapEntry
  • Peach\Util\Arrays
  • Peach\Util\DefaultComparator
  • Peach\Util\DefaultEquator
  • Peach\Util\HashMap
  • Peach\Util\HashMapEntry
  • Peach\Util\Strings
  • Peach\Util\Values

Interfaces

  • Peach\DF\Codec
  • Peach\DT\Format
  • Peach\DT\Time
  • Peach\Http\BodyRenderer
  • Peach\Http\Endpoint
  • Peach\Http\HeaderField
  • Peach\Http\MultiHeaderField
  • Peach\Http\SingleHeaderField
  • Peach\Markup\BreakControl
  • Peach\Markup\Component
  • Peach\Markup\Container
  • Peach\Markup\Helper
  • Peach\Markup\Node
  • Peach\Markup\Renderer
  • Peach\Util\Comparable
  • Peach\Util\Comparator
  • Peach\Util\Equator
  • Peach\Util\Map
  • Peach\Util\MapEntry
  • Overview
  • Namespace
  • Class
  1: <?php
  2: /*
  3:  * Copyright (c) 2014 @trashtoy
  4:  * https://github.com/trashtoy/
  5:  * 
  6:  * Permission is hereby granted, free of charge, to any person obtaining a copy of
  7:  * this software and associated documentation files (the "Software"), to deal in
  8:  * the Software without restriction, including without limitation the rights to use,
  9:  * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
 10:  * Software, and to permit persons to whom the Software is furnished to do so,
 11:  * subject to the following conditions:
 12:  * 
 13:  * The above copyright notice and this permission notice shall be included in all
 14:  * copies or substantial portions of the Software.
 15:  * 
 16:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17:  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 18:  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 19:  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 20:  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 21:  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 22:  */
 23: /**
 24:  * PHP class file.
 25:  * @auhtor trashtoy
 26:  * @since  2.0.0
 27:  */
 28: namespace Peach\DT;
 29: use Peach\Util\Values;
 30: use Peach\Util\Arrays;
 31: 
 32: /**
 33:  * 時間関連のユーティリティクラスです.
 34:  */
 35: class Util
 36: {
 37:     /**
 38:      * このクラスはインスタンス化できません.
 39:      */
 40:     private function __construct() {}
 41:     
 42:     /**
 43:      * 2つの時間オブジェクトを比較します. 一つめの引数のほうが後 (未来) の場合は正の値,
 44:      * 前 (過去) の場合は負の値, 同じ場合は 0 を返します.
 45:      * 
 46:      * このメソッドは, 一つ目の時間オブジェクトの {@link Time::compareTo} メソッドを呼び出し
 47:      * 二つ目の時間オブジェクトと比較した結果を返します.
 48:      *
 49:      * @param  Time $time1 1つ目の時間
 50:      * @param  Time $time2 2つ目の時間
 51:      * @return int         1つ目の時間を2つ目の時間と比較した結果
 52:      * @see    Time::compareTo()
 53:      */
 54:     public static function compareTime(Time $time1, Time $time2)
 55:     {
 56:         return $time1->compareTo($time2);
 57:     }
 58:     
 59:     /**
 60:      * 引数に渡された時間オブジェクトの中で最も古いものを返します.
 61:      * 引数に Time 型オブジェクトを羅列するか, Time 型オブジェクトの配列を指定してください.
 62:      * 引数に Time が一つも存在しない場合は NULL を返します.
 63:      * 
 64:      * @param array|Time $time...
 65:      * @return Time      引数の中で最も古い Time オブジェクト.
 66:      *                   引数が空か, Time オブジェクトが含まれていない場合は NULL
 67:      */
 68:     public static function oldest()
 69:     {
 70:         $args = func_get_args();
 71:         if (isset($args[0]) && is_array($args[0])) {
 72:             $args = $args[0];
 73:         }
 74:         return Arrays::min(Arrays::pickup($args, "Peach\\DT\\Time"));
 75:     }
 76:     
 77:     /**
 78:      * 引数に渡された時間オブジェクトの中で最新のものを返します.
 79:      * 引数に Time 型オブジェクトを羅列するか, Time 型オブジェクトの配列を指定してください.
 80:      * 引数に Time が一つも存在しない場合は NULL を返します.
 81:      * 
 82:      * @param  array|Time $time...
 83:      * @return Time       引数の中で最新の Time オブジェクト.
 84:      *                    引数が空か, Time オブジェクトが含まれていない場合は NULL
 85:      */
 86:     public static function latest()
 87:     {
 88:         $args = func_get_args();
 89:         if (isset($args[0]) && is_array($args[0])) {
 90:             $args = $args[0];
 91:         }
 92:         return Arrays::max(Arrays::pickup($args, "Peach\\DT\\Time"));
 93:     }
 94:     
 95:     /**
 96:      * 引数に指定された年・月・日 (オプションで時・分・秒) の妥当性を検証します.
 97:      * 妥当な組み合わせの場合は TRUE, それ以外は FALSE を返します.
 98:      * 引数に数値以外の型が含まれていた場合, それが数値文字列
 99:      * ({@link http://www.php.net/manual/ja/function.is-numeric.php is_numeric()} が TRUE を返す)
100:      * の場合のみ妥当とみなします.
101:      * 
102:      * @param int $year   年
103:      * @param int $month  月
104:      * @param int $date   日
105:      * @param int $hour   時
106:      * @param int $minute 分
107:      * @param int $second 秒
108:      * @return bool 組み合わせが妥当な場合に TRUE, それ以外は FALSE
109:      */
110:     public static function validate($year, $month, $date, $hour = 0, $minute = 0, $second = 0)
111:     {
112:         $test = array("year" => $year, "month" => $month, "date" => $date, "hour" => $hour, "minute" => $minute, "second" => $second);
113:         $d    = new Timestamp($year, $month, $date, $hour, $minute, $second);
114:         foreach ($test as $key => $value) {
115:             if (!is_numeric($value) || $d->get($key) !== intval($value)) {
116:                 return false;
117:             }
118:         }
119:         return true;
120:     }
121:     
122:     /**
123:      * システム時刻と GMT との時差を, 分単位で返します.
124:      * 
125:      * システム時刻が GMT よりも未来の場合 (UTC + 1 以上) は負の値,
126:      * システム時刻が GMT よりも過去の場合 (UTF - 1 以下) は正の値を返します.
127:      * 例えばタイムゾーンが "Asia/Tokyo" (+09:00) の場合,
128:      * 返り値は -540 となります.
129:      * 
130:      * @return int
131:      */
132:     public static function getTimeZoneOffset()
133:     {
134:         $local = mktime(0, 0, 0, 1, 1, 1970);
135:         $gmt   = gmmktime(0, 0, 0, 1, 1, 1970);
136:         return ($local - $gmt) / 60;
137:     }
138:     
139:     /**
140:      * 指定された時差 (分単位) の値が妥当かどうかを調べ, 必要に応じて値を丸めた結果を返します.
141:      * 世界で実際に使用されているタイムゾーンは UTC-12 から UTC+14 ですが,
142:      * このメソッドでは -23:45 から +23:45 を妥当なタイムゾーンとみなします.
143:      * もしも -23:45 以前の時差が指定された場合は -23:45 (1425),
144:      * +23:45 以降の時差が指定された場合は +23:45 (-1425) に丸めた結果を返します.
145:      * 
146:      * もしも NULL が指定された場合は {@link Util::getTimeZoneOffset} の結果を返します.
147:      * それ以外の数値以外の値が指定された場合は, 整数にキャストした値を使用します.
148:      * 
149:      * @param  int $offset
150:      * @return int 引数を -1425 以上 1425 以下に丸めた値
151:      */
152:     public static function cleanTimeZoneOffset($offset)
153:     {
154:         return isset($offset) ?
155:             Values::intValue($offset, -1425, 1425) : // -23:45 から +23:45 まで
156:             Util::getTimeZoneOffset();
157:     }
158: }
159: 
PEACH2 API documentation generated by ApiGen