]> git.donarmstrong.com Git - reference.git/blob - .svn/pristine/7a/7a7a014b8146bb7768477142a8f981333ed04bb2.svn-base
Import original source of Reference 0-Reference
[reference.git] / .svn / pristine / 7a / 7a7a014b8146bb7768477142a8f981333ed04bb2.svn-base
1 # This module is part of da_reference, and is released
2 # under the terms of the GPL version 2, or any later version. See the
3 # file README and COPYING for more information.
4 # Copyright 2003 by Don Armstrong <don@donarmstrong.com>.
5 # $Id$
6
7 package Reference::Field::Date;
8
9 =head1 NAME
10
11  --
12
13 =head1 SYNOPSIS
14
15
16 =head1 DESCRIPTION
17
18
19 =head1 BUGS
20
21 None known.
22
23 =cut
24
25
26 use strict;
27 use vars qw($REVISION $DEBUG);
28
29 use NEXT;
30 use Params::Validate qw(:types validate_with);
31 use Date::Manip;
32
33
34 BEGIN{
35      ($REVISION) = q$LastChangedRevision$ =~ /\$LastChangedRevision:\s+([^\s+])/;
36      $DEBUG = 0 unless defined $DEBUG;
37 }
38
39 =head2 date
40
41      
42
43 XXX DOCUMENT ME
44
45 =cut
46
47
48 sub date{
49      my $self = shift;
50      my %params;
51      if (scalar(@_) == 1) {
52           $params{date} = shift;
53           $params{output} = 'scalar';
54      }
55      else {
56           %params = validate_with(params => \@_,
57                                   spec   => {date  => {type     => ARRAYREF|SCALAR|HASHREF|UNDEF,
58                                                        optional => 1,
59                                                       },
60                                              day   => {type => SCALAR|UNDEF,
61                                                        optional => 1,
62                                                       },
63                                              year  => {type => SCALAR|UNDEF,
64                                                        optional => 1,
65                                                       },
66                                              month => {type => SCALAR|UNDEF,
67                                                        optional => 1,
68                                                       },
69                                              output => {default => 'scalar',
70                                                         type    => SCALAR,
71                                                        },
72                                             },
73                                  );
74      }
75      # Update author according to the passed information
76      if (defined $params{day} or defined $params{year} or defined $params{month}) {
77           $self->{reference}->{date}->{day}    = $params{day}   if defined $params{day};
78           $self->{reference}->{date}->{year}   = $params{year}  if defined $params{year};
79           $self->{reference}->{date}->{month}  = $params{month} if defined $params{month};
80      }
81      elsif (defined $params{date}) {
82           $self->{reference}->{date} = {day   => undef,
83                                         year  => undef,
84                                         month => undef,
85                                        };
86           my $date = ParseDate($params{date});
87           $self->{reference}->{date}->{unix} = $date;
88           ($self->{reference}->{date}->{day},
89            $self->{reference}->{date}->{year},
90            $self->{reference}->{date}->{month}) = UnixDate($date,qw(%e %Y %m));
91      }
92
93      local $_ = $params{output};
94      if (/bibtex/) {
95           return UnixDate($self->{reference}->{date}->{unix},'%B %e %Y') if defined $self->{reference}->{date}->{unix};
96           return join(' ',$self->{reference}->{date}->{day},$self->{reference}->{date}->{year},$self->{reference}->{date}->{month});
97      }
98      elsif (/year/) {
99          return UnixDate($self->{reference}->{date}->{unix},'%Y') if defined $self->{reference}->{date}->{unix};
100          return $self->{reference}->{date}->{year};
101      }
102      else {
103           return UnixDate($self->{reference}->{date}->{unix},'%B %e %Y') if defined $self->{reference}->{date}->{unix};
104           return join(' ',$self->{reference}->{date}->{day},$self->{reference}->{date}->{year},$self->{reference}->{date}->{month});
105      }
106 }
107
108 =head2 year
109
110      
111
112 Returns the year associated with the date field
113
114
115 =cut
116
117
118 sub year{
119      my $self = shift;
120
121      return $self->{reference}->{date}->{year};
122 }
123
124 =head2 day
125
126      
127
128 Returns the day associated with the date field
129
130 =cut
131
132 sub day{
133      my $self = shift;
134
135      return $self->{reference}->{date}->{day};
136 }
137
138 =head2 month
139
140      
141
142 Returns the month associated with the date field
143
144 =cut
145
146 sub month{
147      my $self = shift;
148
149      return $self->{reference}->{date}->{month};
150 }
151
152
153 sub _init{
154      my $self = shift;
155
156      $self->{reference}->{date} = {month => undef,
157                                    year  => undef,
158                                    day   => undef,
159                                    unix  => undef,
160                                   };
161
162      $self->NEXT::_init;
163
164 }
165
166
167
168
169 1;
170
171
172 __END__
173
174
175
176
177
178