2002年04月06日(Sat) 天気はいいけど気温がさがってきた [長年日記]
● タイムゾーンすげかえスクリプト
ローカルタイムで、 タイムゾーンを変えても時刻が一緒に見えるスクリプトを、 書いてみました。 (rd2に通すとマニュアルをHTMLで作ってくれます。カッコイイ! tDiaryのパラグラフが変わっちゃわないように、 空行には一個ずつ空白が入れてあります。)
#!/usr/bin/ruby
=begin
= Time zone manupulation extension to class Time
== additional class methods
=end
class Time
=begin
--- Time.with_tz ( dst_tz )
Returns a ((|Time|)) in timezone ((|dst_tz|)) (specified by the
normal tiemzone, i.e. not (({'CEST'})) but (({'CET'}))) with a
certain time offset so that local time is the same as the original
((|Time|)) except for timezone.
Note: in case if the offsetted time falls between the end of normal
time and the start of summer time (daylight savings time), the
result seems to be in summer time and 1 hour later than expected.
=end
def with_tz (dst_tz)
cur_tz=ENV['TZ'] # preserve the original timezone
ENV['TZ']=dst_tz # let's be in the destination timezone
result = Time::local *(self.to_a[0..7]+[false,dst_tz])
ENV['TZ']=cur_tz # return to the original timezone
result # a Time holds timezone where it was created in
end
=begin
== Changelog
* Apr 8 2002 zunda <zunda @ freeshell.org>
* first release
== Copyright
Copyright (C) 2002 zunda <zunda @ freeshell.org>
Permission is granted, under the terms of GPL, for use, copying,
modification, distribution, and distribution of modified versions of
this work as long as the above copyright notice is included.
=end
end
# test script
srctz='Japan'
dsttz='CET'
ENV['TZ']=srctz
[
Time::local (2002,"mar",31,1,15,1),
Time::local (2002,"mar",31,2,15,1),
Time::local (2002,"mar",31,3,15,1),
Time::local (2002,"oct",27,1,15,1),
Time::local (2002,"oct",27,2,15,1),
Time::local (2002,"oct",27,3,15,1),
].each { |time|
puts "#{time.to_s} -> #{time.with_tz(dsttz).to_s}"
}
こーたっくさんのタイムゾーンを指定して現在時刻を得る という動作も、
などとすれば実現できると思うのですが(冬時間のタイムゾーン名を使ってください)、 もっと簡単には、たぶん、tdiary.rbの起動時に、
でもいいかも。これで、システムのタイムゾーンではなく、
環境変数TZで指定したタイムゾーンで、
時刻が表現されると思います。
僕は、.with_tz()を使って、
日記データのタイムゾーンの変換に挑戦してみようと思っています。
ちなみに、日本標準時JSTはISOだかなんだったかで定義されていないようで、
JSTにしたいときは、
TZにはJapanと入れなくちゃいけません。変なの。
● sourceforge.jpもむずかしい
アカウントはつくってみたのですが…
本日のツッコミ(全2件) [ツッコミを入れる]
最近のツッコまれどころ