おまぬけ活動日誌

最近のツッコまれどころ

この日誌から Google してもらう


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件) [ツッコミを入れる]
> famao (2002年04月06日(Sat) 11:40)

souceforge.jp...。むずいですね。なんとなくzundaさんはiiimf-skkのプロジェクトに拉致ったあげく、とりあえずドキュメントリーダだかそういうのに勝手に指名しときました(ぉ。ついでに0.1.20もリリースしときましたので、バグみつけたらもりもり報告してください。

> ずんだあん (2002年04月07日(Sun) 20:21)

むむ。拉致られている:)そーかー。自分からプロジェクトに入りたいーとか言うんじゃないんだ。<br>新規プロジェクトなら勝手に作れちゃうというのが不思議。


作り手とその取り巻きだけが楽しんでる間は本物じゃない。その中身が理解できない人々の生活を変えてこそ本物だ


zunda <zunda at freeshell.org>