| 1 | package Lingua::ZH::DateTime; | ||
| 2 | ########################################################### | ||
| 3 | # Zhongwen Date&Time | ||
| 4 | ########################################################### | ||
| 5 | # Copyright (c) 2005-2006 hoowa sun & Meng.H P.R.China | ||
| 6 | # | ||
| 7 | # See COPYRIGHT section in pod text below for usage and distribution rights. | ||
| 8 | # | ||
| 9 | # <hoowa.sun@gmail.com> | ||
| 10 | # www.perlchina.org | ||
| 11 | # last modify 2006-2-21 | ||
| 12 | ########################################################### | ||
| 13 | $Lingua::ZH::DateTime::VERSION='0.01'; | ||
| 14 | |||
| 15 | use strict; | ||
| 16 | use vars qw/$locale $charset $lc_string $dt_string/; | ||
| 17 | |||
| 18 | $dt_string = { | ||
| 19 | 'gb2312' => { | ||
| 20 | 'year'=>'Äê', | ||
| 21 | 'month'=>'ÔÂ', | ||
| 22 | 'day'=>'ÈÕ', | ||
| 23 | }, | ||
| 24 | 'big5' => { | ||
| 25 | } | ||
| 26 | }; | ||
| 27 | $lc_string = { | ||
| 28 | 'china' => { | ||
| 29 | 'month'=> { | ||
| 30 | 'jan' => '1', 'feb' => '2', 'mar' => '3', | ||
| 31 | 'apr' => '4', 'may' => '5', 'jun' => '6', | ||
| 32 | 'Jul' => '7', 'aug' => '8', 'sep' => '9', | ||
| 33 | 'sept' => '9', 'oct' => '10', 'nov' => '11', | ||
| 34 | 'dec' => '12' | ||
| 35 | }, | ||
| 36 | 'day' => { | ||
| 37 | '1' => '1', '2' => '2', '3' => '3', | ||
| 38 | '4' => '4', '5' => '5', '6' => '6', | ||
| 39 | '7' => '7', '8' => '8', '9' => '9', | ||
| 40 | '10' => '10', '11' => '11', '12' => '12', | ||
| 41 | '13' => '13', '14' => '14', '15' => '15', | ||
| 42 | '16' => '16', '17' => '17', '18' => '18', | ||
| 43 | '19' => '19', '20' => '20', '21' => '21', | ||
| 44 | '22' => '22', '23' => '23', '24' => '24', | ||
| 45 | '25' => '25', '26' => '26', '27' => '27', | ||
| 46 | '28' => '28', '29' => '29', '30' => '30', | ||
| 47 | '31' => '31' | ||
| 48 | }, | ||
| 49 | 'hour' => { | ||
| 50 | '1' => '1', '2' => '2', '3' => '3', | ||
| 51 | '4' => '4', '5' => '5', '6' => '6', | ||
| 52 | '7' => '7', '8' => '8', '9' => '9', | ||
| 53 | '10' => '10', '11' => '11', '12' => '12', | ||
| 54 | '13' => '13', '14' => '14', '15' => '15', | ||
| 55 | '16' => '16', '17' => '17', '18' => '18', | ||
| 56 | '19' => '19', '20' => '20', '21' => '21', | ||
| 57 | '22' => '22', '23' => '23', '24' => '24' | ||
| 58 | } | ||
| 59 | } | ||
| 60 | }; | ||
| 61 | |||
| 62 | |||
| 63 | sub new { | ||
| 64 | my $self = {}; | ||
| 65 | my (undef,%args) = @_; | ||
| 66 | $locale = $args{'locale'} if ($args{'locale'}); | ||
| 67 | $charset = $args{'charset'} if ($args{'charset'}); | ||
| 68 | |||
| 69 | bless $self; | ||
| 70 | return $self; | ||
| 71 | } | ||
| 72 | |||
| 73 | ############################## | ||
| 74 | # METHOD | ||
| 75 | sub set { | ||
| 76 | my $self = shift; | ||
| 77 | my %args = @_; | ||
| 78 | $locale = $args{'locale'} if($args{'locale'}); | ||
| 79 | $charset = $args{'charset'} if ($args{'charset'}) ; | ||
| 80 | |||
| 81 | return(1); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub convert { | ||
| 85 | my $self = shift; | ||
| 86 | my %args = @_; | ||
| 87 | my ($week,$year,$mon,$day,$hour,$min,$sec,$date,$time); | ||
| 88 | |||
| 89 | if (!$args{'asctime'}) { | ||
| 90 | $args{'asctime'} = localtime(); | ||
| 91 | } | ||
| 92 | |||
| 93 | ($week,$mon,$day,$time,$year) = split(/\s+/,$args{'asctime'}); | ||
| 94 | ($hour,$min,$sec) = split(/:/,$time); | ||
| 95 | $mon = lc($mon); | ||
| 96 | $hour =~ s/^0//; | ||
| 97 | |||
| 98 | $args{'output'} = "datetime" if(!$args{'output'}); | ||
| 99 | $locale = "china" if(!$locale); | ||
| 100 | $charset = "gb2312" if(!$charset); | ||
| 101 | |||
| 102 | if ($locale eq 'china') { | ||
| 103 | |||
| 104 | $date = $year . $dt_string->{$charset}{"year"} . | ||
| 105 | $lc_string->{$locale}{'month'}{$mon} . $dt_string->{$charset}{"month"} . | ||
| 106 | $lc_string->{$locale}{'day'}{$day} . $dt_string->{$charset}{"day"}; | ||
| 107 | |||
| 108 | $time = $lc_string->{$locale}{'hour'}{$hour} . ":$min:$sec"; | ||
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | #return value | ||
| 114 | return $date if($args{'output'} eq 'date'); | ||
| 115 | return $time if($args{'output'} eq 'time'); | ||
| 116 | return "$date $time" if($args{'output'} eq 'datetime'); | ||
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | =head1 NAME | ||
| 121 | |||
| 122 | Lingua::ZH::DateTime - convert time to chinese format. | ||
| 123 | |||
| 124 | =head1 SYNOPSIS | ||
| 125 | |||
| 126 | =head1 DESCRIPTION | ||
| 127 | |||
| 128 | This module can convert asctime to chinese format in your locale. | ||
| 129 | you can select china singapore hongkong taiwan locale and gb2312/big5 charset. | ||
| 130 | asctime is ASC C standard format will from C<localtime> like this: | ||
| 131 | |||
| 132 | $asctime = localtime(); | ||
| 133 | |||
| 134 | =head1 METHOD | ||
| 135 | |||
| 136 | =head2 new | ||
| 137 | |||
| 138 | my $zdt = new Lingua::ZH::DateTime(locale=>'china',charset=>'gb2312'); | ||
| 139 | |||
| 140 | Instantiates a new object. | ||
| 141 | |||
| 142 | =head2 set | ||
| 143 | |||
| 144 | my $zdt->set(locale=>'china',charset=>'gb2312'); | ||
| 145 | |||
| 146 | set locale and charset encode. | ||
| 147 | |||
| 148 | =over 2 | ||
| 149 | |||
| 150 | =item * locale -> china only now. | ||
| 151 | |||
| 152 | =item * charset -> gb2312 only now. | ||
| 153 | |||
| 154 | =back | ||
| 155 | |||
| 156 | =head2 convert | ||
| 157 | |||
| 158 | $zdt->convert(asctime=>'Thu Oct 13 04:54:34 1994',output=>'date'); | ||
| 159 | |||
| 160 | =over 2 | ||
| 161 | |||
| 162 | =item * ctime -> input asctime data. | ||
| 163 | |||
| 164 | =item * output -> output in 3 types: date,time,datetime. | ||
| 165 | |||
| 166 | =back | ||
| 167 | |||
| 168 | =head1 AUTHORS | ||
| 169 | |||
| 170 | Lingua::ZH::DateTime by hoowa sun and Meng.H. | ||
| 171 | |||
| 172 | =head1 COPYRIGHT | ||
| 173 | |||
| 174 | The Lingua::ZH::DateTime module is Copyright (c) 2005-2006 hoowa sun & Meng.H | ||
| 175 | P.R.China. All rights reserved. | ||
| 176 | |||
| 177 | You may distribute under the terms of either the GNU General Public | ||
| 178 | License or the Artistic License, as specified in the Perl README file. | ||
| 179 | |||
| 180 | =head1 WARRANTY | ||
| 181 | |||
| 182 | The Lingua::ZH::DateTime is free Open Source software. | ||
| 183 | |||
| 184 | IT COMES WITHOUT WARRANTY OF ANY KIND. | ||
| 185 | |||
| 186 | =head1 SUPPORT | ||
| 187 | |||
| 188 | http://www.perlchina.org | ||
| 189 | |||
| 190 | =cut | ||
| 191 | |||
| 192 | 1; |