⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timezone.pm

📁 1. 记录每个帖子的访问人情况
💻 PM
📖 第 1 页 / 共 2 页
字号:
    return $self->name;}sub STORABLE_thaw{    my $self = shift;    my $cloning = shift;    my $serialized = shift;    my $class = ref $self || $self;    my $obj;    if ( $class->isa(__PACKAGE__) )    {        $obj = __PACKAGE__->new( name => $serialized );    }    else    {        $obj = $class->new( name => $serialized );    }    # This breaks the "singleton-ness" of timezone objects, but    # there's no way to tell Storable to simply use an existing    # object.  This shouldn't matter since we copy the underlying    # structures by reference here, so span generation in one object    # will be visible in another also in memory.    %$self = %$obj;}## Functions#sub offset_as_seconds{    my $offset = shift;    return undef unless defined $offset;    return 0 if $offset eq '0';    my ( $sign, $hours, $minutes, $seconds );    if ( $offset =~ /^([\+\-])?(\d\d?):(\d\d)(?::(\d\d))?$/ )    {        ( $sign, $hours, $minutes, $seconds ) = ( $1, $2, $3, $4 );    }    elsif ( $offset =~ /^([\+\-])?(\d\d)(\d\d)(\d\d)?$/ )    {        ( $sign, $hours, $minutes, $seconds ) = ( $1, $2, $3, $4 );    }    else    {        return undef;    }    $sign = '+' unless defined $sign;    return undef unless $hours >= 0 && $hours <= 99;    return undef unless $minutes >= 0 && $minutes <= 59;    return undef unless ! defined( $seconds ) || ( $seconds >= 0 && $seconds <= 59 );    my $total =  $hours * 3600 + $minutes * 60;    $total += $seconds if $seconds;    $total *= -1 if $sign eq '-';    return $total;}sub offset_as_string{    my $offset = shift;    return undef unless defined $offset;    return undef unless $offset >= -359999 && $offset <= 359999;    my $sign = $offset < 0 ? '-' : '+';    $offset = abs($offset);    my $hours = int( $offset / 3600 );    $offset %= 3600;    my $mins = int( $offset / 60 );    $offset %= 60;    my $secs = int( $offset );    return ( $secs ?             sprintf( '%s%02d%02d%02d', $sign, $hours, $mins, $secs ) :             sprintf( '%s%02d%02d', $sign, $hours, $mins )           );}1;__END__=head1 NAMEDateTime::TimeZone - Time zone object base class and factory=head1 SYNOPSIS  use DateTime;  use DateTime::TimeZone  my $tz = DateTime::TimeZone->new( name => 'America/Chicago' );  my $dt = DateTime->now();  my $offset = $tz->offset_for_datetime($dt);=head1 DESCRIPTIONThis class is the base class for all time zone objects.  A time zoneis represented internally as a set of observances, each of whichdescribes the offset from GMT for a given time period.Note that without the C<DateTime.pm> module, this module does not domuch.  It's primary interface is through a C<DateTime> object, andmost users will not need to directly use C<DateTime::TimeZone>methods.=head1 USAGEThis class has the following methods:=over 4=item * new( name => $tz_name )Given a valid time zone name, this method returns a new time zoneblessed into the appropriate subclass.  Subclasses are named for thegiven time zone, so that the time zone "America/Chicago" is theDateTime::TimeZone::America::Chicago class.If the name given is a "link" name in the Olson database, the objectcreated may have a different name.  For example, there is a link fromthe old "EST5EDT" name to "America/New_York".There are also several special values that can be given as names.If the "name" parameter is "floating", then aC<DateTime::TimeZone::Floating> object is returned.  A floating timezone does have I<any> offset, and is always the same time.  This isuseful for calendaring applications, which may need to specify that agiven event happens at the same I<local> time, regardless of where itoccurs.  See RFC 2445 for more details.If the "name" parameter is "UTC", then a C<DateTime::TimeZone::UTC>object is returned.If the "name" is an offset string, it is converted to a number, and aC<DateTime::TimeZone::OffsetOnly> object is returned.=back=head3 The "local" time zoneIf the "name" parameter is "local", then the module attempts todetermine the local time zone for the system.First it checks C<$ENV> for keys named "TZ", "SYS$TIMEZONE_RULE","SYS$TIMEZONE_NAME", "UCX$TZ", or "TCPIP$TZC" (the last 4 are forVMS).  If this is defined, and it is not the string "local", then itis treated as any other valid name (including "floating"), and theconstructor tries to create a time zone based on that name.Next, it checks for the existence of a symlink at F</etc/localtime>.It follows this link to the real file and figures out what the file'sname is.  It then tries to turn this name into a valid time zone.  Forexample, if this file is linked to F</usr/share/zoneinfo/US/Central>,it will end up trying "US/Central", which will then be converted to"America/Chicago" internally.Some systems just copy the relevant file to F</etc/localtime> insteadof making a symlink.  In this case, we look in F</usr/share/zoneinfo>for a file that has the same size and content as F</etc/localtime> todetermine the local time zone.Then it checks for a file called F</etc/timezone> or F</etc/TIMEZONE>.If one of these exists, it is read and it tries to create a time zonewith the name contained in the file.Finally, it checks for a file called F</etc/sysconfig/clock>.  If thisfile exists, it looks for a line inside the file matchingC</^(?:TIME)ZONE="([^"]+)"/>.  If this line exists, it tries the value as atime zone name.If none of these methods work, it gives up and dies.=head2 Object MethodsC<DateTime::TimeZone> objects provide the following methods:=over 4=item * offset_for_datetime( $dt )Given a C<DateTime> object, this method returns the offset in secondsfor the given datetime.  This takes into account historical time zoneinformation, as well as Daylight Saving Time.  The offset isdetermined by looking at the object's UTC Rata Die days and seconds.=item * offset_for_local_datetime( $dt )Given a C<DateTime> object, this method returns the offset in secondsfor the given datetime.  Unlike the previous method, this method usesthe local time's Rata Die days and seconds.  This should only be donewhen the corresponding UTC time is not yet known, because local timescan be ambiguous due to Daylight Saving Time rules.=item * nameReturns the name of the time zone.  If this value is passed to theC<new()> method, it is guaranteed to create the same object.=item * short_name_for_datetime( $dt )Given a C<DateTime> object, this method returns the "short name" forthe current observance and rule this datetime is in.  These are nameslike "EST", "GMT", etc.It is B<strongly> recommended that you do not rely on these names foranything other than display.  These names are not official, and manyof them are simply the invention of the Olson database maintainers.Moreover, these names are not unique.  For example, there is an "EST"at both -0500 and +1000/+1100.=item * is_floatingReturns a boolean indicating whether or not this object represents afloating time zone, as defined by RFC 2445.=item * is_utcIndicates whether or not this object represents the UTC (GMT) timezone.=item * is_olsonReturns true if the time zone is a named time zone from the Olsondatabase.=item * categoryReturns the part of the time zone name before the first slash.  Forexample, the "America/Chicago" time zone would return "America".=back=head2 Class MethodsThis class provides one class method:=over 4=item * is_valid_name ($name)Given a string, this method returns a boolean value indicating whetheror not the string is a valid time zone name.  If you are usingC<DateTime::TimeZone::Alias>, any aliases you've created will be valid.=back=head2 Storable HooksThis module provides freeze and thaw hooks for C<Storable> so that thehuge data structures for Olson time zones are not actually stored inthe serialized structure.If you subclass C<DateTime::TimeZone>, you will inherit its hooks,which may not work for your module, so please test the interaction ofyour module with Storable.=head2 FunctionsThis class also contains several functions, none of which areexported.=over 4=item * all_namesThis returns a pre-sorted list of all the time zone names.  This listdoes not include link names.  In scalar context, it returns an arrayreference, while in list context it returns an array.=item * categoriesThis returns a list of all time zone categories.  In scalar context,it returns an array reference, while in list context it returns anarray.=item * linksThis returns a hash of all time zone links, where the keys are theold, deprecated names, and the values are the new names.  In scalarcontext, it returns a hash reference, while in list context it returnsa hash.=item * names_in_category( $category )Given a valid category, this method returns a list of the names inthat category, without the category portion.  So the list for the"America" category would include the strings "Chicago","Kentucky/Monticello", and "New_York".  In scalar context, it returnsan array reference, while in list context it returns an array.=item * offset_as_seconds( $offset )Given an offset as a string, this returns the number of secondsrepresented by the offset as a positive or negative number.  ReturnsC<undef> if $offset is not in the range C<-99:59:59> to C<+99:59:59>.The offset is expected to match eitherC</^([\+\-])?(\d\d?):(\d\d)(?::(\d\d))?$/> orC</^([\+\-])?(\d\d)(\d\d)(\d\d)?$/>.  If it doesn't match either ofthese, C<undef> will be returned.This means that if you want to specify hours as a single digit, theneach element of the offset must be separated by a colon (:).=item * offset_as_string( $offset )Given an offset as a number, this returns the offset as a string.Returns C<undef> if $offset is not in the range C<-359999> to C<359999>.=back=head1 SUPPORTSupport for this module is provided via the datetime@perl.org emaillist.  See http://lists.perl.org/ for more details.Please submit bugs to the CPAN RT system athttp://rt.cpan.org/NoAuth/ReportBug.html?Queue=datetime%3A%3Atimezoneor via email at bug-datetime-timezone@rt.cpan.org.=head1 AUTHORDave Rolsky <autarch@urth.org>, inspired by Jesse Vincent's work onDate::ICal::Timezone, and with help from the datetime@perl.org list.=head1 COPYRIGHTCopyright (c) 2003 David Rolsky.  All rights reserved.  This programis free software; you can redistribute it and/or modify it under thesame terms as Perl itself.The full text of the license can be found in the LICENSE file includedwith this module.=head1 SEE ALSOdatetime@perl.org mailing listhttp://datetime.perl.org/The tools directory of the DateTime::TimeZone distribution includestwo scripts that may be of interest to some people.  They areparse_olson and tests_from_zdump.  Please run them with the --helpflag to see what they can be used for.=cut

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -