Zend_Locale-DatesTimes.xml 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <!-- EN-Revision: 15103 -->
  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 role="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. ]]>
  177. </programlisting>
  178. </example>
  179. <para>
  180. <code>getDate()</code> は "ロケール対応" なので、
  181. <code>$locale</code> を指定することで
  182. そのロケールの書式の日付文字列を使用できます。
  183. オプション '<code>fix_date</code>' は、簡単なテストを行って
  184. 日付や月の妥当性を確認します。問題が見つかったら、
  185. 経験則によってそれを何とか修正しようとします。
  186. '<classname>Zend_Locale_Format::STANDARD</classname>' を '<code>date_format</code>'
  187. の値として指定すると、<code>setOptions()</code>
  188. で設定されているクラス全体のデフォルト日付書式を使用しないようにできます。
  189. これにより、getDate は <code>$locale</code>
  190. のデフォルトの日付書式を使用するようになります。
  191. </para>
  192. <example id="zend.locale.date.normalize.example-2">
  193. <title>ロケールを指定した日付の正規化</title>
  194. <programlisting role="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. ]]>
  203. </programlisting>
  204. </example>
  205. <para>
  206. 入力に日付と時刻の両方が含まれている場合に、
  207. 完全な日付と時刻が返されます。
  208. </para>
  209. <example id="zend.locale.date.normalize.example-3">
  210. <title>日付と時刻の正規化</title>
  211. <programlisting role="php"><![CDATA[
  212. $locale = new Zend_Locale('de_AT');
  213. $date = Zend_Locale_Format::getDate('13.04.2005 22:14:55',
  214. array('date_format' =>
  215. Zend_Locale_Format::STANDARD,
  216. 'locale' => $locale)
  217. );
  218. print_r ($date);
  219. ]]>
  220. </programlisting>
  221. </example>
  222. <para>
  223. 書式を指定したい場合は、
  224. <code>$locale</code> ではなく <code>$format</code> 引数を指定します。
  225. <code>$format</code> としてサポートされるのは、一文字のコード
  226. (H, m, s, y, M, d) および MMMM、EEEE だけです。
  227. </para>
  228. <example id="zend.locale.date.normalize.example-4">
  229. <title>ユーザ定義の日付書式への正規化</title>
  230. <programlisting role="php"><![CDATA[
  231. $date = Zend_Locale_Format::getDate('13200504T551422',
  232. array('date_format' =>
  233. 'ddyyyyMM ssmmHH')
  234. );
  235. print_r ($date);
  236. ]]>
  237. </programlisting>
  238. </example>
  239. <para>
  240. 書式には次の記号が使用できます。
  241. </para>
  242. <table id="zend.locale.date.normalize.table-3">
  243. <title>書式定義</title>
  244. <tgroup cols='2'>
  245. <thead>
  246. <row>
  247. <entry>書式指定文字</entry>
  248. <entry>説明</entry>
  249. </row>
  250. </thead>
  251. <tbody>
  252. <row>
  253. <entry>d あるいは dd</entry>
  254. <entry>1 桁あるいは 2 桁の日</entry>
  255. </row>
  256. <row>
  257. <entry>M あるいは MM</entry>
  258. <entry>1 桁あるいは 2 桁の月</entry>
  259. </row>
  260. <row>
  261. <entry>y あるいは yy</entry>
  262. <entry>1 桁あるいは 2 桁の年</entry>
  263. </row>
  264. <row>
  265. <entry>yyyy</entry>
  266. <entry>4 桁の年</entry>
  267. </row>
  268. <row>
  269. <entry>h</entry>
  270. <entry>1 桁あるいは 2 桁の時</entry>
  271. </row>
  272. <row>
  273. <entry>m</entry>
  274. <entry>1 桁あるいは 2 桁の分</entry>
  275. </row>
  276. <row>
  277. <entry>s</entry>
  278. <entry>1 桁あるいは 2 桁の秒</entry>
  279. </row>
  280. </tbody>
  281. </tgroup>
  282. </table>
  283. <para>
  284. 適切な書式の例は、次のようになります。
  285. </para>
  286. <table id="zend.locale.date.normalize.table-4">
  287. <title>書式の例</title>
  288. <tgroup cols='3'>
  289. <thead>
  290. <row>
  291. <entry>書式</entry>
  292. <entry>入力</entry>
  293. <entry>出力</entry>
  294. </row>
  295. </thead>
  296. <tbody>
  297. <row>
  298. <entry>dd.MM.yy</entry>
  299. <entry>1.4.6</entry>
  300. <entry>['day'] => 1,
  301. ['month'] => 4,
  302. ['year'] => 6</entry>
  303. </row>
  304. <row>
  305. <entry>dd.MM.yy</entry>
  306. <entry>01.04.2006</entry>
  307. <entry>['day'] => 1, ['month'] => 4, ['year'] => 2006</entry>
  308. </row>
  309. <row>
  310. <entry>yyyyMMdd</entry>
  311. <entry>1.4.6</entry>
  312. <entry>['day'] => 6, ['month'] => 4, ['year'] => 1</entry>
  313. </row>
  314. </tbody>
  315. </tgroup>
  316. </table>
  317. <note>
  318. <title>データベースの日付書式</title>
  319. <para>
  320. データベース (たとえば MySql や MsSql など) の日付値をパースするには、
  321. getDate() のかわりに <classname>Zend_Date</classname> の ISO_8601 書式を使用します。
  322. </para>
  323. </note>
  324. <para>
  325. オプション '<code>fix_date</code>' は、簡単なテストを行って
  326. 日付や月の妥当性を確認します。問題が見つかったら、
  327. 経験則によってそれを何とか修正しようとします。
  328. <code>getDate()</code> は、
  329. 入力に問題がある場合にそれを自動で検出し、訂正します。
  330. たとえば年の位置が違っている場合などがこれにあたります。
  331. </para>
  332. <example id="zend.locale.date.normalize.example-5">
  333. <title>入力された日付の訂正処理</title>
  334. <programlisting role="php"><![CDATA[
  335. $date = Zend_Locale_Format::getDate('41.10.20',
  336. array('date_format' => 'ddMMyy',
  337. 'fix_date' => true)
  338. );
  339. // 41 は、日付ではなく年として返されます
  340. print_r ($date);
  341. ]]>
  342. </programlisting>
  343. </example>
  344. </sect2>
  345. <sect2 id="zend.locale.date.test">
  346. <title>日付かどうかの確認</title>
  347. <para>
  348. <code>checkDateFormat($inputString, array('date_format' => $format, $locale))</code> を使用して、
  349. 指定した文字列が期待通りの日付を含むかどうかを調べます。
  350. <code>checkDateFormat()</code> メソッドは <code>getDate()</code> を使用します。
  351. しかし、オプション <code>'fixdate'</code> は使用しません。これを使用すると、
  352. 入力が日付書式を満たしていない場合にも true を返してしまうからです。
  353. 月と日の位置が入れ替わっているなどのエラーが入力で検出された場合、
  354. オプション <code>'fixdate'</code> は
  355. 経験則によってそれを "修正" してから検証します。
  356. </para>
  357. <example id="zend.locale.date.test.example-1">
  358. <title>日付かどうかの確認</title>
  359. <programlisting role="php"><![CDATA[
  360. $locale = new Zend_Locale('de_AT');
  361. if (Zend_Locale_Format::checkDateFormat('13.Apr.2006',
  362. array('date_format' =>
  363. Zend_Locale_Format::STANDARD,
  364. $locale)
  365. ) {
  366. print "日付です";
  367. } else {
  368. print "日付ではありません";
  369. }
  370. ]]>
  371. </programlisting>
  372. </example>
  373. </sect2>
  374. <sect2 id="zend.locale.time.normalizing">
  375. <title>時刻の正規化</title>
  376. <para>
  377. 通常、入力に日付と時刻が両方含まれている場合は、
  378. 時刻は日付とともに返されます。
  379. 適切な書式はわからないが、ユーザの入力のロケールがわかるという場合に
  380. <code>getTime()</code> を使用します。
  381. これは、選択したロケールに対するデフォルトの時刻書式を使用するからです。
  382. </para>
  383. <example id="zend.locale.time.normalizing.example-1">
  384. <title>未知の時刻の正規化</title>
  385. <programlisting role="php"><![CDATA[
  386. $locale = new Zend_Locale('de_AT');
  387. if (Zend_Locale_Format::getTime('13:44:42',
  388. array('date_format' =>
  389. Zend_Locale_Format::STANDARD,
  390. 'locale' => $locale)) {
  391. print "時刻です";
  392. } else {
  393. print "時刻ではありません";
  394. }
  395. ]]>
  396. </programlisting>
  397. </example>
  398. </sect2>
  399. <sect2 id="zend.locale.time.test">
  400. <title>時刻かどうかの確認</title>
  401. <para>
  402. <code><code>checkDateFormat()</code></code> を使用して、
  403. 指定した文字列が有効な時刻を含むかどうかを調べます。
  404. 使用法は、日付の確認の場合と同じです。ただし、
  405. <code>date_format</code> には期待するパーツを指定しなければなりません。
  406. </para>
  407. <example id="zend.locale.time.test.example-1">
  408. <title>時刻かどうかの確認</title>
  409. <programlisting role="php"><![CDATA[
  410. $locale = new Zend_Locale('de_AT');
  411. if (Zend_Locale_Format::checkDateFormat('13:44:42',
  412. array('date_format' => 'HH:mm:ss',
  413. 'locale' => $locale)) {
  414. print "時刻です";
  415. } else {
  416. print "時刻ではありません";
  417. }
  418. ]]>
  419. </programlisting>
  420. </example>
  421. </sect2>
  422. </sect1>
  423. <!--
  424. vim:se ts=4 sw=4 et:
  425. -->