ます’s Blog - どうでもいい記事100選

どうでもいい記事100選

date関数のフォーマット・オプション「y」とか「h」とか

今日はちょっとムシャクシャ系。

「Y」と入力したつもりが「y」になっていたとか、「H」と入力したつもりが「h」になっていたとか、ちょっと考えられない。。。まず確認しろよ!
そんなに頻繁に使うなら定数で予め定義しておけよ。。。午前中にテストしてたから気がつかなかったとか、お粗末すぎる。それ以前に「y」だと桁数が違ってますから!


そんなクソ野郎には、こんなパッチがお似合いカモね。一生守られてろ!って感じ。
どこまでフォローしてもらったら気が済むんだろうか。その割には言い訳だけは一人前だし。プンプン。

--- php-4.4.9,orig/ext/standard/datetime.c	2007-12-31 16:22:52.000000000 +0900
+++ php-4.4.9/ext/standard/datetime.c	2009-06-24 15:26:05.000000000 +0900
@@ -376,6 +376,7 @@
 			case 'O':		/* GMT offset in [+-]HHMM format */
 				size += 5;
 				break;
+			case 'y':		/* year, numeric, 4 digits */
 			case 'Y':		/* year, numeric, 4 digits */
 				size += 4;
 				break;
@@ -385,7 +386,6 @@
 			case 'B':		/* Swatch Beat, 3 digits */
 				size += 3;
 				break;
-			case 'y':		/* year, numeric, 2 digits */
 			case 'm':		/* month, numeric */
 			case 'n':		/* month, numeric, no leading zeroes */
 			case 'd':		/* day of the month, numeric */
@@ -442,6 +442,7 @@
 			case 'l':		/* day (of the week), textual, full */
 				strcat(Z_STRVAL_P(return_value), day_full_names[ta->tm_wday]);
 				break;
+			case 'y':		/* year, numeric, 4 digits */
 			case 'Y':		/* year, numeric, 4 digits */
 				sprintf(tmp_buff, "%d", ta->tm_year + YEAR_BASE);  /* SAFE */
 				strcat(Z_STRVAL_P(return_value), tmp_buff);
@@ -456,10 +457,6 @@
 				sprintf(tmp_buff, "%d", ta->tm_yday);  /* SAFE */
 				strcat(Z_STRVAL_P(return_value), tmp_buff);
 				break;
-			case 'y':		/* year, numeric, 2 digits */
-				sprintf(tmp_buff, "%02d", ((ta->tm_year)%100));  /* SAFE */
-				strcat(Z_STRVAL_P(return_value), tmp_buff);
-				break;
 			case 'm':		/* month, numeric */
 				sprintf(tmp_buff, "%02d", ta->tm_mon + 1);  /* SAFE */
 				strcat(Z_STRVAL_P(return_value), tmp_buff);
@@ -476,15 +473,11 @@
 				sprintf(tmp_buff, "%d", ta->tm_mday); /* SAFE */
 				strcat(Z_STRVAL_P(return_value), tmp_buff);
 				break;
+			case 'h':		/* hour, numeric, 24 hour format */
 			case 'H':		/* hour, numeric, 24 hour format */
 				sprintf(tmp_buff, "%02d", ta->tm_hour);  /* SAFE */
 				strcat(Z_STRVAL_P(return_value), tmp_buff);
 				break;
-			case 'h':		/* hour, numeric, 12 hour format */
-				h = ta->tm_hour % 12; if (h==0) h = 12;
-				sprintf(tmp_buff, "%02d", h);  /* SAFE */
-				strcat(Z_STRVAL_P(return_value), tmp_buff);
-				break;
 			case 'G':      /* hour, numeric, 24 hour format, no leading zeros */
 				sprintf(tmp_buff, "%d", ta->tm_hour);  /* SAFE */
 				strcat(Z_STRVAL_P(return_value), tmp_buff);
--- php-5.2.10,orig/ext/date/php_date.c	2009-06-06 07:34:30.000000000 +0900
+++ php-5.2.10/ext/date/php_date.c	2009-06-24 15:20:41.000000000 +0900
@@ -797,7 +797,7 @@
 
 			/* year */
 			case 'L': length = slprintf(buffer, 32, "%d", timelib_is_leap((int) t->y)); break;
