-
Notifications
You must be signed in to change notification settings - Fork 2
/
ParseExcelFormatStrings.pm
1974 lines (1747 loc) · 74.6 KB
/
ParseExcelFormatStrings.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Spreadsheet::XLSX::Reader::LibXML::ParseExcelFormatStrings;
use version; our $VERSION = qv('v0.36.24');
use 5.010;
use Moose::Role;
requires
'get_excel_region',
###LogSD 'get_log_space',
;
use Types::Standard qw(
Int Str Maybe
Num HashRef ArrayRef
CodeRef Object ConsumerOf
InstanceOf HasMethods Bool
is_Object is_Num is_Int
);
use Carp qw( confess );# cluck
use Type::Coercion;
use Type::Tiny;
use DateTimeX::Format::Excel 0.012;
use DateTime::Format::Flexible;
use Clone 'clone';
use lib '../../../../../lib',;
###LogSD use Log::Shiras::Telephone;
###LogSD use Log::Shiras::UnhideDebug;
use Spreadsheet::XLSX::Reader::LibXML::Types qw(
PositiveNum NegativeNum
ZeroOrUndef NotNegativeNum
Excel_number_0
);#
#########1 Dispatch Tables & Package Variables 5#########6#########7#########8#########9
my $coercion_index = 0;
my @type_list = ( PositiveNum, NegativeNum, ZeroOrUndef, Str );
my $last_date_cldr = 'yyyy-m-d';
my $last_duration = 0;
my $last_sub_seconds = 0;
my $last_format_rem = 0;
my $duration_order ={ h => 'm', m =>'s', s =>'0' };
my $number_build_dispatch ={
all =>[qw(
_convert_negative
_divide_by_thousands
_convert_to_percent
_split_decimal_integer
_move_decimal_point
_build_fraction
_round_decimal
_add_commas
_pad_exponent
)],
scientific =>[qw(
_convert_negative
_split_decimal_integer
_move_decimal_point
_round_decimal
_add_commas
_pad_exponent
)],
percent =>[qw(
_convert_negative
_convert_to_percent
_split_decimal_integer
_round_decimal
_add_commas
)],
fraction =>[qw(
_convert_negative
_split_decimal_integer
_build_fraction
_add_commas
)],
integer =>[qw(
_convert_negative
_divide_by_thousands
_split_decimal_integer
_round_decimal
_add_commas
)],
decimal =>[qw(
_convert_negative
_divide_by_thousands
_split_decimal_integer
_round_decimal
_add_commas
)],
};
#########1 Public Attributes 3#########4#########5#########6#########7#########8#########9
has epoch_year =>( # Move to required?
isa => Int,
reader => 'get_epoch_year',
default => 1900,
);
has cache_formats =>(
isa => Bool,
reader => 'get_cache_behavior',
writer => 'set_cache_behavior',
default => 1,
);
has datetime_dates =>(
isa => Bool,
reader => 'get_date_behavior',
writer => 'set_date_behavior',
default => 0,
);
has format_cash =>(
isa => HashRef,
traits => ['Hash'],
handles =>{
has_cached_format => 'exists',
get_cached_format => 'get',
set_cached_format => 'set',
},
default => sub{ {} },
);
#########1 Public Methods 3#########4#########5#########6#########7#########8#########9
sub parse_excel_format_string{# Currently only handles dates and times
my( $self, $format_strings, $coercion_name ) = @_;
###LogSD my $phone = Log::Shiras::Telephone->new(
###LogSD name_space => $self->get_log_space . '::parse_excel_format_string', );
if( !defined $format_strings ){
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Nothing passed to convert",] );
return Excel_number_0;
}
$format_strings =~ s/\\//g;
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "parsing the custom excel format string: $format_strings",] );
my $conversion_type = 'number';
# Check the cache
my $cache_key;
if( $self->get_cache_behavior ){
$cache_key = $format_strings; # TODO fix the non-hashkey character issues;
if( $self->has_cached_format( $cache_key ) ){
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Format already built - returning stored value for: $cache_key", ] );
return $self->get_cached_format( $cache_key );
}else{
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Building new format for key: $cache_key", ] );
}
}
# Split into the four sections positive, negative, zero, and text
$format_strings =~ s/General/\@/ig;# Change General to text input
my @format_string_list = split /;/, $format_strings;
my $last_is_text = ( $format_string_list[-1] =~ /\@/ ) ? 1 : 0 ;
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Is the last position text: $last_is_text", ] );
# Make sure the full range of number inputs are sent down the right path;
my @used_type_list = @{\@type_list};
$used_type_list[0] =
( scalar( @format_string_list ) - $last_is_text == 1 ) ? Maybe[Num] :
( scalar( @format_string_list ) - $last_is_text == 2 ) ? Maybe[NotNegativeNum] : $type_list[0] ;
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Now operating on each format string", @format_string_list,
###LogSD '..with used type list:', map{ $_->name } @used_type_list, ] );
my $format_position = 0;
my @coercion_list;
my $action_type;
my $is_date = 0;
my $date_text = 0;
for my $format_string ( @format_string_list ){
$format_string =~ s/_.//g;# no character justification to other rows
$format_string =~ s/\*//g;# Remove the repeat character listing (not supported here)
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Building format for: $format_string", ] );
# Pull out all the straight through stuff
my @deconstructed_list;
my $x = 0;
while( defined $format_string and my @result = $format_string =~
/^( # Collect any formatting stuff first
(AM\/PM| # Date 12 hr flag
A\/P| # Another date 12 hr flag
\[hh?\]| # Elapsed hours
\[mm\]| # Elapsed minutes
\[ss\]| # Elapsed seconds
[dmyhms]+)| # DateTime chunks
([0-9#\?]+[,\-\_]?[#0\?]*,*| # Number string
\.| # Split integers from decimals
[Ee][+\-]| # Exponential notiation
%)| # Percentage
(\@) # Text input
)?( # Finish collecting format actions
(\"[^\"]*\")| # Anything in quotes just passes through
(\[[^\]]*\])| # Anything in brackets needs modification
[\(\)\$\-\+\/\:\!\^\&\'\~\{\}\<\>\=\s]| # All the pass through characters
\,\s # comma space for verbal separation
)?(.*)/x ){
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Now processing: $format_string", '..with result:', @result ] );
my $pre_action = $1;
my $date = $2;
my $number = $3;
my $text = $4;
my $fixed_value = $5;
$format_string = $8;
if( $fixed_value ){
if( $fixed_value =~ /\[\$([^\-\]]*)\-?\d*\]/ ){# removed the localized element of fixed values
$fixed_value = $1;
}elsif( $fixed_value =~ /\[[^hms]*\]/ ){# Remove all color and conditionals as they will not be used
$fixed_value = undef;
}elsif( $fixed_value =~ /\"\-\"/ and $format_string ){# remove decimal justification for zero bars
###LogSD $phone->talk( level => 'trace', message => [
###LogSD "Initial format string: $format_string", ] );
$format_string =~ s/^(\?+)//;
###LogSD $phone->talk( level => 'trace', message => [
###LogSD "updated format string: $format_string", ] );
}
}
if( defined $pre_action ){
my $current_action =
( $date ) ? 'DATE' :
( defined $number ) ? 'NUMBER' :
( $text ) ? 'TEXT' : 'BAD' ;
$is_date = 1 if $date;
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Current action from -$pre_action- is: $current_action" ] );
if( $action_type and $current_action and ($current_action ne $action_type) ){
###LogSD $phone->talk( level => 'info', message => [
###LogSD "General action type: $action_type",,
###LogSD "is failing current action: $current_action", ] );
my $fail = 1;
if( $action_type eq 'DATE' ){
$conversion_type = 'date';
###LogSD $phone->talk( level => 'info', message => [
###LogSD "Checking the date mishmash", ] );
if( $current_action eq 'NUMBER' ){
###LogSD $phone->talk( level => 'info', message => [
###LogSD "Special case of number following action", ] );
if( ( $pre_action =~ /^\.$/ and $format_string =~ /^0+/ ) or
( $pre_action =~ /^0+$/ and $deconstructed_list[-1]->[0] =~ /^\.$/ ) ){
$current_action = 'DATE';
$fail = 0;
}
}elsif( $pre_action eq '@' ){
###LogSD $phone->talk( level => 'info', message => [
###LogSD "Excel conversion of pre-epoch datestring pass through highjacked here", ] );
$current_action = 'DATESTRING';
$fail = 0;
}
}elsif( $action_type eq 'NUMBER' ){
###LogSD $phone->talk( level => 'info', message => [
###LogSD "Checking for possible text in a number field for a pass throug", ] );
if( $current_action eq 'TEXT' ){
###LogSD $phone->talk( level => 'info', message => [
###LogSD "Special case of text following a number", ] );
$fail = 0;
}
}
if( $fail ){
confess "Bad combination of actions in this format string: $format_strings - $action_type - $current_action";
}
}
$action_type = $current_action if $current_action;
###LogSD $phone->talk( level => 'debug', message => [
###LogSD (($pre_action) ? "First action resolved to: $pre_action" : undef),
###LogSD (($fixed_value) ? "Extracted fixed value: $fixed_value" : undef),
###LogSD (($format_string) ? "Remaining string: $format_string" : undef),
###LogSD "With updated deconstruction list:", @deconstructed_list, ] );
}else{
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Early elements unusable - remaining string: $format_string", ] );
}
push @deconstructed_list, [ $pre_action, $fixed_value ];
if( $x++ == 30 ){
confess "Regex matching failed (with an infinite loop) for excel format string: $format_string";
}
###LogSD $phone->talk( level => 'debug', message => [
###LogSD (($pre_action) ? "First action resolved to: $pre_action" : undef),
###LogSD (($fixed_value) ? "Extracted fixed value: $fixed_value" : undef),
###LogSD (($format_string) ? "Remaining string: $format_string" : undef),
###LogSD "With updated deconstruction list:", @deconstructed_list, ] );
last if length( $format_string ) == 0;
}
push @deconstructed_list, [ $format_string, undef ] if $format_string;
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Current segment type: $action_type",
###LogSD "List with fixed values separated:", @deconstructed_list ] );
my $method = '_build_' . lc($action_type);
my $filter = ( $action_type eq 'TEXT' ) ? Str : $used_type_list[$format_position++];
if( $action_type eq 'DATESTRING' ){
$date_text = 1;
$filter = Str;
}
push @coercion_list, $self->$method( $filter, \@deconstructed_list );
}
push @coercion_list, $self->_build_datestring( Str, [ [ '@', '' ] ] ) if $is_date and !$date_text;
###LogSD $phone->talk( level => 'debug', message => [
###LogSD 'Length of coersion list: ' . scalar( @coercion_list ),
###LogSD (map{ if( is_Object( $_ ) and $_->can( 'name' ) ){ $_->name }else{ $_ } } @coercion_list), ] );
# Build the final format
$coercion_name =~ s/__/_${conversion_type}_/ if $coercion_name;
my %args = (
name => ($coercion_name // ($action_type . '_' . $coercion_index++)),
coercion => Type::Coercion->new(
type_coercion_map => [ @coercion_list ],
),
#~ coerce => 1,
);
my $final_type = Type::Tiny->new( %args );
# Save the cache
$self->set_cached_format( $cache_key => $final_type ) if $self->get_cache_behavior;
return $final_type;
}
#########1 Private Attributes 3#########4#########5#########6#########7#########8#########9
#########1 Private Methods 3#########4#########5#########6#########7#########8#########9
sub _build_text{
my( $self, $type_filter, $list_ref ) = @_;
###LogSD my $phone = Log::Shiras::Telephone->new( name_space =>
###LogSD $self->get_log_space . '::_build_text', );
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Building an anonymous sub to process text values" ] );
my $sprintf_string;
my $found_string = 0;
for my $piece ( @$list_ref ){
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "processing text piece:", $piece ] );
if( !$found_string and defined $piece->[0] ){
$sprintf_string .= '%s';
$found_string = 1;
}
if( $piece->[1] ){
$sprintf_string .= $piece->[1];
}
}
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Final sprintf string: $sprintf_string" ] );
my $return_sub = sub{
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Updated Input: $_[0]" ] );
return sprintf( $sprintf_string, $_[0] );
};
return( Str, $return_sub );
}
sub _build_date{
my( $self, $type_filter, $list_ref ) = @_;
###LogSD my $phone = Log::Shiras::Telephone->new( name_space =>
###LogSD $self->get_log_space . '::_build_date', );
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Building an anonymous sub to process date values" ] );
my ( $cldr_string, $format_remainder );
my $is_duration = 0;
my $sub_seconds = 0;
if( !$self->get_date_behavior ){
# Process once to build the cldr string
my $prior_duration;
for my $piece ( @$list_ref ){
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "processing date piece:", $piece ] );
if( defined $piece->[0] ){
###LogSD $phone->talk( level => 'debug', message =>[
###LogSD "Manageing the cldr part: " . $piece->[0] ] );
if( $piece->[0] =~ /\[(.+)\]/ ){
###LogSD $phone->talk( level => 'debug', message =>[ "Possible duration" ] );
(my $initial,) = split //, $1;
my $length = length( $1 );
$is_duration = [ $initial, 0, [ $piece->[1] ], [ $length ] ];
if( $is_duration->[0] =~ /[hms]/ ){
$piece->[0] = '';
$piece->[1] = '';
$prior_duration = $is_duration->[0];
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "found a duration piece:", $is_duration,
###LogSD "with prior duration: $prior_duration" ] );
}else{
confess "Bad duration element found: $is_duration->[0]";
}
}elsif( ref( $is_duration ) eq 'ARRAY' ){
###LogSD $phone->talk( level => 'debug', message =>[ "adding to duration", $piece ] );
my $next_duration = $duration_order->{$prior_duration};
if( $piece->[0] eq '.' ){
push @{$is_duration->[2]}, $piece->[0];
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "found a period" ] );
}elsif( $piece->[0] =~ /$next_duration/ ){
my $length = length( $piece->[0] );
$is_duration->[1]++;
push @{$is_duration->[2]}, $piece->[1] if $piece->[1];
push @{$is_duration->[3]}, $length;
($prior_duration,) = split //, $piece->[0];
if( $piece->[0] =~ /^0+$/ ){
$piece->[0] =~ s/0/S/g;
$sub_seconds = $piece->[0];
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "found a subseconds format piece: $sub_seconds" ] );
}
$piece->[0] = '';
$piece->[1] = '';
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Current duration:", $is_duration,
###LogSD "with prior duration: $prior_duration" ] );
}else{
confess "Bad duration element found: $piece->[0]";
}
}elsif( $piece->[0] =~ /m/ ){
###LogSD $phone->talk( level => 'debug', message =>[ "Minutes or Months" ] );
if( ($cldr_string and $cldr_string =~ /:'?$/) or ($piece->[1] and $piece->[1] eq ':') ){
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Found minutes - leave them alone" ] );
}else{
$piece->[0] =~ s/m/L/g;
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Converting to cldr stand alone months (m->L)" ] );
}
}elsif( $piece->[0] =~ /h/ ){
$piece->[0] =~ s/h/H/g;
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Converting 12 hour clock to 24 hour clock" ] );
}elsif( $piece->[0] =~ /AM?\/PM?/i ){
$cldr_string =~ s/H/h/g;
$piece->[0] = 'a';
###LogSD $phone->talk( level => 'debug', message =>[ "Set 12 hour clock and AM/PM" ] );
}elsif( $piece->[0] =~ /d{3,5}/ ){
$piece->[0] =~ s/d/E/g;
###LogSD $phone->talk( level => 'debug', message =>[ "Found a weekday request" ] );
}elsif( !$sub_seconds and $piece->[0] =~ /[\.]/){#
$piece->[0] = "'.'";
#~ $piece->[0] = "':'";
$sub_seconds = 1;
###LogSD $phone->talk( level => 'debug', message =>[ "Starting sub seconds" ] );
}elsif( $sub_seconds eq '1' ){
###LogSD $phone->talk( level => 'debug', message =>[ "Formatting sub seconds" ] );
if( $piece->[0] =~ /^0+$/ ){
$piece->[0] =~ s/0/S/g;
$sub_seconds = $piece->[0];
$piece->[0] = '';
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "found a subseconds format piece: $sub_seconds" ] );
}else{
confess "Bad sub-seconds element after [$cldr_string] found: $piece->[0]";
}
}
if( $sub_seconds and $sub_seconds ne '1' ){
$format_remainder .= $piece->[0];
}else{
$cldr_string .= $piece->[0];
}
}
if( $piece->[1] ){
if( $sub_seconds and $sub_seconds ne '1' ){
$format_remainder .= $piece->[1];
}else{
$cldr_string .= $piece->[1];
}
}
###LogSD $phone->talk( level => 'debug', message => [
###LogSD (($cldr_string) ? "Updated CLDR string: $cldr_string" : undef),
###LogSD (($format_remainder) ? "Updated format remainder: $format_remainder" : undef),
###LogSD (($is_duration) ? ('Duration ref:', $is_duration) : undef) ] );
}
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Updated CLDR string: $cldr_string",
###LogSD (($is_duration) ? ('...and duration:', $is_duration) : undef ) ] );
$last_date_cldr = $cldr_string;
$last_duration = $is_duration;
$last_sub_seconds = $sub_seconds;
$last_format_rem = $format_remainder;
}
my @args_list = ( $self->get_epoch_year == 1904 ) ? ( system_type => 'apple_excel' ) : ();
my $converter = DateTimeX::Format::Excel->new( @args_list );
my $conversion_sub = sub{
my $num = $_[0];
if( !defined $num ){
return undef;
}
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Processing date number: $num",
###LogSD '..with duration:', $is_duration,
###LogSD "..and sub-seconds: $sub_seconds",
###LogSD (($format_remainder) ? "..and format_remainder: $format_remainder" : undef) ] );
my $dt = $converter->parse_datetime( $num );
my $return_string;
my $calc_sub_secs;
if( $is_duration ){
my $di = $dt->subtract_datetime_absolute( $converter->_get_epoch_start );
if( $self->get_date_behavior ){
return $di;
}
my $sign = DateTime->compare_ignore_floating( $dt, $converter->_get_epoch_start );
$return_string = ( $sign == -1 ) ? '-' : '' ;
my $key = $is_duration->[0];
my $delta_seconds = $di->seconds;
my $delta_nanosecs = $di->nanoseconds;
$return_string .= $self->_build_duration( $is_duration, $delta_seconds, $delta_nanosecs );
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Duration return string: $return_string" ] );
}else{
if( $self->get_date_behavior ){
return $dt;
}
if( $sub_seconds ){
$calc_sub_secs = $dt->format_cldr( $sub_seconds );
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Processing sub-seconds: $calc_sub_secs" ] );
if( "0.$calc_sub_secs" >= 0.5 ){
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Rounding seconds back down" ] );
$dt->subtract( seconds => 1 );
}
}
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Converting it with CLDR string: $cldr_string" ] );
$return_string .= $dt->format_cldr( $cldr_string );
if( $sub_seconds and $sub_seconds ne '1' ){
$return_string .= $calc_sub_secs;
}
$return_string .= $dt->format_cldr( $format_remainder ) if $format_remainder;
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "returning: $return_string" ] );
}
return $return_string;
};
return( $type_filter, $conversion_sub );
}
sub _build_datestring{
my( $self, $type_filter, $list_ref ) = @_;
###LogSD my $phone = Log::Shiras::Telephone->new( name_space =>
###LogSD $self->get_log_space . '::_build_datestring', );
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Building an anonymous sub to process date strings" ] );
my ( $cldr_string, $format_remainder );
my $conversion_sub = sub{
my $date = $_[0];
if( !$date ){
return undef;
}
my $calc_sub_secs;
if( $date =~ /(.*:\d+)\.(\d+)(.*)/ ){
$calc_sub_secs = $2;
$date = $1;
$date .= $3 if $3;
$calc_sub_secs .= 0 x (9 - length( $calc_sub_secs ));
}
my $dt = DateTime::Format::Flexible->parse_datetime(
$date, lang =>[ $self->get_excel_region ]
);
$dt->add( nanoseconds => $calc_sub_secs ) if $calc_sub_secs;
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Processing date string: $date",
###LogSD "..with duration:", $last_duration,
###LogSD "..and sub-seconds: $last_sub_seconds",
###LogSD "..and stripped nanoseconds: $calc_sub_secs" ] );
my $return_string;
if( $last_duration ){
my @args_list = ( $self->get_epoch_year == 1904 ) ? ( system_type => 'apple_excel' ) : ();
my $converter = DateTimeX::Format::Excel->new( @args_list );
my $di = $dt->subtract_datetime_absolute( $converter->_get_epoch_start );
if( $self->get_date_behavior ){
return $di;
}
my $sign = DateTime->compare_ignore_floating( $dt, $converter->_get_epoch_start );
$return_string = ( $sign == -1 ) ? '-' : '' ;
my $key = $last_duration->[0];
my $delta_seconds = $di->seconds;
my $delta_nanosecs = $di->nanoseconds;;
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Delta seconds: $delta_seconds",
###LogSD (($delta_nanosecs) ? "Delta nanoseconds: $delta_nanosecs" : undef) ] );
$return_string .= $self->_build_duration( $last_duration, $delta_seconds, $delta_nanosecs );
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Duration return string: $return_string" ] );
}else{
if( $self->get_date_behavior ){
return $dt;
}
if( $last_sub_seconds ){
$calc_sub_secs = $dt->format_cldr( $last_sub_seconds );
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Processing sub-seconds: $calc_sub_secs" ] );
if( "0.$calc_sub_secs" >= 0.5 ){
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Rounding seconds back down" ] );
$dt->subtract( seconds => 1 );
}
}
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Converting it with CLDR string: $last_date_cldr" ] );
$return_string .= $dt->format_cldr( $last_date_cldr );
if( $last_sub_seconds and $last_sub_seconds ne '1' ){
$return_string .= $calc_sub_secs;
}
$return_string .= $dt->format_cldr( $last_format_rem ) if $last_format_rem;
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "returning: $return_string" ] );
}
return $return_string;
};
return( $type_filter, $conversion_sub );
}
sub _build_duration{
my( $self, $duration_ref, $delta_seconds, $delta_nanosecs ) = @_;
###LogSD my $phone = Log::Shiras::Telephone->new( name_space =>
###LogSD $self->get_log_space . '::_build_date::_build_duration', );
###LogSD $phone->talk( level => 'debug', message => [
###LogSD 'Building a duration string with duration ref:', $duration_ref,
###LogSD "With delta seconds: $delta_seconds",
###LogSD (($delta_nanosecs) ? "And delta nanoseconds: $delta_nanosecs" : undef) ] );
my $return_string;
my $key = $duration_ref->[0];
my $first = 1;
for my $position ( 0 .. $duration_ref->[1] ){
if( $key eq '0' ){
my $length = length( $last_sub_seconds );
$return_string .= '.' . sprintf( "%0.${length}f", $delta_nanosecs/1000000000);
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Return string with nanoseconds: $return_string", ] );
}
if( $key eq 's' ){
$return_string .= ( $first ) ? $delta_seconds :
sprintf "%0$duration_ref->[3]->[$position]d", $delta_seconds;
$first = 0;
$key = $duration_order->{$key};
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Delta seconds: $delta_seconds",
###LogSD "Next key to process: $key" ] );
}
if( $key eq 'm' ){
my $minutes = int($delta_seconds/60);
$delta_seconds = $delta_seconds - ($minutes*60);
$return_string .= ( $first ) ? $minutes :
sprintf "%0$duration_ref->[3]->[$position]d", $minutes;
$first = 0;
$key = $duration_order->{$key};
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Calculated minutes: $minutes",
###LogSD "Remaining seconds: $delta_seconds",
###LogSD "Next key to process: $key" ] );
}
if( $key eq 'h' ){
my $hours = int($delta_seconds /(60*60));
$delta_seconds = $delta_seconds - ($hours*60*60);
$return_string .= ( $first ) ? $hours :
sprintf "%0$duration_ref->[3]->[$position]d", $hours;
$first = 0;
$key = $duration_order->{$key};
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Calculated hours: $hours",
###LogSD "Remaining seconds: $delta_seconds",
###LogSD "Next key to process: $key" ] );
}
$return_string .= $duration_ref->[2]->[$position] if $duration_ref->[2]->[$position];
}
return $return_string;
}
sub _build_number{
my( $self, $type_filter, $list_ref ) = @_;
###LogSD my $phone = Log::Shiras::Telephone->new( name_space =>
###LogSD $self->get_log_space . '::_build_number', );
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Processing a number list to see how it should be converted",
###LogSD 'With type constraint: ' . $type_filter->name,
###LogSD '..using list ref:' , $list_ref ] );
my ( $code_hash_ref, $number_type, );
# Resolve zero replacements quickly
if( $type_filter->name eq 'ZeroOrUndef' and
!$list_ref->[-1]->[0] and $list_ref->[-1]->[1] eq '"-"' ){
###LogSD $phone->talk( level => 'debug', message =>[
###LogSD "Found a zero to bar replacement" ] );
my $return_string;
for my $piece ( @$list_ref ){
$return_string .= $piece->[1];
}
$return_string =~ s/"\-"/\-/;
return( $type_filter, sub{ $return_string } );
}
# Process once to determine what to do
for my $piece ( @$list_ref ){
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "processing number piece:", $piece ] );
if( defined $piece->[0] ){
if( my @result = $piece->[0] =~ /^([0-9#\?]+)([,\-\_])?([#0\?]+)?(,+)?$/ ){
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Regex yielded result:", @result ] );
my $comma = ($2) ? $2 : undef,
my $comma_less = defined( $3) ? "$1$3" : $1;
my $comma_group = $3 ? length( $3 ) : 0;
my $divide_by_thousands = ( $4 ) ? (( $2 and $2 ne ',' ) ? $4 : "$2$4" ) : undef;#eval{ $2 . $4 }
my $divisor = $1 if $1 =~ /^([0-9]+)$/;
my ( $leading_zeros, $trailinq_zeros );
if( $comma_less =~ /^[\#\?]*(0+)$/ ){
$leading_zeros = $1;
}
if( $comma_less =~ /^(0+)[\#\?]*$/ ){
$trailinq_zeros = $1;
}
$code_hash_ref->{divide_by_thousands} = length( $divide_by_thousands ) if $divide_by_thousands;
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "The comma less string is extracted to: $comma_less",
###LogSD ((defined $comma_group) ? "The separator group length is: $comma_group" : undef),
###LogSD (($comma) ? "The separator character is: $comma" : undef),
###LogSD ((length( $leading_zeros )) ? ".. w/leading zeros: $leading_zeros" : undef),
###LogSD ((length( $trailinq_zeros )) ? ".. w/trailing zeros: $trailinq_zeros" : undef),
###LogSD (($divisor) ? "..with identified divisor: $divisor" : undef),
###LogSD 'Initial code hash:', $code_hash_ref] );
if( !$number_type ){
$number_type = 'INTEGER';
$code_hash_ref->{integer}->{leading_zeros} = length( $leading_zeros ) if $leading_zeros and length( $leading_zeros );
$code_hash_ref->{integer}->{minimum_length} = length( $comma_less );
if( $comma ){
@{$code_hash_ref->{integer}}{ 'group_length', 'comma' } = ( $comma_group, $comma );
}
if( defined $piece->[1] ){
if( $piece->[1] =~ /(\s+)/ ){
$code_hash_ref->{separator} = $1;
}elsif( $piece->[1] eq '/' ){
$number_type = 'FRACTION';
$code_hash_ref->{numerator}->{leading_zeros} = length( $leading_zeros ) if length( $leading_zeros );
delete $code_hash_ref->{integer};
}
}
}elsif( ($number_type eq 'INTEGER') or $number_type eq 'DECIMAL' ){
if( $piece->[1] and $piece->[1] eq '/'){
$number_type = 'FRACTION';
}else{
$number_type = 'DECIMAL';
$code_hash_ref->{decimal}->{trailing_zeros} = length( $trailinq_zeros ) if length( $trailinq_zeros );
$code_hash_ref->{decimal}->{max_length} = length( $comma_less );
}
}elsif( ($number_type eq 'SCIENTIFIC') or $number_type eq 'FRACTION' ){
$code_hash_ref->{exponent}->{leading_zeros} = length( $leading_zeros ) if $leading_zeros and length( $leading_zeros );
$code_hash_ref->{fraction}->{target_length} = length( $comma_less );
if( $divisor ){
$code_hash_ref->{fraction}->{divisor} = $divisor;
}
}
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Current number type: $number_type", 'updated settings:', $code_hash_ref] );
}elsif( $piece->[0] =~ /^((\.)|([Ee][+\-])|(%))$/ ){
if( $2 ){
$number_type = 'DECIMAL';
$code_hash_ref->{separator} = $1;
}elsif( $3 ){
$number_type = 'SCIENTIFIC';
$code_hash_ref->{separator} = $2;
}else{
$number_type = 'PERCENT';
}
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Number type now: $number_type" ] );
}else{
confess "badly formed number format passed: $piece->[0]";
}
}
}
# Set negative type
if( $type_filter->name eq 'NegativeNum' ){
$code_hash_ref->{negative_type} = 1;
}
my $method = '_build_' . lc( $number_type ) . '_sub';
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Resolved the number type to: $number_type",
###LogSD 'Working with settings:', $code_hash_ref ] );
my $conversion_sub = $self->$method( $type_filter, $list_ref, $code_hash_ref );
return( $type_filter, $conversion_sub );
}
sub _build_integer_sub{
my( $self, $type_filter, $list_ref, $conversion_defs ) = @_;
###LogSD my $phone = Log::Shiras::Telephone->new( name_space =>
###LogSD $self->get_log_space . '::_build_number::_build_integer_sub', );
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Building an anonymous sub to return integer values",
###LogSD 'With type constraint: ' . $type_filter->name,
###LogSD '..using list ref:' , $list_ref, '..and conversion defs:', $conversion_defs ] );
my $sprintf_string;
# Process once to determine what to do
my $found_integer = 0;
for my $piece ( @$list_ref ){
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "processing number piece:", $piece ] );
if( !$found_integer and defined $piece->[0] ){
$sprintf_string .= '%s';
$found_integer = 1;
}
if( $piece->[1] ){
$sprintf_string .= $piece->[1];
}
}
$conversion_defs->{no_decimal} = 1;
$conversion_defs->{sprintf_string} = $sprintf_string;
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Final sprintf string: $sprintf_string" ] );
my $dispatch_sequence = $number_build_dispatch->{decimal};
my $conversion_sub = sub{
my $adjusted_input = $_[0];
if( !defined $adjusted_input or $adjusted_input eq '' ){
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Return undef for empty strings" ] );
return undef;
}
my $value_definitions = clone( $conversion_defs );
$value_definitions->{initial_value} = $adjusted_input;
###LogSD $phone->talk( level => 'trace', message => [
###LogSD 'Building scientific output with:', $conversion_defs,
###LogSD '..and dispatch sequence:', $dispatch_sequence ] );
my $built_ref = $self->_build_elements( $dispatch_sequence, $value_definitions );
###LogSD $phone->talk( level => 'trace', message => [
###LogSD "Received built ref:", $built_ref ] );
my $return .= sprintf(
$built_ref->{sprintf_string},
$built_ref->{integer}->{value}
);
$return = $built_ref->{sign} . $return if $built_ref->{sign} and $return;
return $return;
};
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Conversion sub for filter name: " . $type_filter->name, $conversion_sub ] );
return $conversion_sub;
}
sub _build_decimal_sub{
my( $self, $type_filter, $list_ref, $conversion_defs ) = @_;
###LogSD my $phone = Log::Shiras::Telephone->new( name_space =>
###LogSD $self->get_log_space . '::_build_number::_build_decimal_sub', );
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Building an anonymous sub to return decimal values",
###LogSD 'With type constraint: ' . $type_filter->name,
###LogSD '..using list ref:' , $list_ref, '..and code hash ref:', $conversion_defs ] );
my $sprintf_string;
# Process once to determine what to do
for my $piece ( @$list_ref ){
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "processing number piece:", $piece ] );
if( defined $piece->[0] ){
if( $piece->[0] eq '.' ){
$sprintf_string .= '.';
}else{
$sprintf_string .= '%s';
}
}
if( $piece->[1] ){
$sprintf_string .= $piece->[1];
}
}
$conversion_defs->{sprintf_string} = $sprintf_string;
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Final sprintf string: $sprintf_string" ] );
my $dispatch_sequence = $number_build_dispatch->{decimal};
my $conversion_sub = sub{
my $adjusted_input = $_[0];
if( !defined $adjusted_input or $adjusted_input eq '' ){
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Return undef for empty strings" ] );
return undef;
}
my $value_definitions = clone( $conversion_defs );
$value_definitions->{initial_value} = $adjusted_input;
###LogSD $phone->talk( level => 'trace', message => [
###LogSD 'Building scientific output with:', $conversion_defs,
###LogSD '..and dispatch sequence:', $dispatch_sequence ] );
my $built_ref = $self->_build_elements( $dispatch_sequence, $value_definitions );
###LogSD $phone->talk( level => 'trace', message => [
###LogSD "Received built ref:", $built_ref ] );
my $return .= sprintf(
$built_ref->{sprintf_string},
$built_ref->{integer}->{value},
$built_ref->{decimal}->{value},
);
$return = $built_ref->{sign} . $return if $built_ref->{sign} and $return;
return $return;
};
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Conversion sub for filter name: " . $type_filter->name, $conversion_sub ] );
return $conversion_sub;
}
sub _build_percent_sub{
my( $self, $type_filter, $list_ref, $conversion_defs ) = @_;
###LogSD my $phone = Log::Shiras::Telephone->new( name_space =>
###LogSD $self->get_log_space . '::_build_number::_build_percent_sub', );
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Building an anonymous sub to return decimal values",
###LogSD 'With type constraint: ' . $type_filter->name,
###LogSD '..using list ref:' , $list_ref, '..and code hash ref:', $conversion_defs ] );
my $sprintf_string;
my $decimal_count = 0;
# Process once to determine what to do
for my $piece ( @$list_ref ){
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "processing number piece:", $piece ] );
if( defined $piece->[0] ){
if( $piece->[0] eq '%' ){
$sprintf_string .= '%%';
}elsif( $piece->[0] eq '.' ){
$sprintf_string .= '.';
}else{
$sprintf_string .= '%s';
$decimal_count++;
}
}
if( $piece->[1] ){
$sprintf_string .= $piece->[1];
}
}
$conversion_defs->{no_decimal} = 1 if $decimal_count < 2;
$conversion_defs->{sprintf_string} = $sprintf_string;
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Final sprintf string: $sprintf_string" ] );
my $dispatch_sequence = $number_build_dispatch->{percent};
my $conversion_sub = sub{
my $adjusted_input = $_[0];
if( !defined $adjusted_input or $adjusted_input eq '' ){
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Return undef for empty strings" ] );
return undef;
}
my $value_definitions = clone( $conversion_defs );
$value_definitions->{initial_value} = $adjusted_input;
###LogSD $phone->talk( level => 'trace', message => [
###LogSD 'Building scientific output with:', $conversion_defs,
###LogSD '..and dispatch sequence:', $dispatch_sequence ] );
my $built_ref = $self->_build_elements( $dispatch_sequence, $value_definitions );
###LogSD $phone->talk( level => 'trace', message => [
###LogSD "Received built ref:", $built_ref ] );
my $return;
if( $decimal_count < 2 ){
$return .= sprintf(
$built_ref->{sprintf_string},
$built_ref->{integer}->{value},
);
}else{
$return .= sprintf(
$built_ref->{sprintf_string},
$built_ref->{integer}->{value},
$built_ref->{decimal}->{value},
);
}
$return = $built_ref->{sign} . $return if $built_ref->{sign} and $return;
return $return;
};
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Conversion sub for filter name: " . $type_filter->name, $conversion_sub ] );
return $conversion_sub;
}
sub _build_scientific_sub{
my( $self, $type_filter, $list_ref, $conversion_defs ) = @_;
###LogSD my $phone = Log::Shiras::Telephone->new( name_space =>
###LogSD $self->get_log_space . '::_build_number::_build_scientific_sub', );
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Building an anonymous sub to return decimal values",
###LogSD 'With type constraint: ' . $type_filter->name,
###LogSD '..using list ref:' , $list_ref, '..and code hash ref:', $conversion_defs ] );
# Process once to determine what to do
my ( $sprintf_string, $exponent_sprintf );
$conversion_defs->{no_decimal} = ( exists $conversion_defs->{decimal} ) ? 0 : 1 ;
for my $piece ( @$list_ref ){
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "processing number piece:", $piece ] );
if( defined $piece->[0] ){
if( $piece->[0] =~ /(E)(.)/ ){
$sprintf_string .= $1;
$exponent_sprintf = '%';
$exponent_sprintf .= '+' if $2 eq '+';
if( exists $conversion_defs->{exponent}->{leading_zeros} ){
$exponent_sprintf .= '0.' . $conversion_defs->{exponent}->{leading_zeros};
}
$exponent_sprintf .= 'd';
}elsif( $piece->[0] eq '.' ){
$sprintf_string .= '.';