[Bio] / Sprout / ERDBTypeDate.pm Repository:
ViewVC logotype

Annotation of /Sprout/ERDBTypeDate.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (view) (download) (as text)

1 : parrello 1.1 #!/usr/bin/perl -w
2 :    
3 :     #
4 :     # Copyright (c) 2003-2006 University of Chicago and Fellowship
5 :     # for Interpretations of Genomes. All Rights Reserved.
6 :     #
7 :     # This file is part of the SEED Toolkit.
8 :     #
9 :     # The SEED Toolkit is free software. You can redistribute
10 :     # it and/or modify it under the terms of the SEED Toolkit
11 :     # Public License.
12 :     #
13 :     # You should have received a copy of the SEED Toolkit Public License
14 :     # along with this program; if not write to the University of Chicago
15 :     # at info@ci.uchicago.edu or the Fellowship for Interpretation of
16 :     # Genomes at veronika@thefig.info or download a copy from
17 :     # http://www.theseed.org/LICENSE.TXT.
18 :     #
19 :    
20 :     package ERDBTypeDate;
21 :    
22 :     use strict;
23 :     use Tracer;
24 :     use ERDB;
25 :     use Time::Local qw(timelocal_nocheck);
26 :     use POSIX qw(strftime);
27 :     use base qw(ERDBType);
28 :    
29 :     use constant MONTHS => [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
30 :    
31 :     =head1 ERDB Date Type Definition
32 :    
33 :     =head2 Introduction
34 :    
35 :     This object represents the primitive data type for dates. Dates are stored as a
36 :     whole number of seconds since the Unix epoch, which was midnight on January 1,
37 :     1970 UCT. Dates prior to 1970 are negative numbers, but bad things happen if you
38 :     try to go back beyond 1800, because of the calendar conversions in the 18th
39 :     century.
40 :    
41 :     As a convenience, if a date is specified as a string of the style
42 :     C<mm/dd/yy hh:mm:ss>, it will be converted from the local time to the internal
43 :     representation. (The hours must be in military time-- 0 to 24.) There is no
44 :     corresponding conversion on the way out.
45 :    
46 :     =head3 new
47 :    
48 :     my $et = ERDBTypeDate->new();
49 :    
50 :     Construct a new ERDBTypeDate descriptor.
51 :    
52 :     =cut
53 :    
54 :     sub new {
55 :     # Get the parameters.
56 :     my ($class) = @_;
57 :     # Create the ERDBTypeDate object.
58 :     my $retVal = { };
59 :     # Bless and return it.
60 :     bless $retVal, $class;
61 :     return $retVal;
62 :     }
63 :    
64 :     =head2 Public Methods
65 :    
66 :     =head3 parseDate
67 :    
68 :     my ($y, $mo, $d, $h, $mi, $s) = $et->parseDate($string);
69 :    
70 :     Parse the string into the constituent date components: month, day, year,
71 :     hour, minute, second. The pieces are not validated in any meaningful way,
72 :     but if the date won't parse, an empty list will be returned.
73 :    
74 :     =over 4
75 :    
76 :     =item string
77 :    
78 :     Input string representing a date. It must in in a standard C<mm/dd/yy hh:mm:ss>
79 :     format with a 24-hour clock.
80 :    
81 :     =item RETURN
82 :    
83 :     Returns the six components of a time stamp, in order from largest significance
84 :     to smallest significance.
85 :    
86 :     =back
87 :    
88 :     =cut
89 :    
90 :     sub parseDate {
91 :     # Get the parameters.
92 :     my ($self, $string) = @_;
93 :     # Declare the return variables.
94 :     my ($y, $mo, $d, $h, $mi, $s);
95 :     # Parse the string. Note that the time and the seconds are optional.
96 :     # The constructs that make them conditional use the clustering operator
97 :     # (?:) so that they don't interfere in the grouping results.
98 :     if ($string =~ m#^\s*(\d+)/(\d+)/(\d+)(?:\s+(\d+):(\d+)(?::(\d+))?)?\s*$#) {
99 :     # Extract the pieces of the time stamp. Note that the hours, minutes,
100 :     # and seconds all default to 0 if they weren't found.
101 :     ($mo, $d, $y, $h, $mi, $s) = ($1, $2, $3, $4 || 0, $5 || 0, $6 || 0);
102 :     }
103 :     # Return the results.
104 :     return ($y, $mo, $d, $h, $mi, $s);
105 :     }
106 :    
107 :     =head2 Virtual Methods
108 :    
109 :     =head3 averageLength
110 :    
111 :     my $value = $et->averageLength();
112 :    
113 :     Return the average length of a data item of this field type when it is stored in the
114 :     database. This value is used to compute the expected size of a database table.
115 :    
116 :     =cut
117 :    
118 :     sub averageLength {
119 :     return 8;
120 :     }
121 :    
122 :     =head3 prettySortValue
123 :    
124 :     my $value = $et->prettySortValue();
125 :    
126 :     Number indicating where fields of this type should go in relation to other
127 :     fields. The value should be somewhere between C<1> and C<5>. A value outside
128 :     that range will make terrible things happen.
129 :    
130 :     =cut
131 :    
132 :     sub prettySortValue() {
133 :     return 1;
134 :     }
135 :    
136 :     =head3 validate
137 :    
138 :     my $okFlag = $et->validate($value);
139 :    
140 :     Return an error message if the specified value is invalid for this field type.
141 :    
142 :     The parameters are as follows.
143 :    
144 :     =over 4
145 :    
146 :     =item value
147 :    
148 :     Value of this type, for validation.
149 :    
150 :     =item RETURN
151 :    
152 :     Returns an empty string if the specified field is valid, and an error message
153 :     otherwise.
154 :    
155 :     =back
156 :    
157 :     =cut
158 :    
159 :     sub validate {
160 :     # Get the parameters.
161 :     my ($self, $value) = @_;
162 :     # Assume it's valid until we prove otherwise.
163 :     my $retVal = "";
164 :     if ($value =~ m/^[+-]?\d+$/) {
165 :     # Here the value is a number, so we just need to verify that
166 :     # it will fit. No sane date will ever fail this check.
167 :     if ($value > 9223372036854775807 || $value < -9223372036854775807) {
168 :     $retVal = "Date number is out of range.";
169 :     }
170 :     } else {
171 :     # Here we have to have a date string. Parse it and complain if it
172 :     # won't parse.
173 :     my ($y, $mo, $d, $h, $mi, $s) = $self->parseDate($value);
174 :     if (! defined $y) {
175 :     $retVal = "Date has an invalid format.";
176 :     } else {
177 :     # Validate the individual pieces of the date.
178 :     if ($y > 99 && $y < 1800) {
179 :     $retVal = "Dates cannot be prior to 1800.";
180 :     } elsif ($mo < 1 || $mo > 12) {
181 :     $retVal = "Date has an invalid month."
182 :     } elsif ($d < 1 || $d > MONTHS->[$mo]) {
183 :     $retVal = "Date has an invalid day of month.";
184 :     } elsif ($d == 29 && $mo == 2 &&
185 :     ($y % 4 != 0 || $y % 100 == 0 && $y % 400 != 0)) {
186 :     $retVal = "Date is for February 29 in a non-leap year.";
187 :     } elsif ($h >= 24) {
188 :     $retVal = "Date has an invalid hour number.";
189 :     } elsif ($mi >= 60) {
190 :     $retVal = "Date has an invalid minute number.";
191 :     } elsif ($s >= 60) {
192 :     $retVal = "Date has an invalid second number.";
193 :     }
194 :     }
195 :     }
196 :     # Return the determination.
197 :     return $retVal;
198 :     }
199 :    
200 :     =head3 encode
201 :    
202 :     my $string = $et->encode($value, $mode);
203 :    
204 :     Encode a value of this field type for storage in the database (or in a database load
205 :     file.)
206 :    
207 :     The parameters are as follows.
208 :    
209 :     =over 4
210 :    
211 :     =item value
212 :    
213 :     Value of this type, for encoding.
214 :    
215 :     =item mode
216 :    
217 :     TRUE if the value is being encoding for placement in a load file, FALSE if it
218 :     is being encoded for use as an SQL statement parameter. In most cases, the
219 :     encoding is the same for both modes.
220 :    
221 :     =back
222 :    
223 :     =cut
224 :    
225 :     sub encode {
226 :     # Get the parameters.
227 :     my ($self, $value, $mode) = @_;
228 :     # Declare the return variable.
229 :     my $retVal = $value;
230 :     # Is it a date string?
231 :     my ($y, $mo, $d, $h, $mi, $s) = $self->parseDate($value);
232 :     if (defined $y) {
233 :     # Yes. Convert it from local time.
234 :     $retVal = timelocal_nocheck($s, $mi, $h, $d-1, $mo-1, $y);
235 :     }
236 :     # Return the result.
237 :     return $retVal;
238 :     }
239 :    
240 :     =head3 decode
241 :    
242 :     my $value = $et->decode($string);
243 :    
244 :     Decode a string from the database into a value of this field type.
245 :    
246 :     The parameters are as follows.
247 :    
248 :     =over 4
249 :    
250 :     =item string
251 :    
252 :     String from the database to be decoded.
253 :    
254 :     =item RETURN
255 :    
256 :     Returns a value of the desired type.
257 :    
258 :     =back
259 :    
260 :     =cut
261 :    
262 :     sub decode {
263 :     # Get the parameters.
264 :     my ($self, $string) = @_;
265 :     # Declare the return variable.
266 :     my $retVal = $string;
267 :     # Return the result.
268 :     return $retVal;
269 :     }
270 :    
271 :     =head3 sqlType
272 :    
273 :     my $typeString = $et->sqlType();
274 :    
275 :     Return the SQL data type for this field type.
276 :    
277 :     =cut
278 :    
279 :     sub sqlType {
280 :     return "BIGINT";
281 :     }
282 :    
283 :     =head3 indexMod
284 :    
285 :     my $length = $et->indexMod();
286 :    
287 :     Return the index modifier for this field type. The index modifier is the number of
288 :     characters to be indexed. If it is undefined, the field cannot be indexed. If it
289 :     is an empty string, the entire field is indexed. The default is an empty string.
290 :    
291 :     =cut
292 :    
293 :     sub indexMod {
294 :     return '';
295 :     }
296 :    
297 :     =head3 sortType
298 :    
299 :     my $letter = $et->sortType();
300 :    
301 :     Return the sorting type for this field type. The sorting type is C<n> for integers,
302 :     C<g> for floating-point numbers, and the empty string for character fields.
303 :     The default is the empty string.
304 :    
305 :     =cut
306 :    
307 :     sub sortType {
308 :     return "n";
309 :     }
310 :    
311 :     =head3 documentation
312 :    
313 :     my $docText = $et->documentation();
314 :    
315 :     Return the documentation text for this field type. This should be in TWiki markup
316 :     format, though HTML will also work.
317 :    
318 :     =cut
319 :    
320 :     sub documentation() {
321 :     return 'Date and time stamp, in seconds since 1970.';
322 :     }
323 :    
324 :     =head3 name
325 :    
326 :     my $name = $et->name();
327 :    
328 :     Return the name of this type, as it will appear in the XML database definition.
329 :    
330 :     =cut
331 :    
332 :     sub name() {
333 :     return "date";
334 :     }
335 :    
336 :     =head3 default
337 :    
338 :     my $defaultValue = $et->default();
339 :    
340 :     Default value to be used for fields of this type if no default value is
341 : parrello 1.2 specified in the database definition or in an L<ERDBLoadGroup/Put>
342 : parrello 1.1 call during a loader operation. The default is undefined, which means
343 :     an error will be thrown during the load.
344 :    
345 :     =cut
346 :    
347 :     sub default {
348 :     return time;
349 :     }
350 :    
351 :     =head3 align
352 :    
353 :     my $alignment = $et->align();
354 :    
355 :     Return the display alignment for fields of this type: either C<left>, C<right>, or
356 :     C<center>. The default is C<left>.
357 :    
358 :     =cut
359 :    
360 :     sub align {
361 :     return 'left';
362 :     }
363 :    
364 :     =head3 html
365 :    
366 :     my $html = $et->html($value);
367 :    
368 :     Return the HTML for displaying the content of a field of this type in an output
369 :     table. The default is the raw value, html-escaped.
370 :    
371 :     =cut
372 :    
373 :     sub html {
374 :     my ($self, $value) = @_;
375 :     # Break the time into its component parts.
376 :     my @times = localtime($value);
377 :     # Convert them to a string.
378 :     my $retVal = strftime("%m/%d/%Y %H:%M:%S", @times);
379 :     # Return the result.
380 :     return $retVal;
381 :     }
382 :    
383 :    
384 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3