Time::Piece::MySQL とタイムゾーン (訂正あり) - Yet Another Hackadelic
Unix timeã«ã¿ã¤ã ã¾ã¼ã³ã®æ¦å¿µãæã¡è¾¼ãã®ã¯æ°æã¡æªãã§ãã
å¾ãããUnix timeã«å¯¾ãã¦ã¿ã¤ã ã¾ã¼ã³ã«å¯¾å¿ããç®çã§æ°å¤ãå ç®æ¸ç®ãã¦ã¯ãããªãã¨æãã¾ãã
ä½æ
ãªãUnix timeã¯ãUTCã«ããã1970/1/1ããã®çµéç§æ°ãã ããã§ãã
ã¿ã¤ã ã¾ã¼ã³ãJSTã«ãªã£ã¦ãã¦ãNOW()ã¯JSTã«ãããæå»ãè¿ãã¦ããã¨ããåæã§è©±ãé²ãã¾ããã
ï¼å½ç¶ã®ãã¨ãªãããUNIX_TIMESTAMP()ã¯UTCã«ãããæéãè¿ãã¦ããï¼
ä½ã話ãããããããã¦ãããã¨ããã¨ã
$ perl -MTime::Piece -MTime::Piece::MySQL -e 'my $t = Time::Piece->from_mysql_datetime("2010-11-05 12:12:47"); warn $t;' Fri Nov 5 12:12:47 2010 at -e line 1.ããã¯æ£ãããã§ããã©ã
ãã®åºçºç¹ããæ¢ã«æ£ãããªããã§ãããããä¸è¦æ£ããããã«è¦ãããã§ããã©ã
ããå®ã¯ãFri Nov 5 12:12:47 2010 UTCããåºåããã¦ãã¾ãã
ã¿ã¤ã ã¾ã¼ã³æ
å ±ãç¡ãã®ã§Time::Pieceã¯æ¸¡ããããã®ããFri Nov 5 12:12:47 2010 UTCãã ã¨æã£ã¦ãã¾ãã
ããå®ã¯
ã®ã§epochãï¼æéåãããï¼ããã«è¦ããï¼å¤ãè¿ãã¦ãã¾ãã
ãã®å¾ã¯é£éçã«è©±ããããããªã£ã¦ãã ããªã®ã§å²æãã¾ãã
æ®æ®µTime::Piece使ã£ã¦ãªãã®ã§è¯ãåãããªããã§ããã軽ãã½ã¼ã¹ãè¦ãæãc_islocalã¨ããã®ã$ENV{TZ}ãè¦ãããã®ãã©ã°ã£ã½ãã®ã§ã
% env TZ=JST-9 perl -MTime::Piece -MTime::Piece::MySQL -e 'my $t = Time::Piece->from_mysql_datetime("2010-11-05 12:12:47"); $t->[10] = 1; warn $t->epoch;' 1288926767 at -e line 1.
ã¨ããã¨"2010-11-05 12:12:47"ãJSTã ã¨æã£ã¦ããã¦ãæ£ããUnix timeãåºãã¦ããã¾ãã
ãã¡ããTZãå¤ããã°ã¡ããã¨Unix timeãå¤ãã£ã¦ããã¾ãã
ï¼$t->[10]ã¨ã$t->tzoffsetã¨ããåºåãã¦ã¿ãã¨ä½ã¨ãªããããããï¼
ãã ãæååå¤æï¼warn $tï¼ããã¨ãã«TZã«å¿ãã¦å¤ãã£ã¦ãããªãã£ã½ãã®ã§ãTime::Pieceã¯ã¤ã±ããªãã©ã¤ãã©ãªã ã¨æãã¾ãã
ï¼Time::Pieceãè¯ãåãã£ã¦ãªãã®ã§ãããããããã¡ããã¨ã¿ã¤ã ã¾ã¼ã³ãæ±ãæ¹æ³ãããã®ããç¥ãã¾ããï¼
ãªãå人çã«ã¯ãæéãæ±ãã©ã¤ãã©ãªã¯parseæã«ã¿ã¤ã ã¾ã¼ã³ãæå®ã§ãããããã¦ããã¦æ¬²ãããªã¼ã¨æãã¾ãããã®æ¹ãæ··ä¹±ãå°ãªããã¨ã
ããããã¿ã¤ã ã¾ã¼ã³å¨ãã¯perlã®UNICODEã®æ±ãï¼åºæ¬çã«å
¨ã¦utf8 flaggedã§æ±ã£ã¦ãåºåããã¨ãã«encodeããçãªè©±ï¼ã¨åæ§ã«ãåºæ¬çã«å
¨ã¦UTCã§ããåããã¦ãå¾ã«åºåããæã ãé©åãªã¿ã¤ã ã¾ã¼ã³ãè¨å®ãã¦åºãããã«ããã°ãè²ã
ãªæ©ã¿ã¯ç¡ããªããã§ã¯ãªãããªã¼ã¨æãã¾ãã
id:kits perl -MTime::Piece -le '$t=Time::Piece->strptime("2010-11-05 12:12:47 +0900","%Y-%m-%d %H:%M:%S %z"); print $t->epoch' ã®ããã«ããã°ããããã«æãããã試ãã¦ãã¡ã ã£ãã®ã«ãããªãã«ãªâ¦ã¨æã£ã¦ããä¸åº¦è©¦ãã¦ã¿ããTime::Pieceã¢ãããã¼ãããã確ãã«âã®éãã ã£ãã
% perl-version Time::Piece Time::Piece = 1.15 % perl -MTime::Piece -le '$t=Time::Piece->strptime("2010-11-05 12:12:47 +0900","%Y-%m-%d %H:%M:%S %z"); print $t->epoch' Error parsing time at /usr/lib/perl5/5.10/i686-cygwin/Time/Piece.pm line 470. % perl-version Time::Piece Time::Piece = 1.20 % perl -MTime::Piece -le '$t=Time::Piece->strptime("2010-11-05 12:12:47 +0900","%Y-%m-%d %H:%M:%S %z"); print $t->epoch' 1288926767