Time-Piece-1.08 > Time::Piece

名前

Time::Piece - オブジェクト指向な時間オブジェクト

概要

    use Time::Piece;
    
    my $t = localtime;
    print "Time is $t\n";
    print "Year is ", $t->year, "\n";

説明

This module replaces the standard localtime and gmtime functions with implementations that return objects. It does so in a backwards compatible manner, so that using localtime/gmtime in the way documented in perlfunc will still return what you expect.

このモジュールは、標準の localtime と gmtime 関数を、オブジェクトを返す実装のものに置き換えます。 後方互換性があるので、perlfunc に書かれているように、localtime/gmtime を使っても、期待通りのものを返します。

The module actually implements most of an interface described by Larry Wall on the perl5-porters mailing list here: http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2000-01/msg00241.html

このモジュールは、実際に、Larry Wall が、perl5-portersメーリングリストに書いた、 ほとんどのインターフェースを実装します: http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2000-01/msg00241.html

使い方

After importing this module, when you use localtime or gmtime in a scalar context, rather than getting an ordinary scalar string representing the date and time, you get a Time::Piece object, whose stringification happens to produce the same effect as the localtime and gmtime functions. There is also a new() constructor provided, which is the same as localtime(), except when passed a Time::Piece object, in which case it's a copy constructor. The following methods are available on the object:

このモジュールをインポートした後は、スカラコンテキストで、localtime か、gmtime を使うと、 日付と時間を表現する普通のスカラ文字列ではなくて、Time::Pieceオブジェクトを得ます。 Time::Pieceオブジェクトの stringification は、localtime と gmtime たまたま関数と同じ効果があります。 new() コンストラクタもありますが、Time::Pieceオブジェクトが渡されない限りlocaltime()と同じです。 Time::Pieceオブジェクトが渡されると、コピーするコンストラクタになります。 下記のメソッドがオブジェクトで利用できます:

    $t->sec                 # $t->second も可
    $t->min                 # $t->minute も可
    $t->hour                # 24時制
    $t->mday                # $t->day_of_month も可
    $t->mon                 # 1 = 一月
    $t->_mon                # 0 = 一月
    $t->monname             # Feb
    $t->month               # $t->monname と同じ
    $t->fullmonth           # February
    $t->year                # 0始まり(AD0年は、もちろん、BC1年)
    $t->_year               # 年から1900引いたもの
    $t->yy                  # 2桁の年
    $t->wday                # 1 = 日曜
    $t->_wday               # 0 = 日曜
    $t->day_of_week         # 0 = 日曜
    $t->wdayname            # Tue
    $t->day                 # wdayname と同じ
    $t->fullday             # Tuesday
    $t->yday                # $t->day_of_year も可, 0 = Jan 01
    $t->isdst               # $t->daylight_savings も可。

    $t->hms                 # 12:34:56
    $t->hms(".")            # 12.34.56
    $t->time                # $t->hms と同じ

    $t->ymd                 # 2000-02-29
    $t->date                # $t->ymd と同じ
    $t->mdy                 # 02-29-2000
    $t->mdy("/")            # 02/29/2000
    $t->dmy                 # 29-02-2000
    $t->dmy(".")            # 29.02.2000
    $t->datetime            # 2000-02-29T12:34:56 (ISO 8601)
    $t->cdate               # Tue Feb 29 12:34:56 2000
    "$t"                    # $t->cdate と同じ

    $t->epoch               # epochからの秒
    $t->tzoffset            # timezone offset in a Time::Seconds object

    $t->julian_day          # ユリウス歴が始まってからの日の数
    $t->mjd                 # modified Julian date (JD-2400000.5 days)

    $t->week                # 週番号 (ISO 8601)

    $t->is_leap_year        # 閏年なら真を返す
    $t->month_last_day      # 28-31

    $t->time_separator($s)  # デフォルトのセパレータをセットします (default ":")
    $t->date_separator($s)  # デフォルトのセパレータをセットします (default "-")
    $t->day_list(@days)     # デフォルトの曜日をセットします
    $t->mon_list(@days)     # デフォルトの月名をセットします

    $t->strftime(FORMAT)    # POSIX::strftime と同じ(without the overhead
                            # of the full POSIX extension)
    $t->strftime()          # "Tue, 29 Feb 2000 12:34:56 GMT"
    
    Time::Piece->strptime(STRING, FORMAT)
                            # strptime man ページを見て下さい
                            # 新しく、Time::Piece object を作ります

Local Locales

Both wdayname (day) and monname (month) allow passing in a list to use to index the name of the days against. This can be useful if you need to implement some form of localisation without actually installing or using locales.

曜日(day)と月名(month)の両方ともに、曜日のインデックスに使うリストに渡すことを 許しています。実際にインストールしたり、ロケールを使わずに、 なんらかのローカライゼーションを実装する必要があれば、有益でしょう。

  my @days = qw( Dimanche Lundi Merdi Mercredi Jeudi Vendredi Samedi );

  my $french_day = localtime->day(@days);