-			case 'y': length = slprintf(buffer, 32, "%02d", (int) t->y % 100); break;
+			case 'y':
 			case 'Y': length = slprintf(buffer, 32, "%s%04ld", t->y < 0 ? "-" : "", llabs(t->y)); break;
 
 			/* time */
@@ -814,7 +814,7 @@
 			}
 			case 'g': length = slprintf(buffer, 32, "%d", (t->h % 12) ? (int) t->h % 12 : 12); break;
 			case 'G': length = slprintf(buffer, 32, "%d", (int) t->h); break;
-			case 'h': length = slprintf(buffer, 32, "%02d", (t->h % 12) ? (int) t->h % 12 : 12); break;
+			case 'h':
 			case 'H': length = slprintf(buffer, 32, "%02d", (int) t->h); break;
 			case 'i': length = slprintf(buffer, 32, "%02d", (int) t->i); break;
 			case 's': length = slprintf(buffer, 32, "%02d", (int) t->s); break;
--- php-5.3.0RC4,orig/ext/date/php_date.c	2009-06-18 02:56:17.000000000 +0900
+++ php-5.3.0RC4/ext/date/php_date.c	2009-06-24 15:22:09.000000000 +0900
@@ -1049,7 +1049,7 @@
 
 			/* year */
 			case 'L': length = slprintf(buffer, 32, "%d", timelib_is_leap((int) t->y)); break;
-			case 'y': length = slprintf(buffer, 32, "%02d", (int) t->y % 100); break;
+			case 'y':
 			case 'Y': length = slprintf(buffer, 32, "%s%04ld", t->y < 0 ? "-" : "", llabs((timelib_sll) t->y)); break;
 
 			/* time */
@@ -1066,7 +1066,7 @@
 			}
 			case 'g': length = slprintf(buffer, 32, "%d", (t->h % 12) ? (int) t->h % 12 : 12); break;
 			case 'G': length = slprintf(buffer, 32, "%d", (int) t->h); break;
-			case 'h': length = slprintf(buffer, 32, "%02d", (t->h % 12) ? (int) t->h % 12 : 12); break;
+			case 'h':
 			case 'H': length = slprintf(buffer, 32, "%02d", (int) t->h); break;
 			case 'i': length = slprintf(buffer, 32, "%02d", (int) t->i); break;
 			case 's': length = slprintf(buffer, 32, "%02d", (int) t->s); break;

gist.githubにも登録しておきました。

結果は以下の通り。

% /usr/local/src/php-4.4.9,orig/sapi/cli/php -r 'var_dump( date( "y/m/d h:i:s" ) );'
string(17) "09/06/24 04:55:19"
% /usr/local/src/php-4.4.9/sapi/cli/php -r 'var_dump( date( "y/m/d h:i:s" ) );'
string(19) "2009/06/24 16:55:22"
% /usr/local/src/php-5.2.10,orig/sapi/cli/php -ddate.timezone='Asia/Tokyo' -r 'var_dump( date( "y/m/d h:i:s" ) );'
string(17) "09/06/24 04:55:25"
% /usr/local/src/php-5.2.10/sapi/cli/php -ddate.timezone='Asia/Tokyo' -r 'var_dump( date( "y/m/d h:i:s" ) );'
string(19) "2009/06/24 16:55:27"
% /usr/local/src/php-5.3.0RC4,orig/sapi/cli/php -ddate.timezone='Asia/Tokyo' -r 'var_dump( date( "y/m/d h:i:s" ) );'
string(17) "09/06/24 04:55:28"
% /usr/local/src/php-5.3.0RC4/sapi/cli/php -ddate.timezone='Asia/Tokyo' -r 'var_dump( date( "y/m/d h:i:s" ) );'
string(19) "2009/06/24 16:55:30"
%

挙動を変えずにワーニングを発生させるのもアリだな。それでいこう。
ただ、こんなのと同列と思われているのか。。。と思うと非常に切ない。