Zend_Locale-DatesTimes.xml 20 KB

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