These settings can be overriden globally too:

これらの設定はグローバルにもオーバーライドできます:

  Time::Piece::day_list(@days);

Or for months:

また、月なら:

  Time::Piece::mon_list(@months);

And locally for months:

ローカルにするなら:

  print localtime->month(@months);

日付の演算

It's possible to use simple addition and subtraction of objects:

単純なオブジェクトの足し算、引き算ができます:

    use Time::Seconds;
    
    my $seconds = $t1 - $t2;
    $t1 += ONE_DAY; # add 1 day (constant from Time::Seconds)

The following are valid ($t1 and $t2 are Time::Piece objects):

下記のようにx使えます($t と $t2 はTime::Piece オブジェクト):

    $t1 - $t2; # returns Time::Seconds object
    $t1 - 42; # returns Time::Piece object
    $t1 + 533; # returns Time::Piece object

However adding a Time::Piece object to another Time::Piece object will cause a runtime error.

ですが、Time::Piece オブジェクトに、別のTime::Piece オブジェクトを 足すのは、実行時エラーを引き起こします。

Note that the first of the above returns a Time::Seconds object, so while examining the object will print the number of seconds (because of the overloading), you can also get the number of minutes, hours, days, weeks and years in that delta, using the Time::Seconds API.

上の例の最初のものは、Time::Seconds オブジェクトを返すことに注意して下さい。 オブジェクトを試験することで秒数を出力(オーバーロードによって)する一方で、 Time::Seconds のAPIを使って、その差異から、分数、時間数、日数、週の数、年数を、 得ることが出来ます。

日付の比較

Date comparisons are also possible, using the full suite of "<", ">", "<=", ">=", "<=>", "==" and "!=".

日付の比較もまた可能です。 "<"、 ">"、"<="、 ">="、 "<=>"、 "=="、 "!=" の全てが使えます。

日付の解析

Time::Piece links to your C library's strptime() function, allowing you incredibly flexible date parsing routines. For example:

Time::Piece は、C ライブラリの strptime()関数にリンクしているので、 信じられない程柔軟に日付を解析するルーチンを使えます。たとえば:

  my $t = Time::Piece->strptime("Sun 3rd Nov, 1943",
                                "%A %drd %b, %Y");
  
  print $t->strftime("%a, %d %b %Y");

次のように出力します:

  Wed, 03 Nov 1943

(see, it's even smart enough to fix my obvious date bug)

(見て下さい、一目瞭然な日付のバグを直すのに十分賢い)

For more information see "man strptime", which should be on all unix systems.

もっと知りたければ、"man strptime"を見て下さい。これは、 すべての unix システムで使えます。

YYYY-MM-DDThh:mm:ss

The ISO 8601 standard defines the date format to be YYYY-MM-DD, and the time format to be hh:mm:ss (24 hour clock), and if combined, they should be concatenated with date first and with a capital 'T' in front of the time.

ISO 8601 標準は、日付のフォーマットを YYYY-MM-DDとして、定めています。 時間は、hh:mm:ss(24時)です。組み合わせる場合、 日付を最初にして、時間の前に大文字の'T'で結び付ける必要があります。

週番号

The week number may be an unknown concept to some readers. The ISO 8601 standard defines that weeks begin on a Monday and week 1 of the year is the week that includes both January 4th and the first Thursday of the year. In other words, if the first Monday of January is the 2nd, 3rd, or 4th, the preceding days of the January are part of the last week of the preceding year. Week numbers range from 1 to 53.

週番号は、読者によっては、その概念を知らないかもしれません。 ISO 8601 標準は、週は、月曜で始まり、一年のweek 1は、1月4日と、 一年の最初の木曜日の両方を含む週です。 言い換えると、1月の最初の月曜日が2日か、3日か、4日であれば、 その前の日は、昨年の最後の週の一部になります。週番号は、1から53の範囲です。

グローバルなオーバーライド

Finally, it's possible to override localtime and gmtime everywhere, by including the ':override' tag in the import list:

localtime と gmtime をどこにでもオーバーライドするのを可能にします。 インポートリストに、':override'を含めて下さい:

    use Time::Piece ':override';

著者

Matt Sergeant, matt@sergeant.org Jarkko Hietaniemi, jhi@iki.fi (while creating Time::Piece for core perl)

ライセンス

This module is free software, you may distribute it under the same terms as Perl.

SEE ALSO

The excellent Calendar FAQ at http://www.tondering.dk/claus/calendar.html

バグ

The test harness leaves much to be desired. Patches welcome.

翻訳について

翻訳者:加藤敦 (ktat.is@gmail.com)

Perlドキュメント日本語訳 Project にて、 Perlモジュール、ドキュメントの翻訳を行っております。

 http://perldocjp.sourceforge.jp/
 http://sourceforge.jp/projects/perldocjp/
 http://www.freeml.com/ctrl/html/MLInfoForm/perldocjp@freeml.com
 http://www.perldoc.jp