WriteExcel/Utility.pm untested

filter: Statementstmt 0.0% 0 / 126 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 240 CC 23 SCAR 29.0
CC
23
Cov
0%
xl_decode_date_EU
42.0
xl_decode_date_US
42.0
xl_parse_time
20.0
CRAP 18.2
1 package Spreadsheet::WriteExcel::Utility;
2  
3 ###############################################################################
4 #
5 # Utility - Helper functions for Spreadsheet::WriteExcel.
6 #
7 # Copyright 2000-2010, John McNamara, jmcnamara@cpan.org
8 #
9 #
10  
11 use Exporter;
12 use strict;
13 use autouse 'Date::Calc' => qw(Delta_DHMS Decode_Date_EU Decode_Date_US);
14 use autouse 'Date::Manip' => qw(ParseDate Date_Init);
15  
16  
17 # Do all of the export preparation
18 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
19  
20 # Row and column functions
21 my @rowcol = qw(
22 xl_rowcol_to_cell
23 xl_cell_to_rowcol
24 xl_range_formula
25 xl_inc_row
26 xl_dec_row
27 xl_inc_col
28 xl_dec_col
29 );
30  
31 # Date and Time functions
32 my @dates = qw(
33 xl_date_list
34 xl_date_1904
35 xl_parse_time
36 xl_parse_date
37 xl_parse_date_init
38 xl_decode_date_EU
39 xl_decode_date_US
40 );
41  
42  
43 @ISA = qw(Exporter);
44 @EXPORT_OK = ();
45 @EXPORT = (@rowcol, @dates);
46 %EXPORT_TAGS = (
47 rowcol => \@rowcol,
48 dates => \@dates
49 );
50  
51 $VERSION = '2.40';
52  
53  
54  
55 =encoding latin1
56  
57 =head1 NAME
58  
59 Utility - Helper functions for Spreadsheet::WriteExcel.
60  
61  
62  
63 =head1 SYNOPSIS
64  
65 Functions to help with some common tasks when using Spreadsheet::WriteExcel.
66  
67 These functions mainly relate to dealing with rows and columns in A1 notation and to handling dates and times.
68  
69 use Spreadsheet::WriteExcel::Utility; # Import everything
70  
71 ($row, $col) = xl_cell_to_rowcol('C2'); # (1, 2)
72 $str = xl_rowcol_to_cell(1, 2); # C2
73 $str = xl_inc_col('Z1' ); # AA1
74 $str = xl_dec_col('AA1' ); # Z1
75  
76 $date = xl_date_list(2002, 1, 1); # 37257
77 $date = xl_parse_date("11 July 1997"); # 35622
78 $time = xl_parse_time('3:21:36 PM'); # 0.64
79 $date = xl_decode_date_EU("13 May 2002"); # 37389
80  
81  
82  
83  
84 =head1 DESCRIPTION
85  
86 This module provides a set of functions to help with some common tasks encountered when using the Spreadsheet::WriteExcel module. The two main categories of function are:
87  
88 Row and column functions: these are used to deal with Excel's A1 representation of cells. The functions in this category are:
89  
90 xl_rowcol_to_cell
91 xl_cell_to_rowcol
92 xl_range_formula
93 xl_inc_row
94 xl_dec_row
95 xl_inc_col
96 xl_dec_col
97  
98 Date and Time functions: these are used to convert dates and times to the numeric format used by Excel. The functions in this category are:
99  
100 xl_date_list
101 xl_date_1904
102 xl_parse_time
103 xl_parse_date
104 xl_parse_date_init
105 xl_decode_date_EU
106 xl_decode_date_US
107  
108 All of these functions are exported by default. However, you can use import lists if you wish to limit the functions that are imported:
109  
110 use Spreadsheet::WriteExcel::Utility; # Import everything
111 use Spreadsheet::WriteExcel::Utility qw(xl_date_list); # xl_date_list only
112 use Spreadsheet::WriteExcel::Utility qw(:rowcol); # Row/col functions
113 use Spreadsheet::WriteExcel::Utility qw(:dates); # Date functions
114  
115  
116  
117 =head1 ROW AND COLUMN FUNCTIONS
118  
119  
120 Spreadsheet::WriteExcel supports two forms of notation to designate the position of cells: Row-column notation and A1 notation.
121  
122 Row-column notation uses a zero based index for both row and column while A1 notation uses the standard Excel alphanumeric sequence of column letter and 1-based row. Columns range from A to IV i.e. 0 to 255, rows range from 1 to 16384 in Excel 5 and 65536 in Excel 97. For example:
123  
124 (0, 0) # The top left cell in row-column notation.
125 ('A1') # The top left cell in A1 notation.
126  
127 (1999, 29) # Row-column notation.
128 ('AD2000') # The same cell in A1 notation.
129  
130 Row-column notation is useful if you are referring to cells programmatically:
131  
132 for my $i (0 .. 9) {
133 $worksheet->write($i, 0, 'Hello'); # Cells A1 to A10
134 }
135  
136 A1 notation is useful for setting up a worksheet manually and for working with formulas:
137  
138 $worksheet->write('H1', 200);
139 $worksheet->write('H2', '=H7+1');
140  
141 The functions in the following sections can be used for dealing with A1 notation, for example:
142  
143 ($row, $col) = xl_cell_to_rowcol('C2'); # (1, 2)
144 $str = xl_rowcol_to_cell(1, 2); # C2
145  
146  
147 Cell references in Excel can be either relative or absolute. Absolute references are prefixed by the dollar symbol as shown below:
148  
149 A1 # Column and row are relative
150 $A1 # Column is absolute and row is relative
151 A$1 # Column is relative and row is absolute
152 $A$1 # Column and row are absolute
153  
154 An absolute reference only has an effect if the cell is copied. Refer to the Excel documentation for further details. All of the following functions support absolute references.
155  
156 =cut
157  
158  
159  
160  
161 ###############################################################################
162 ###############################################################################
163  
164 =head2 xl_rowcol_to_cell($row, $col, $row_absolute, $col_absolute)
165  
166 Parameters: $row: Integer
167 $col: Integer
168 $row_absolute: Boolean (1/0) [optional, default is 0]
169 $col_absolute: Boolean (1/0) [optional, default is 0]
170  
171 Returns: A string in A1 cell notation
172  
173  
174 This function converts a zero based row and column cell reference to a A1 style string:
175  
176 $str = xl_rowcol_to_cell(0, 0); # A1
177 $str = xl_rowcol_to_cell(0, 1); # B1
178 $str = xl_rowcol_to_cell(1, 0); # A2
179  
180  
181 The optional parameters C<$row_absolute> and C<$col_absolute> can be used to indicate if the row or column is absolute:
182  
183 $str = xl_rowcol_to_cell(0, 0, 0, 1); # $A1
184 $str = xl_rowcol_to_cell(0, 0, 1, 0); # A$1
185 $str = xl_rowcol_to_cell(0, 0, 1, 1); # $A$1
186  
187 See L<ROW AND COLUMN FUNCTIONS> for an explanation of absolute cell references.
188  
189  
190 =cut
191 ###############################################################################
192 #
193 # xl_rowcol_to_cell($row, $col, $row_absolute, $col_absolute)
194 #
195 sub xl_rowcol_to_cell {
196  
197 my $row = $_[0];
198 my $col = $_[1];
199 my $row_abs = $_[2] ? '$' : '';
200 my $col_abs = $_[3] ? '$' : '';
201 my $col_str = '';
202  
203 # Change from 0-indexed to 1 indexed.
204 $row++;
205 $col++;
206  
207 while ( $col ) {
208 # Set remainder from 1 .. 26
209 my $remainder = $col % 26 || 26;
210  
211 # Convert the $remainder to a character. C-ishly.
212 my $col_letter = chr( ord( 'A' ) + $remainder - 1 );
213  
214 # Accumulate the column letters, right to left.
215 $col_str = $col_letter . $col_str;
216  
217 # Get the next order of magnitude.
218 $col = int( ( $col - 1 ) / 26 );
219 }
220  
221 return $col_abs . $col_str . $row_abs . $row;
222 }
223  
224  
225  
226  
227 ###############################################################################
228 ###############################################################################
229  
230 =head2 xl_cell_to_rowcol($string)
231  
232  
233 Parameters: $string String in A1 format
234  
235 Returns: List ($row, $col)
236  
237 This function converts an Excel cell reference in A1 notation to a zero based row and column. The function will also handle Excel's absolute, C<$>, cell notation.
238  
239 my ($row, $col) = xl_cell_to_rowcol('A1'); # (0, 0)
240 my ($row, $col) = xl_cell_to_rowcol('B1'); # (0, 1)
241 my ($row, $col) = xl_cell_to_rowcol('C2'); # (1, 2)
242 my ($row, $col) = xl_cell_to_rowcol('$C2' ); # (1, 2)
243 my ($row, $col) = xl_cell_to_rowcol('C$2' ); # (1, 2)
244 my ($row, $col) = xl_cell_to_rowcol('$C$2'); # (1, 2)
245  
246 =cut
247 ###############################################################################
248 #
249 # xl_cell_to_rowcol($string)
250 #
251 # Returns: ($row, $col, $row_absolute, $col_absolute)
252 #
253 # The $row_absolute and $col_absolute parameters aren't documented because they
254 # mainly used internally and aren't very useful to the user.
255 #
256 sub xl_cell_to_rowcol {
257  
258 my $cell = shift;
259  
260 $cell =~ /(\$?)([A-Z]{1,3})(\$?)(\d+)/;
261  
262 my $col_abs = $1 eq "" ? 0 : 1;
263 my $col = $2;
264 my $row_abs = $3 eq "" ? 0 : 1;
265 my $row = $4;
266  
267 # Convert base26 column string to number
268 # All your Base are belong to us.
269 my @chars = split //, $col;
270 my $expn = 0;
271 $col = 0;
272  
273 while (@chars) {
274 my $char = pop(@chars); # LS char first
275 $col += (ord($char) -ord('A') +1) * (26**$expn);
276 $expn++;
277 }
278  
279 # Convert 1-index to zero-index
280 $row--;
281 $col--;
282  
283 return $row, $col, $row_abs, $col_abs;
284 }
285  
286  
287  
288  
289 ###############################################################################
290 ###############################################################################
291  
292 =head2 xl_range_formula($sheetname, $row_1, $row_2, $col_1, $col_2)
293  
294 Parameters: $sheetname String
295 $row_1: Integer
296 $row_2: Integer
297 $col_1: Integer
298 $col_2: Integer
299  
300 Returns: A worksheet range formula as a string.
301  
302 This function converts zero based row and column cell references to an A1 style formula string:
303  
304 my $str = xl_range_formula('Sheet1', 0, 9, 0, 0); # =Sheet1!$A$1:$A$10
305 my $str = xl_range_formula('Sheet2', 6, 65, 1, 1); # =Sheet2!$B$7:$B$66
306 my $str = xl_range_formula('New data', 1, 8, 2, 2); # ='New data'!$C$2:$C$9
307  
308  
309 This is useful for setting ranges in Chart objects:
310  
311  
312 $chart->add_series(
313 categories => xl_range_formula('Sheet1', 1, 9, 0, 0),
314 values => xl_range_formula('Sheet1', 1, 9, 1, 1),
315 );
316  
317 # Which is the same as:
318  
319 $chart->add_series(
320 categories => '=Sheet1!$A$2:$A$10',
321 values => '=Sheet1!$B$2:$B$10',
322 );
323  
324  
325 =cut
326 ###############################################################################
327 #
328 # xl_range_formula($sheetname, $row_1, $row_2, $col_1, $col_2)
329 #
330 sub xl_range_formula {
331  
332 my ($sheetname, $row_1, $row_2, $col_1, $col_2) = @_;
333  
334 # Use Excel's conventions and quote the sheet name if it contains any
335 # non-word character or if it isn't already quoted.
336 if ($sheetname =~ /\W/ && $sheetname !~ /^'/) {
337 $sheetname = q(') . $sheetname . q(');
338 }
339  
340 my $range1 = xl_rowcol_to_cell($row_1, $col_1, 1, 1);
341 my $range2 = xl_rowcol_to_cell($row_2, $col_2, 1, 1);
342  
343 return '=' . $sheetname . '!' . $range1 . ':' . $range2;
344 }
345  
346  
347  
348  
349 ###############################################################################
350 ###############################################################################
351  
352 =head2 xl_inc_row($string)
353  
354  
355 Parameters: $string, a string in A1 format
356  
357 Returns: Incremented string in A1 format
358  
359 This functions takes a cell reference string in A1 notation and increments the row. The function will also handle Excel's absolute, C<$>, cell notation:
360  
361 my $str = xl_inc_row('A1' ); # A2
362 my $str = xl_inc_row('B$2' ); # B$3
363 my $str = xl_inc_row('$C3' ); # $C4
364 my $str = xl_inc_row('$D$4'); # $D$5
365  
366  
367 =cut
368 ###############################################################################
369 #
370 # xl_inc_row($string)
371 #
372 sub xl_inc_row {
373  
374 my $cell = shift;
375 my ($row, $col, $row_abs, $col_abs) = xl_cell_to_rowcol($cell);
376  
377 return xl_rowcol_to_cell(++$row, $col, $row_abs, $col_abs);
378 }
379  
380  
381  
382  
383 ###############################################################################
384 ###############################################################################
385  
386 =head2 xl_dec_row($string)
387  
388  
389 Parameters: $string, a string in A1 format
390  
391 Returns: Decremented string in A1 format
392  
393 This functions takes a cell reference string in A1 notation and decrements the row. The function will also handle Excel's absolute, C<$>, cell notation:
394  
395 my $str = xl_dec_row('A2' ); # A1
396 my $str = xl_dec_row('B$3' ); # B$2
397 my $str = xl_dec_row('$C4' ); # $C3
398 my $str = xl_dec_row('$D$5'); # $D$4
399  
400  
401 =cut
402 ###############################################################################
403 #
404 # xl_dec_row($string)
405 #
406 # Decrements the row number of an Excel cell reference in A1 notation.
407 # For example C4 to C3
408 #
409 # Returns: a cell reference string.
410 #
411 sub xl_dec_row {
412  
413 my $cell = shift;
414 my ($row, $col, $row_abs, $col_abs) = xl_cell_to_rowcol($cell);
415  
416 return xl_rowcol_to_cell(--$row, $col, $row_abs, $col_abs);
417 }
418  
419  
420  
421  
422 ###############################################################################
423 ###############################################################################
424  
425 =head2 xl_inc_col($string)
426  
427  
428 Parameters: $string, a string in A1 format
429  
430 Returns: Incremented string in A1 format
431  
432 This functions takes a cell reference string in A1 notation and increments the column. The function will also handle Excel's absolute, C<$>, cell notation:
433  
434 my $str = xl_inc_col('A1' ); # B1
435 my $str = xl_inc_col('Z1' ); # AA1
436 my $str = xl_inc_col('$B1' ); # $C1
437 my $str = xl_inc_col('$D$5'); # $E$5
438  
439  
440 =cut
441 ###############################################################################
442 #
443 # xl_inc_col($string)
444 #
445 # Increments the column number of an Excel cell reference in A1 notation.
446 # For example C3 to D3
447 #
448 # Returns: a cell reference string.
449 #
450 sub xl_inc_col {
451  
452 my $cell = shift;
453 my ($row, $col, $row_abs, $col_abs) = xl_cell_to_rowcol($cell);
454  
455 return xl_rowcol_to_cell($row, ++$col, $row_abs, $col_abs);
456 }
457  
458  
459  
460  
461 ###############################################################################
462 ###############################################################################
463  
464 =head2 xl_dec_col($string)
465  
466 Parameters: $string, a string in A1 format
467  
468 Returns: Decremented string in A1 format
469  
470 This functions takes a cell reference string in A1 notation and decrements the column. The function will also handle Excel's absolute, C<$>, cell notation:
471  
472 my $str = xl_dec_col('B1' ); # A1
473 my $str = xl_dec_col('AA1' ); # Z1
474 my $str = xl_dec_col('$C1' ); # $B1
475 my $str = xl_dec_col('$E$5'); # $D$5
476  
477  
478 =cut
479 ###############################################################################
480 #
481 # xl_dec_col($string)
482 #
483 sub xl_dec_col {
484  
485 my $cell = shift;
486 my ($row, $col, $row_abs, $col_abs) = xl_cell_to_rowcol($cell);
487  
488 return xl_rowcol_to_cell($row, --$col, $row_abs, $col_abs);
489 }
490  
491  
492  
493  
494 =head1 TIME AND DATE FUNCTIONS
495  
496  
497 Dates and times in Excel are represented by real numbers, for example "Jan 1 2001 12:30 AM" is represented by the number 36892.521.
498  
499 The integer part of the number stores the number of days since the epoch and the fractional part stores the percentage of the day in seconds.
500  
501 The epoch can be either 1900 or 1904. Excel for Windows uses 1900 and Excel for Macintosh uses 1904. The epochs are:
502  
503 1900: 0 January 1900 i.e. 31 December 1899
504 1904: 1 January 1904
505  
506 Excel on Windows and the Macintosh will convert automatically between one system and the other. By default Spreadsheet::WriteExcel uses the 1900 format. To use the 1904 epoch you must use the C<set_1904()> workbook method, see the Spreadsheet::WriteExcel documentation.
507  
508 There are two things to note about the 1900 date format. The first is that the epoch starts on 0 January 1900. The second is that the year 1900 is erroneously but deliberately treated as a leap year. Therefore you must add an extra day to dates after 28 February 1900. The functions in the following section will deal with these issues automatically. The reason for this anomaly is explained at http://support.microsoft.com/support/kb/articles/Q181/3/70.asp
509  
510 Note, a date or time in Excel is like any other number. To display the number as a date you must apply a number format to it: Refer to the C<set_num_format()> method in the Spreadsheet::WriteExcel documentation:
511  
512 $date = xl_date_list(2001, 1, 1, 12, 30);
513 $format->set_num_format('mmm d yyyy hh:mm AM/PM');
514 $worksheet->write('A1', $date , $format); # Jan 1 2001 12:30 AM
515  
516 To use these functions you must install the C<Date::Manip> and C<Date::Calc> modules. See L<REQUIREMENTS> and the individual requirements of each functions.
517  
518 See also the DateTime::Format::Excel module,http://search.cpan.org/search?dist=DateTime-Format-Excel which is part of the DateTime project and which deals specifically with converting dates and times to and from Excel's format.
519  
520  
521 =cut
522  
523  
524 ###############################################################################
525 ###############################################################################
526  
527 =head2 xl_date_list($years, $months, $days, $hours, $minutes, $seconds)
528  
529  
530 Parameters: $years: Integer
531 $months: Integer [optional, default is 1]
532 $days: Integer [optional, default is 1]
533 $hours: Integer [optional, default is 0]
534 $minutes: Integer [optional, default is 0]
535 $seconds: Float [optional, default is 0]
536  
537 Returns: A number that represents an Excel date
538 or undef for an invalid date.
539  
540 Requires: Date::Calc
541  
542 This function converts an array of data into a number that represents an Excel date. All of the parameters are optional except for C<$years>.
543  
544 $date1 = xl_date_list(2002, 1, 2); # 2 Jan 2002
545 $date2 = xl_date_list(2002, 1, 2, 12); # 2 Jan 2002 12:00 pm
546 $date3 = xl_date_list(2002, 1, 2, 12, 30); # 2 Jan 2002 12:30 pm
547 $date4 = xl_date_list(2002, 1, 2, 12, 30, 45); # 2 Jan 2002 12:30:45 pm
548  
549 This function can be used in conjunction with functions that parse date and time strings. In fact it is used in most of the following functions.
550  
551  
552 =cut
553 ###############################################################################
554 #
555 # xl_date_list($years, $months, $days, $hours, $minutes, $seconds)
556 #
557 sub xl_date_list {
558  
559 return undef unless @_;
560  
561 my $years = $_[0];
562 my $months = $_[1] || 1;
563 my $days = $_[2] || 1;
564 my $hours = $_[3] || 0;
565 my $minutes = $_[4] || 0;
566 my $seconds = $_[5] || 0;
567  
568 my @date = ($years, $months, $days, $hours, $minutes, $seconds);
569 my @epoch = (1899, 12, 31, 0, 0, 0);
570  
571 ($days, $hours, $minutes, $seconds) = Delta_DHMS(@epoch, @date);
572  
573 my $date = $days + ($hours*3600 +$minutes*60 +$seconds)/(24*60*60);
574  
575 # Add a day for Excel's missing leap day in 1900
576 $date++ if ($date > 59);
577  
578 return $date;
579 }
580  
581  
582 ###############################################################################
583 ###############################################################################
584  
585 =head2 xl_parse_time($string)
586  
587  
588 Parameters: $string, a textual representation of a time
589  
590 Returns: A number that represents an Excel time
591 or undef for an invalid time.
592  
593 This function converts a time string into a number that represents an Excel time. The following time formats are valid:
594  
595 hh:mm [AM|PM]
596 hh:mm [AM|PM]
597 hh:mm:ss [AM|PM]
598 hh:mm:ss.ss [AM|PM]
599  
600  
601 The meridian, AM or PM, is optional and case insensitive. A 24 hour time is assumed if the meridian is omitted
602  
603 $time1 = xl_parse_time('12:18');
604 $time2 = xl_parse_time('12:18:14');
605 $time3 = xl_parse_time('12:18:14 AM');
606 $time4 = xl_parse_time('1:18:14 AM');
607  
608 Time in Excel is expressed as a fraction of the day in seconds. Therefore you can calculate an Excel time as follows:
609  
610 $time = ($hours*3600 +$minutes*60 +$seconds)/(24*60*60);
611  
612  
613 =cut
614 ###############################################################################
615 #
616 # xl_parse_time($string)
617 #
618 sub xl_parse_time {
619  
620 my $time = shift;
621  
622 if ($time =~ /(\d{1,2}):(\d\d):?((?:\d\d)(?:\.\d+)?)?(?:\s+)?(am|pm)?/i) {
623  
624 my $hours = $1;
625 my $minutes = $2;
626 my $seconds = $3 || 0;
627 my $meridian = lc($4) || '';
628  
629 # Normalise midnight and midday
630 $hours = 0 if ($hours == 12 && $meridian ne '');
631  
632 # Add 12 hours to the pm times. Note: 12.00 pm has been set to 0.00.
633 $hours += 12 if $meridian eq 'pm';
634  
635 # Calculate the time as a fraction of 24 hours in seconds
636 return ($hours*3600 +$minutes*60 +$seconds)/(24*60*60);
637  
638 }
639 else {
640 return undef; # Not a valid time string
641 }
642 }
643  
644  
645 ###############################################################################
646 ###############################################################################
647  
648 =head2 xl_parse_date($string)
649  
650  
651 Parameters: $string, a textual representation of a date and time
652  
653 Returns: A number that represents an Excel date
654 or undef for an invalid date.
655  
656 Requires: Date::Manip and Date::Calc
657  
658 This function converts a date and time string into a number that represents an Excel date.
659  
660 The parsing is performed using the C<ParseDate()> function of the Date::Manip module. Refer to the Date::Manip documentation for further information about the date and time formats that can be parsed. In order to use this function you will probably have to initialise some Date::Manip variables via the C<xl_parse_date_init()> function, see below.
661  
662 xl_parse_date_init("TZ=GMT","DateFormat=non-US");
663  
664 $date1 = xl_parse_date("11/7/97");
665 $date2 = xl_parse_date("Friday 11 July 1997");
666 $date3 = xl_parse_date("10:30 AM Friday 11 July 1997");
667 $date4 = xl_parse_date("Today");
668 $date5 = xl_parse_date("Yesterday");
669  
670 Note, if you parse a string that represents a time but not a date this function will add the current date. If you want the time without the date you can do something like the following:
671  
672 $time = xl_parse_date("10:30 AM");
673 $time -= int($time);
674  
675  
676 =cut
677 ###############################################################################
678 #
679 # xl_parse_date($string)
680 #
681 sub xl_parse_date {
682  
683 my $date = ParseDate($_[0]);
684  
685 return undef unless defined $date;
686  
687 # Unpack the return value from ParseDate()
688 my ($years, $months, $days, $hours, undef, $minutes, undef, $seconds) =
689 unpack("A4 A2 A2 A2 C A2 C A2", $date);
690  
691 # Convert to Excel date
692 return xl_date_list($years, $months, $days, $hours, $minutes, $seconds);
693 }
694  
695  
696  
697  
698 ###############################################################################
699 ###############################################################################
700  
701 =head2 xl_parse_date_init("variable=value", ...)
702  
703  
704 Parameters: A list of Date::Manip variable strings
705  
706 Returns: A list of all the Date::Manip strings
707  
708 Requires: Date::Manip
709  
710 This function is used to initialise variables required by the Date::Manip module. You should call this function before calling C<xl_parse_date()>. It need only be called once.
711  
712 This function is a thin wrapper for the C<Date::Manip::Date_Init()> function. You can use C<Date_Init()> directly if you wish. Refer to the Date::Manip documentation for further information.
713  
714 xl_parse_date_init("TZ=MST","DateFormat=US");
715 $date1 = xl_parse_date("11/7/97"); # November 7th 1997
716  
717 xl_parse_date_init("TZ=GMT","DateFormat=non-US");
718 $date1 = xl_parse_date("11/7/97"); # July 11th 1997
719  
720  
721 =cut
722 ###############################################################################
723 #
724 # xl_parse_date_init("variable=value", ...)
725 #
726 sub xl_parse_date_init {
727  
728 Date_Init(@_); # How lazy is that.
729 }
730  
731  
732  
733  
734 ###############################################################################
735 ###############################################################################
736  
737 =head2 xl_decode_date_EU($string)
738  
739  
740 Parameters: $string, a textual representation of a date and time
741  
742 Returns: A number that represents an Excel date
743 or undef for an invalid date.
744  
745 Requires: Date::Calc
746  
747 This function converts a date and time string into a number that represents an Excel date.
748  
749 The date parsing is performed using the C<Decode_Date_EU()> function of the Date::Calc module. Refer to the Date::Calc for further information about the date formats that can be parsed. Also note the following from the Date::Calc documentation:
750  
751 "If the year is given as one or two digits only (i.e., if the year is less than 100), it is mapped to the window 1970 -2069 as follows":
752  
753 0 E<lt>= $year E<lt> 70 ==> $year += 2000;
754 70 E<lt>= $year E<lt> 100 ==> $year += 1900;
755  
756 The time portion of the string is parsed using the C<xl_parse_time()> function described above.
757  
758 Note: the EU in the function name means that a European date format is assumed if it is not clear from the string. See the first example below.
759  
760 $date1 = xl_decode_date_EU("11/7/97"); #11 July 1997
761 $date2 = xl_decode_date_EU("Sat 12 Sept 1998");
762 $date3 = xl_decode_date_EU("4:30 AM Sat 12 Sept 1998");
763  
764  
765 =cut
766 ###############################################################################
767 #
768 # xl_decode_date_EU($string)
769 #
770 sub xl_decode_date_EU {
771  
772 return undef unless @_;
773  
774 my $date = shift;
775 my @date;
776 my $time = 0;
777  
778 # Remove and decode the time portion of the string
779 if ($date =~ s/(\d{1,2}:\d\d:?(\d\d(\.\d+)?)?(\s+)?(am|pm)?)//i) {
780 $time = xl_parse_time($1);
781 return undef unless defined $time;
782 }
783  
784 # Return if the string is now blank, i.e. it contained a time only.
785 return $time if $date =~ /^\s*$/;
786  
787 # Decode the date portion of the string
788 @date = Decode_Date_EU($date);
789 return undef unless @date;
790  
791 return xl_date_list(@date) + $time;
792 }
793  
794  
795  
796 ###############################################################################
797 ###############################################################################
798  
799 =head2 xl_decode_date_US($string)
800  
801  
802 Parameters: $string, a textual representation of a date and time
803  
804 Returns: A number that represents an Excel date
805 or undef for an invalid date.
806  
807 Requires: Date::Calc
808  
809 This function converts a date and time string into a number that represents an Excel date.
810  
811 The date parsing is performed using the C<Decode_Date_US()> function of the Date::Calc module. Refer to the Date::Calc for further information about the date formats that can be parsed. Also note the following from the Date::Calc documentation:
812  
813 "If the year is given as one or two digits only (i.e., if the year is less than 100), it is mapped to the window 1970 -2069 as follows":
814  
815 0 <= $year < 70 ==> $year += 2000;
816 70 <= $year < 100 ==> $year += 1900;
817  
818 The time portion of the string is parsed using the C<xl_parse_time()> function described above.
819  
820 Note: the US in the function name means that an American date format is assumed if it is not clear from the string. See the first example below.
821  
822 $date1 = xl_decode_date_US("11/7/97"); # 7 November 1997
823 $date2 = xl_decode_date_US("12 Sept Saturday 1998");
824 $date3 = xl_decode_date_US("4:30 AM 12 Sept Sat 1998");
825  
826  
827 =cut
828 ###############################################################################
829 #
830 # xl_decode_date_US($string)
831 #
832 sub xl_decode_date_US {
833  
834 return undef unless @_;
835  
836 my $date = shift;
837 my @date;
838 my $time = 0;
839  
840 # Remove and decode the time portion of the string
841 if ($date =~ s/(\d{1,2}:\d\d:?(\d\d(\.\d+)?)?(\s+)?(am|pm)?)//i) {
842 $time = xl_parse_time($1);
843 return undef unless defined $time;
844 }
845  
846 # Return if the string is now blank, i.e. it contained a time only.
847 return $time if $date =~ /^\s*$/;
848  
849 # Decode the date portion of the string
850 @date = Decode_Date_US($date);
851 return undef unless @date;
852  
853 return xl_date_list(@date) + $time;
854 }
855  
856  
857  
858  
859 ###############################################################################
860 ###############################################################################
861  
862 =head2 xl_date_1904($date)
863  
864  
865 Parameters: $date, an Excel date with a 1900 epoch
866  
867 Returns: an Excel date with a 1904 epoch or zero if
868 the $date is before 1904
869  
870  
871 This function converts an Excel date based on the 1900 epoch into a date based on the 1904 epoch.
872  
873  
874 $date1 = xl_date_list(2002, 1, 13); # 13 Jan 2002, 1900 epoch
875 $date2 = xl_date_1904($date1); # 13 Jan 2002, 1904 epoch
876  
877  
878 See also the C<set_1904()> workbook method in the Spreadsheet::WriteExcel documentation.
879  
880 =cut
881 ###############################################################################
882 #
883 # xl_decode_date_US($string)
884 #
885 sub xl_date_1904 {
886  
887 my $date = $_[0] || 0;
888  
889 if ($date < 1462) {
890 # before 1904
891 $date = 0;
892 }
893 else {
894 $date -= 1462;
895 }
896  
897 return $date;
898 }
899  
900  
901  
902  
903  
904 =head1 REQUIREMENTS
905  
906 The date and time functions require functions from the C<Date::Manip> and C<Date::Calc> modules. The required functions are "autoused" from these modules so that you do not have to install them unless you wish to use the date and time routines. Therefore it is possible to use the row and column functions without having C<Date::Manip> and C<Date::Calc> installed.
907  
908 For more information about "autousing" refer to the documentation on the C<autouse> pragma.
909  
910  
911  
912 =head1 BUGS
913  
914 When using the autoused functions from C<Date::Manip> and C<Date::Calc> on Perl 5.6.0 with C<-w> you will get a warning like this:
915  
916 "Subroutine xxx redefined ..."
917  
918 The current workaround for this is to put C<use warnings;> near the beginning of your program.
919  
920  
921  
922 =head1 AUTHOR
923  
924 John McNamara jmcnamara@cpan.org
925  
926  
927  
928  
929 =head1 COPYRIGHT
930  
931 Copyright MM-MMX, John McNamara.
932  
933 All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.
934  
935 =cut
936  
937  
938  
939 1;
940  
941  
942 __END__