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