Zend_Locale-DatesTimes.xml 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <!-- EN-Revision: 24249 -->
  4. <sect1 id="zend.locale.date.datesandtimes">
  5. <!-- Skip-EN-Revisions: 22752 -->
  6. <title>日付および時刻の扱い</title>
  7. <para>
  8. <classname>Zend_Locale_Format</classname> では、日付や時刻を扱うためのメソッドを提供しています。
  9. これらを使用して、さまざまなロケールのさまざまな書式間での変換や正規化を行います。
  10. 日付データや
  11. <link linkend="zend.date.constants">
  12. <classname>Zend_Date</classname> がサポートする、さまざまな標準書式や地域化された日付書式
  13. </link>
  14. を満たす日付文字列の操作には <classname>Zend_Date</classname> を使用します。
  15. 既存の定義済み書式を使用すると、「十分にテストされたコードが使用できる」
  16. 「(一般的な使用法に従う限り) 可搬性や相互運用性を保証できる」といった利点があります。
  17. 以下の例は、ここで推奨した内容に従っていません。しかし、
  18. 非標準の書式を使用することで、これらの例は不必要にわかりにくくなってしまっています。
  19. </para>
  20. <sect2 id="zend.locale.date.normalize">
  21. <title>日付および時刻の正規化</title>
  22. <para>
  23. <methodname>getDate()</methodname> メソッドは、地域化された書式の日付を含む文字列をパースします。
  24. 結果は構造化された配列で返されます。この配列のキーが、日付の各部分を表します。
  25. さらに、この配列には 'date_format' というキーがあり、
  26. 入力日付文字列をパースする際に使用する書式文字列がここに含まれます。
  27. 地域化された日付文字列が日付/時刻のすべての要素を含んでいるとは限らないので、
  28. 配列のすべてのキーが存在するとは限りません。
  29. たとえば年月日のみが指定された場合は、時刻に関する情報は配列から取り除かれます。
  30. 逆に時分秒のみが指定された場合も同様です。
  31. 入力に日付や時刻が含まれない場合は、例外がスローされます。
  32. </para>
  33. <para>
  34. <methodname>setOption(array('fix_date' => true))</methodname> が設定されると、
  35. <methodname>getDate()</methodname> メソッドは
  36. 'fixed' というキーを追加します。ここには数値が格納され、
  37. 入力の日付文字列の年月日の順序を入れ替えたりして
  38. 使用する書式にあわせるための情報を表します。
  39. </para>
  40. <table id="zend.locale.date.normalize.table-1">
  41. <title>getDate() のオプション 'fix_date' のキーの値</title>
  42. <tgroup cols='2'>
  43. <thead>
  44. <row>
  45. <entry>値</entry>
  46. <entry>意味</entry>
  47. </row>
  48. </thead>
  49. <tbody>
  50. <row>
  51. <entry>0</entry>
  52. <entry>なにも修正しません。</entry>
  53. </row>
  54. <row>
  55. <entry>1</entry>
  56. <entry>無効な月を修正します。</entry>
  57. </row>
  58. <row>
  59. <entry>2</entry>
  60. <entry>日付と年の位置を入れ替えます。</entry>
  61. </row>
  62. <row>
  63. <entry>3</entry>
  64. <entry>月と年の位置を入れ替えます。</entry>
  65. </row>
  66. <row>
  67. <entry>4</entry>
  68. <entry>月と日の位置を入れ替えます。</entry>
  69. </row>
  70. </tbody>
  71. </tgroup>
  72. </table>
  73. <para>
  74. 日付文字列の書式を明示的に指定したい場合のために、次のような書式トークン指定子をサポートしています。
  75. <classname>Zend_Locale_Format</classname> のメソッドでユーザ定義書式に対応しているものの中で無効な書式指定子を使用した場合、
  76. たとえば <acronym>ISO</acronym> モードで <acronym>PHP</acronym> の 'i' 指定子を使用した場合などには例外がスローされます。
  77. </para>
  78. <para>
  79. これらの (以下の) 指定子は、<classname>Zend_Date</classname> の <methodname>toString()</methodname> がサポートしている完全な "<acronym>ISO</acronym>"
  80. セットの一部です。<acronym>PHP</acronym> の <methodname>date()</methodname> 関数と同じ形式の指定子を使用したい場合は、
  81. まず最初に <methodname>setOptions(array('format_type' => 'php'))</methodname> をコールしてください。
  82. 特定のひとつの書式文字列だけを <acronym>PHP</acronym> の <methodname>date()</methodname> 互換形式から
  83. "<acronym>ISO</acronym>" 形式に変換したい場合は <methodname>convertPhpToIsoFormat()</methodname>
  84. を使用します。現時点では、分の形式のみが異なっています
  85. (<acronym>ISO</acronym> のデフォルトでは 'm'、<acronym>PHP</acronym> の date 関数では 'i' を使用します)。
  86. </para>
  87. <table id="zend.locale.date.normalize.table-2">
  88. <title>返り値</title>
  89. <tgroup cols='5'>
  90. <thead>
  91. <row>
  92. <entry>getDate() 書式文字</entry>
  93. <entry>配列のキー</entry>
  94. <entry>返される値</entry>
  95. <entry>最小値</entry>
  96. <entry>最大値</entry>
  97. </row>
  98. </thead>
  99. <tbody>
  100. <row>
  101. <entry>d</entry>
  102. <entry>day</entry>
  103. <entry>integer</entry>
  104. <entry>1</entry>
  105. <entry>31</entry>
  106. </row>
  107. <row>
  108. <entry>M</entry>
  109. <entry>month</entry>
  110. <entry>integer</entry>
  111. <entry>1</entry>
  112. <entry>12</entry>
  113. </row>
  114. <row>
  115. <entry>y</entry>
  116. <entry>year</entry>
  117. <entry>integer</entry>
  118. <entry>無制限</entry>
  119. <entry>PHP の整数型の最大値</entry>
  120. </row>
  121. <row>
  122. <entry>h</entry>
  123. <entry>hour</entry>
  124. <entry>integer</entry>
  125. <entry>0</entry>
  126. <entry>PHP の整数型の最大値</entry>
  127. </row>
  128. <row>
  129. <entry>m</entry>
  130. <entry>minute</entry>
  131. <entry>integer</entry>
  132. <entry>0</entry>
  133. <entry>PHP の整数型の最大値</entry>
  134. </row>
  135. <row>
  136. <entry>s</entry>
  137. <entry>second</entry>
  138. <entry>integer</entry>
  139. <entry>0</entry>
  140. <entry>PHP の整数型の最大値</entry>
  141. </row>
  142. </tbody>
  143. </tgroup>
  144. </table>
  145. <example id="zend.locale.date.normalize.example-1">
  146. <title>日付の正規化</title>
  147. <programlisting language="php"><![CDATA[
  148. $dateString = Zend_Locale_Format::getDate('13.04.2006',
  149. array('date_format' =>
  150. 'dd.MM.yyyy')
  151. );
  152. // この日付に対応する Zend_Date オブジェクトを作成します
  153. $dateObject = Zend_Date('13.04.2006',
  154. array('date_format' => 'dd.MM.yyyy'));
  155. print_r($dateString); // 出力は、このようになります
  156. Array
  157. (
  158. [format] => dd.MM.yyyy
  159. [day] => 13
  160. [month] => 4
  161. [year] => 2006
  162. )
  163. // 入力値に何らかの問題がある場合に、自動的に修正します
  164. $date = Zend_Locale_Format::getDate('04.13.2006',
  165. array('date_format' => 'dd.MM.yyyy',
  166. 'fix_date' => true)
  167. );
  168. print_r($date); // 出力は、このようになります
  169. Array
  170. (
  171. [format] => dd.MM.yyyy
  172. [day] => 13
  173. [month] => 4
  174. [year] => 2006
  175. [fixed] => 4
  176. )
  177. ]]></programlisting>
  178. </example>
  179. <para>
  180. <methodname>getDate()</methodname> は "ロケール対応" なので、
  181. <code>$locale</code> を指定することで
  182. そのロケールの書式の日付文字列を使用できます。
  183. オプション '<property>fix_date</property>' は、簡単なテストを行って
  184. 日付や月の妥当性を確認します。問題が見つかったら、
  185. 経験則によってそれを何とか修正しようとします。
  186. '<constant>Zend_Locale_Format::STANDARD</constant>' を '<property>date_format</property>'
  187. の値として指定すると、<methodname>setOptions()</methodname>
  188. で設定されているクラス全体のデフォルト日付書式を使用しないようにできます。
  189. これにより、getDate は <code>$locale</code>
  190. のデフォルトの日付書式を使用するようになります。
  191. </para>
  192. <example id="zend.locale.date.normalize.example-2">
  193. <title>ロケールを指定した日付の正規化</title>
  194. <programlisting language="php"><![CDATA[
  195. $locale = new Zend_Locale('de_AT');
  196. $date = Zend_Locale_Format::getDate('13.04.2006',
  197. array('date_format' =>
  198. Zend_Locale_Format::STANDARD,
  199. 'locale' => $locale)
  200. );
  201. print_r ($date);
  202. ]]></programlisting>
  203. </example>
  204. <para>
  205. 入力に日付と時刻の両方が含まれている場合に、
  206. 完全な日付と時刻が返されます。
  207. </para>
  208. <example id="zend.locale.date.normalize.example-3">
  209. <title>日付と時刻の正規化</title>
  210. <programlisting language="php"><![CDATA[
  211. $locale = new Zend_Locale('de_AT');
  212. $date = Zend_Locale_Format::getDate('13.04.2005 22:14:55',
  213. array('date_format' =>
  214. Zend_Locale_Format::STANDARD,
  215. 'locale' => $locale)
  216. );
  217. print_r ($date);
  218. ]]></programlisting>
  219. </example>
  220. <para>
  221. 書式を指定したい場合は、
  222. <code>$locale</code> ではなく <code>$format</code> 引数を指定します。
  223. <code>$format</code> としてサポートされるのは、一文字のコード
  224. (H, m, s, y, M, d) および MMMM、EEEE だけです。
  225. </para>
  226. <example id="zend.locale.date.normalize.example-4">
  227. <title>ユーザ定義の日付書式への正規化</title>
  228. <programlisting language="php"><![CDATA[
  229. $date = Zend_Locale_Format::getDate('13200504T551422',
  230. array('date_format' =>
  231. 'ddyyyyMM ssmmHH')
  232. );
  233. print_r ($date);
  234. ]]></programlisting>
  235. </example>
  236. <para>
  237. 書式には次の記号が使用できます。
  238. </para>
  239. <table id="zend.locale.date.normalize.table-3">
  240. <title>書式定義</title>
  241. <tgroup cols='2'>
  242. <thead>
  243. <row>
  244. <entry>書式指定文字</entry>
  245. <entry>説明</entry>
  246. </row>
  247. </thead>
  248. <tbody>
  249. <row>
  250. <entry>d あるいは dd</entry>
  251. <entry>1 桁あるいは 2 桁の日</entry>
  252. </row>
  253. <row>
  254. <entry>M あるいは MM</entry>
  255. <entry>1 桁あるいは 2 桁の月</entry>
  256. </row>
  257. <row>
  258. <entry>y あるいは yy</entry>
  259. <entry>1 桁あるいは 2 桁の年</entry>
  260. </row>
  261. <row>
  262. <entry>yyyy</entry>
  263. <entry>4 桁の年</entry>
  264. </row>
  265. <row>
  266. <entry>h</entry>
  267. <entry>1 桁あるいは 2 桁の時</entry>
  268. </row>
  269. <row>
  270. <entry>m</entry>
  271. <entry>1 桁あるいは 2 桁の分</entry>
  272. </row>
  273. <row>
  274. <entry>s</entry>
  275. <entry>1 桁あるいは 2 桁の秒</entry>
  276. </row>
  277. </tbody>
  278. </tgroup>
  279. </table>
  280. <para>
  281. 適切な書式の例は、次のようになります。
  282. </para>
  283. <table id="zend.locale.date.normalize.table-4">
  284. <title>書式の例</title>
  285. <tgroup cols='3'>
  286. <thead>
  287. <row>
  288. <entry>書式</entry>
  289. <entry>入力</entry>
  290. <entry>出力</entry>
  291. </row>
  292. </thead>
  293. <tbody>
  294. <row>
  295. <entry>dd.MM.yy</entry>
  296. <entry>1.4.6</entry>
  297. <entry>['day'] => 1,
  298. ['month'] => 4,
  299. ['year'] => 6</entry>
  300. </row>
  301. <row>
  302. <entry>dd.MM.yy</entry>
  303. <entry>01.04.2006</entry>
  304. <entry>['day'] => 1, ['month'] => 4, ['year'] => 2006</entry>
  305. </row>
  306. <row>
  307. <entry>yyyyMMdd</entry>
  308. <entry>1.4.6</entry>
  309. <entry>['day'] => 6, ['month'] => 4, ['year'] => 1</entry>
  310. </row>
  311. </tbody>
  312. </tgroup>
  313. </table>
  314. <note>
  315. <title>データベースの日付書式</title>
  316. <para>
  317. データベース (たとえば MySql や MsSql など) の日付値をパースするには、
  318. getDate() のかわりに <classname>Zend_Date</classname> の ISO_8601 書式を使用します。
  319. </para>
  320. </note>
  321. <para>
  322. オプション '<property>fix_date</property>' は、簡単なテストを行って
  323. 日付や月の妥当性を確認します。問題が見つかったら、
  324. 経験則によってそれを何とか修正しようとします。
  325. <methodname>getDate()</methodname> は、
  326. 入力に問題がある場合にそれを自動で検出し、訂正します。
  327. たとえば年の位置が違っている場合などがこれにあたります。
  328. </para>
  329. <example id="zend.locale.date.normalize.example-5">
  330. <title>入力された日付の訂正処理</title>
  331. <programlisting language="php"><![CDATA[
  332. $date = Zend_Locale_Format::getDate('41.10.20',
  333. array('date_format' => 'ddMMyy',
  334. 'fix_date' => true)
  335. );
  336. // 41 は、日付ではなく年として返されます
  337. print_r ($date);
  338. ]]></programlisting>
  339. </example>
  340. </sect2>
  341. <sect2 id="zend.locale.date.test">
  342. <title>日付かどうかの確認</title>
  343. <para>
  344. <methodname>checkDateFormat($inputString, array('date_format' => $format, $locale))</methodname> を使用して、
  345. 指定した文字列が期待通りの日付を含むかどうかを調べます。
  346. <methodname>checkDateFormat()</methodname> メソッドは <methodname>getDate()</methodname> を使用します。
  347. しかし、オプション <code>'fixdate'</code> は使用しません。これを使用すると、
  348. 入力が日付書式を満たしていない場合にも <constant>TRUE</constant> を返してしまうからです。
  349. 月と日の位置が入れ替わっているなどのエラーが入力で検出された場合、
  350. オプション <code>'fixdate'</code> は
  351. 経験則によってそれを "修正" してから検証します。
  352. </para>
  353. <example id="zend.locale.date.test.example-1">
  354. <title>日付かどうかの確認</title>
  355. <programlisting language="php"><![CDATA[
  356. $locale = new Zend_Locale('de_AT');
  357. if (Zend_Locale_Format::checkDateFormat('13.Apr.2006',
  358. array('date_format' =>
  359. Zend_Locale_Format::STANDARD,
  360. $locale)
  361. ) {
  362. print "日付です";
  363. } else {
  364. print "日付ではありません";
  365. }
  366. ]]></programlisting>
  367. </example>
  368. </sect2>
  369. <sect2 id="zend.locale.time.normalizing">
  370. <title>時刻の正規化</title>
  371. <para>
  372. 通常、入力に日付と時刻が両方含まれている場合は、
  373. 時刻は日付とともに返されます。
  374. 適切な書式はわからないが、ユーザの入力のロケールがわかるという場合に
  375. <methodname>getTime()</methodname> を使用します。
  376. これは、選択したロケールに対するデフォルトの時刻書式を使用するからです。
  377. </para>
  378. <example id="zend.locale.time.normalizing.example-1">
  379. <title>未知の時刻の正規化</title>
  380. <programlisting language="php"><![CDATA[
  381. $locale = new Zend_Locale('de_AT');
  382. if (Zend_Locale_Format::getTime('13:44:42',
  383. array('date_format' =>
  384. Zend_Locale_Format::STANDARD,
  385. 'locale' => $locale)) {
  386. print "時刻です";
  387. } else {
  388. print "時刻ではありません";
  389. }
  390. ]]></programlisting>
  391. </example>
  392. </sect2>
  393. <sect2 id="zend.locale.time.test">
  394. <title>時刻かどうかの確認</title>
  395. <para>
  396. <methodname>checkDateFormat()</methodname> を使用して、
  397. 指定した文字列が有効な時刻を含むかどうかを調べます。
  398. 使用法は、日付の確認の場合と同じです。ただし、
  399. <property>date_format</property> には期待するパーツを指定しなければなりません。
  400. </para>
  401. <example id="zend.locale.time.test.example-1">
  402. <title>時刻かどうかの確認</title>
  403. <programlisting language="php"><![CDATA[
  404. $locale = new Zend_Locale('de_AT');
  405. if (Zend_Locale_Format::checkDateFormat('13:44:42',
  406. array('date_format' => 'HH:mm:ss',
  407. 'locale' => $locale)) {
  408. print "時刻です";
  409. } else {
  410. print "時刻ではありません";
  411. }
  412. ]]></programlisting>
  413. </example>
  414. </sect2>
  415. </sect1>
  416. <!--
  417. vim:se ts=4 sw=4 et:
  418. -->