This file is indexed.

/usr/share/perl5/Spreadsheet/ParseExcel/Utility.pm is in libspreadsheet-parseexcel-perl 0.6500-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
package Spreadsheet::ParseExcel::Utility;

###############################################################################
#
# Spreadsheet::ParseExcel::Utility - Utility functions for ParseExcel.
#
# Used in conjunction with Spreadsheet::ParseExcel.
#
# Copyright (c) 2014      Douglas Wilson
# Copyright (c) 2009-2013 John McNamara
# Copyright (c) 2006-2008 Gabor Szabo
# Copyright (c) 2000-2006 Kawai Takanori
#
# perltidy with standard settings.
#
# Documentation after __END__
#

use strict;
use warnings;

require Exporter;
use vars qw(@ISA @EXPORT_OK);
@ISA       = qw(Exporter);
@EXPORT_OK = qw(ExcelFmt LocaltimeExcel ExcelLocaltime
  col2int int2col sheetRef xls2csv);

our $VERSION = '0.65';

my $qrNUMBER = qr/(^[+-]?\d+(\.\d+)?$)|(^[+-]?\d+\.?(\d*)[eE][+-](\d+))$/;

###############################################################################
#
# ExcelFmt()
#
# This function takes an Excel style number format and converts a number into
# that format. for example: 'hh:mm:ss AM/PM' + 0.01023148 = '12:14:44 AM'.
#
# It does this with a type of templating mechanism. The format string is parsed
# to identify tokens that need to be replaced and their position within the
# string is recorded. These can be thought of as placeholders. The number is
# then converted to the required formats and substituted into the placeholders.
#
# Interested parties should refer to the Excel documentation on cell formats for
# more information: http://office.microsoft.com/en-us/excel/HP051995001033.aspx
# The Microsoft documentation for the Excel Binary File Format, [MS-XLS].pdf,
# also contains a ABNF grammar for number format strings.
#
# Maintainers notes:
# ==================
#
# Note on format subsections:
# A format string can contain 4 possible sub-sections separated by semi-colons:
# Positive numbers, negative numbers, zero values, and text.
# For example: _(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)
#
# Note on conditional formats.
# A number format in Excel can have a conditional expression such as:
#     [>9999999](000)000-0000;000-0000
# This is equivalent to the following in Perl:
#     $format = $number > 9999999 ? '(000)000-0000' : '000-0000';
# Nested conditionals are also possible but we don't support them.
#
# Efficiency: The excessive use of substr() isn't very efficient. However,
# it probably doesn't merit rewriting this function with a parser or regular
# expressions and \G.
#
# TODO: I think the single quote handling may not be required. Check.
#
sub ExcelFmt {

    my ( $format_str, $number, $is_1904, $number_type, $want_subformats ) = @_;

    # Return text strings without further formatting.
    return $number unless $number =~ $qrNUMBER;

    # Handle OpenOffice.org GENERAL format.
    $format_str = '@' if uc($format_str) eq "GENERAL";

    # Check for a conditional at the start of the format. See notes above.
    my $conditional;
    if ( $format_str =~ /^\[([<>=][^\]]+)\](.*)$/ ) {
        $conditional = $1;
        $format_str  = $2;
    }

    # Ignore the underscore token which is used to indicate a padding space.
    $format_str =~ s/_/ /g;

    # Split the format string into 4 possible sub-sections: positive numbers,
    # negative numbers, zero values, and text. See notes above.
    my @formats;
    my $section      = 0;
    my $double_quote = 0;
    my $single_quote = 0;

    # Initial parsing of the format string to remove escape characters. This
    # also handles quoted strings. See note about single quotes above.
  CHARACTER:
    for my $char ( split //, $format_str ) {

        if ( $double_quote or $single_quote ) {
            $formats[$section] .= $char;
            $double_quote = 0 if $char eq '"';
            $single_quote = 0;
            next CHARACTER;
        }

        if ( $char eq ';' ) {
            $section++;
            next CHARACTER;
        }
        elsif ( $char eq '"' ) {
            $double_quote = 1;
        }
        elsif ( $char eq '!' ) {
            $single_quote = 1;
        }
        elsif ( $char eq '\\' ) {
            $single_quote = 1;
        }
        elsif ( $char eq '(' ) {
            next CHARACTER;    # Ignore.
        }
        elsif ( $char eq ')' ) {
            next CHARACTER;    # Ignore.
        }

        # Convert upper case OpenOffice.org date/time formats to lowercase..
        $char = lc($char) if $char =~ /[DMYHS]/;

        $formats[$section] .= $char;
    }

    # Select the appropriate format from the 4 possible sub-sections:
    # positive numbers, negative numbers, zero values, and text.
    # We ignore the Text section since non-numeric values are returned
    # unformatted at the start of the function.
    my $format;
    $section = 0;

    if ( @formats == 1 ) {
        $section = 0;
    }
    elsif ( @formats == 2 ) {
        if ( $number < 0 ) {
            $section = 1;
        }
        else {
            $section = 0;
        }
    }
    elsif ( @formats == 3 ) {
        if ( $number == 0 ) {
            $section = 2;
        }
        elsif ( $number < 0 ) {
            $section = 1;
        }
        else {
            $section = 0;
        }
    }
    else {
        $section = 0;
    }

    # Override the previous choice if the format is conditional.
    if ($conditional) {

        # TODO. Replace string eval with a function.
        $section = eval "$number $conditional" ? 0 : 1;
    }

    # We now have the required format.
    $format = $formats[$section];

    # The format string can contain one of the following colours:
    # [Black] [Blue] [Cyan] [Green] [Magenta] [Red] [White] [Yellow]
    # or the string [ColorX] where x is a colour index from 1 to 56.
    # We don't use the colour but we return it to the caller.
    #
    my $color = '';
    if ( $format =~ s/^(\[[A-Za-z]{3,}(\d{1,2})?\])// ) {
        $color = $1;
    }

    # Remove the locale, such as [$-409], from the format string.
    my $locale = '';
    if ( $format =~ s/^(\[\$?-F?\d+\])// ) {
        $locale = $1;
    }

    # Replace currency locale, such as [$$-409], with $ in the format string.
    # See the RT#60547 test cases in 21_number_format_user.t.
    if ( $format =~ s/(\[\$([^-]+)(-\d+)?\])/$2/s ) {
        $locale = $1;
    }


    # Remove leading # from '# ?/?', '# ??/??' fraction formats.
    $format =~ s{# \?}{?}g;

    # Parse the format string and create an AoA of placeholders that contain
    # the parts of the string to be replaced. The format of the information
    # stored is: [ $token, $start_pos, $end_pos, $option_info ].
    #
    my $format_mode  = '';    # Either: '', 'number', 'date'
    my $pos          = 0;     # Character position within format string.
    my @placeholders = ();    # Arefs with parts of the format to be replaced.
    my $token        = '';    # The actual format extracted from the total str.
    my $start_pos;            # A position variable. Initial parser position.
    my $token_start = -1;     # A position variable.
    my $decimal_pos = -1;     # Position of the punctuation char "." or ",".
    my $comma_count = 0;      # Count of the commas in the format.
    my $is_fraction = 0;      # Number format is a fraction.
    my $is_currency = 0;      # Number format is a currency.
    my $is_percent  = 0;      # Number format is a percentage.
    my $is_12_hour  = 0;      # Time format is using 12 hour clock.
    my $seen_dot    = 0;      # Treat only the first "." as the decimal point.

    # Parse the format.
  PARSER:
    while ( $pos < length $format ) {
        $start_pos = $pos;
        my $char = substr( $format, $pos, 1 );

        # Ignore control format characters such as '#0+-.?eE,%'. However,
        # only ignore '.' if it is the first one encountered. RT 45502.
        if ( ( !$seen_dot && $char !~ /[#0\+\-\.\?eE\,\%]/ )
            || $char !~ /[#0\+\-\?eE\,\%]/ )
        {

            if ( $token_start != -1 ) {
                push @placeholders,
                  [
                    substr( $format, $token_start, $pos - $token_start ),
                    $decimal_pos, $pos - $token_start
                  ];
                $token_start = -1;
            }
        }

        # Processing for quoted strings within the format. See notes above.
        if ( $char eq '"' ) {
            $double_quote = $double_quote ? 0 : 1;
            $pos++;
            next PARSER;
        }
        elsif ( $char eq '!' ) {
            $single_quote = 1;
            $pos++;
            next PARSER;
        }
        elsif ( $char eq '\\' ) {
            if ( $single_quote != 1 ) {
                $single_quote = 1;
                $pos++;
                next PARSER;
            }
        }

        if (   ( defined($double_quote) and ($double_quote) )
            or ( defined($single_quote) and ($single_quote) )
            or ( $seen_dot && $char eq '.' ) )
        {
            $single_quote = 0;
            if (
                ( $format_mode ne 'date' )
                and (  ( substr( $format, $pos, 2 ) eq "\x81\xA2" )
                    || ( substr( $format, $pos, 2 ) eq "\x81\xA3" )
                    || ( substr( $format, $pos, 2 ) eq "\xA2\xA4" )
                    || ( substr( $format, $pos, 2 ) eq "\xA2\xA5" ) )
              )
            {

                # The above matches are currency symbols.
                push @placeholders,
                  [ substr( $format, $pos, 2 ), length($token), 2 ];
                $is_currency = 1;
                $pos += 2;
            }
            else {
                $pos++;
            }
        }
        elsif (
            ( $char =~ /[#0\+\.\?eE\,\%]/ )
            || (    ( $format_mode ne 'date' )
                and ( ( $char eq '-' ) || ( $char eq '(' ) || ( $char eq ')' ) )
            )
          )
        {
            $format_mode = 'number' unless $format_mode;
            if ( substr( $format, $pos, 1 ) =~ /[#0]/ ) {
                if (
                    substr( $format, $pos ) =~
                    /^([#0]+[\.]?[0#]*[eE][\+\-][0#]+)/ )
                {
                    push @placeholders, [ $1, $pos, length($1) ];
                    $pos += length($1);
                }
                else {
                    if ( $token_start == -1 ) {
                        $token_start = $pos;
                        $decimal_pos = length($token);
                    }
                }
            }
            elsif ( substr( $format, $pos, 1 ) eq '?' ) {

                # Look for a fraction format like ?/? or ??/??
                if ( $token_start != -1 ) {
                    push @placeholders,
                      [
                        substr(
                            $format, $token_start, $pos - $token_start + 1
                        ),
                        $decimal_pos,
                        $pos - $token_start + 1
                      ];
                }
                $token_start = $pos;

                # Find the end of the fraction format.
              FRACTION:
                while ( $pos < length($format) ) {
                    if ( substr( $format, $pos, 1 ) eq '/' ) {
                        $is_fraction = 1;
                    }
                    elsif ( substr( $format, $pos, 1 ) eq '?' ) {
                        $pos++;
                        next FRACTION;
                    }
                    else {
                        if ( $is_fraction
                            && ( substr( $format, $pos, 1 ) =~ /[0-9]/ ) )
                        {

                            # TODO: Could invert if() logic and remove this.
                            $pos++;
                            next FRACTION;
                        }
                        else {
                            last FRACTION;
                        }
                    }
                    $pos++;
                }
                $pos--;

                push @placeholders,
                  [
                    substr( $format, $token_start, $pos - $token_start + 1 ),
                    length($token), $pos - $token_start + 1
                  ];
                $token_start = -1;
            }
            elsif ( substr( $format, $pos, 3 ) =~ /^[eE][\+\-][0#]$/ ) {
                if ( substr( $format, $pos ) =~ /([eE][\+\-][0#]+)/ ) {
                    push @placeholders, [ $1, $pos, length($1) ];
                    $pos += length($1);
                }
                $token_start = -1;
            }
            else {
                if ( $token_start != -1 ) {
                    push @placeholders,
                      [
                        substr( $format, $token_start, $pos - $token_start ),
                        $decimal_pos, $pos - $token_start
                      ];
                    $token_start = -1;
                }
                if ( substr( $format, $pos, 1 ) =~ /[\+\-]/ ) {
                    push @placeholders,
                      [ substr( $format, $pos, 1 ), length($token), 1 ];
                    $is_currency = 1;
                }
                elsif ( substr( $format, $pos, 1 ) eq '.' ) {
                    push @placeholders,
                      [ substr( $format, $pos, 1 ), length($token), 1 ];
                    $seen_dot = 1;
                }
                elsif ( substr( $format, $pos, 1 ) eq ',' ) {
                    $comma_count++;
                    push @placeholders,
                      [ substr( $format, $pos, 1 ), length($token), 1 ];
                }
                elsif ( substr( $format, $pos, 1 ) eq '%' ) {
                    $is_percent = 1;
                }
                elsif (( substr( $format, $pos, 1 ) eq '(' )
                    || ( substr( $format, $pos, 1 ) eq ')' ) )
                {
                    push @placeholders,
                      [ substr( $format, $pos, 1 ), length($token), 1 ];
                    $is_currency = 1;
                }
            }
            $pos++;
        }
        elsif ( $char =~ /[ymdhsapg]/i ) {
            $format_mode = 'date' unless $format_mode;
            if ( substr( $format, $pos, 5 ) =~ /am\/pm/i ) {
                push @placeholders, [ 'am/pm', length($token), 5 ];
                $is_12_hour = 1;
                $pos += 5;
            }
            elsif ( substr( $format, $pos, 3 ) =~ /a\/p/i ) {
                push @placeholders, [ 'a/p', length($token), 3 ];
                $is_12_hour = 1;
                $pos += 3;
            }
            elsif ( substr( $format, $pos, 5 ) eq 'mmmmm' ) {
                push @placeholders, [ 'mmmmm', length($token), 5 ];
                $pos += 5;
            }
            elsif (( substr( $format, $pos, 4 ) eq 'mmmm' )
                || ( substr( $format, $pos, 4 ) eq 'dddd' )
                || ( substr( $format, $pos, 4 ) eq 'yyyy' )
                || ( substr( $format, $pos, 4 ) eq 'ggge' ) )
            {
                push @placeholders,
                  [ substr( $format, $pos, 4 ), length($token), 4 ];
                $pos += 4;
            }
            elsif (( substr( $format, $pos, 3 ) eq 'ddd' )
                || ( substr( $format, $pos, 3 ) eq 'mmm' )
                || ( substr( $format, $pos, 3 ) eq 'yyy' ) )
            {
                push @placeholders,
                  [ substr( $format, $pos, 3 ), length($token), 3 ];
                $pos += 3;
            }
            elsif (( substr( $format, $pos, 2 ) eq 'yy' )
                || ( substr( $format, $pos, 2 ) eq 'mm' )
                || ( substr( $format, $pos, 2 ) eq 'dd' )
                || ( substr( $format, $pos, 2 ) eq 'hh' )
                || ( substr( $format, $pos, 2 ) eq 'ss' )
                || ( substr( $format, $pos, 2 ) eq 'ge' ) )
            {
                if (
                       ( substr( $format, $pos, 2 ) eq 'mm' )
                    && (@placeholders)
                    && (   ( $placeholders[-1]->[0] eq 'h' )
                        or ( $placeholders[-1]->[0] eq 'hh' ) )
                  )
                {

                    # For this case 'm' is minutes not months.
                    push @placeholders, [ 'mm', length($token), 2, 'minutes' ];
                }
                else {
                    push @placeholders,
                      [ substr( $format, $pos, 2 ), length($token), 2 ];
                }
                if (   ( substr( $format, $pos, 2 ) eq 'ss' )
                    && ( @placeholders > 1 ) )
                {
                    if (   ( $placeholders[-2]->[0] eq 'm' )
                        || ( $placeholders[-2]->[0] eq 'mm' ) )
                    {

                        # For this case 'm' is minutes not months.
                        push( @{ $placeholders[-2] }, 'minutes' );
                    }
                }
                $pos += 2;
            }
            elsif (( substr( $format, $pos, 1 ) eq 'm' )
                || ( substr( $format, $pos, 1 ) eq 'd' )
                || ( substr( $format, $pos, 1 ) eq 'h' )
                || ( substr( $format, $pos, 1 ) eq 's' ) )
            {
                if (
                       ( substr( $format, $pos, 1 ) eq 'm' )
                    && (@placeholders)
                    && (   ( $placeholders[-1]->[0] eq 'h' )
                        or ( $placeholders[-1]->[0] eq 'hh' ) )
                  )
                {

                    # For this case 'm' is minutes not months.
                    push @placeholders, [ 'm', length($token), 1, 'minutes' ];
                }
                else {
                    push @placeholders,
                      [ substr( $format, $pos, 1 ), length($token), 1 ];
                }
                if (   ( substr( $format, $pos, 1 ) eq 's' )
                    && ( @placeholders > 1 ) )
                {
                    if (   ( $placeholders[-2]->[0] eq 'm' )
                        || ( $placeholders[-2]->[0] eq 'mm' ) )
                    {

                        # For this case 'm' is minutes not months.
                        push( @{ $placeholders[-2] }, 'minutes' );
                    }
                }
                $pos += 1;
            }
        }
        elsif ( ( substr( $format, $pos, 3 ) eq '[h]' ) ) {
            $format_mode = 'date' unless $format_mode;
            push @placeholders, [ '[h]', length($token), 3 ];
            $pos += 3;
        }
        elsif ( ( substr( $format, $pos, 4 ) eq '[mm]' ) ) {
            $format_mode = 'date' unless $format_mode;
            push @placeholders, [ '[mm]', length($token), 4 ];
            $pos += 4;
        }
        elsif ( $char eq '@' ) {
            push @placeholders, [ '@', length($token), 1 ];
            $pos++;
        }
        elsif ( $char eq '*' ) {
            push @placeholders,
              [ substr( $format, $pos, 1 ), length($token), 1 ];
        }
        else {
            $pos++;
        }
        $pos++ if ( $pos == $start_pos );    #No Format match
        $token .= substr( $format, $start_pos, $pos - $start_pos );

    }    # End of parsing.

    # Copy the located format string to a result string that we will perform
    # the substitutions on and return to the user.
    my $result = $token;

    # Add a placeholder between the decimal/comma and end of the token, if any.
    if ( $token_start != -1 ) {
        push @placeholders,
          [
            substr( $format, $token_start, $pos - $token_start + 1 ),
            $decimal_pos, $pos - $token_start + 1
          ];
    }

    #
    # In the next sections we process date, number and text formats. We take a
    # format such as yyyy/mm/dd and replace it with something like 2008/12/25.
    #
    if ( ( $format_mode eq 'date' ) && ( $number =~ $qrNUMBER ) ) {

        # The maximum allowable date in Excel is 9999-12-31T23:59:59.000 which
        # equates to 2958465.999+ in the 1900 epoch and 2957003.999+ in the
        # 1904 epoch. We use 0 as the minimum in both epochs. The 1904 system
        # actually supports negative numbers but that isn't worth the effort.
        my $min_date = 0;
        my $max_date = 2958466;
        $max_date = 2957004 if $is_1904;

        if ( $number < $min_date || $number >= $max_date ) {
            return $number;    # Return unformatted number.
        }

        # Process date formats.
        my @time = ExcelLocaltime( $number, $is_1904 );

        #    0     1     2      3     4       5      6      7
        my ( $sec, $min, $hour, $day, $month, $year, $wday, $msec ) = @time;

        $month++;              # localtime() zero indexed month.
        $year += 1900;         # localtime() year.

        my @full_month_name = qw(
          None January February March April May June July
          August September October November December
        );
        my @short_month_name = qw(
          None Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
        );
        my @full_day_name = qw(
          Sunday Monday Tuesday Wednesday Thursday Friday Saturday
        );
        my @short_day_name = qw(
          Sun Mon Tue Wed Thu Fri Sat
        );

        # Replace the placeholders in the template such as yyyy mm dd with
        # actual numbers or strings.
        my $replacement;
        for my $placeholder ( reverse @placeholders ) {

            if ( $placeholder->[-1] eq 'minutes' ) {

                # For this case 'm/mm' is minutes not months.
                if ( $placeholder->[0] eq 'mm' ) {
                    $replacement = sprintf( "%02d", $min );
                }
                else {
                    $replacement = sprintf( "%d", $min );
                }
            }
            elsif ( $placeholder->[0] eq 'yyyy' ) {

                # 4 digit Year. 2000 -> 2000.
                $replacement = sprintf( '%04d', $year );
            }
            elsif ( $placeholder->[0] eq 'yy' ) {

                # 2 digit Year. 2000 -> 00.
                $replacement = sprintf( '%02d', $year % 100 );
            }
            elsif ( $placeholder->[0] eq 'mmmmm' ) {

                # First character of the month name. 1 -> J.
                $replacement = substr( $short_month_name[$month], 0, 1 );
            }
            elsif ( $placeholder->[0] eq 'mmmm' ) {

                # Full month name. 1 -> January.
                $replacement = $full_month_name[$month];
            }
            elsif ( $placeholder->[0] eq 'mmm' ) {

                # Short month name. 1 -> Jan.
                $replacement = $short_month_name[$month];
            }
            elsif ( $placeholder->[0] eq 'mm' ) {

                # 2 digit month. 1 -> 01.
                $replacement = sprintf( '%02d', $month );
            }
            elsif ( $placeholder->[0] eq 'm' ) {

                # 1 digit month. 1 -> 1.
                $replacement = sprintf( '%d', $month );
            }
            elsif ( $placeholder->[0] eq 'dddd' ) {

                # Full day name. Wednesday (for example.)
                $replacement = $full_day_name[$wday];
            }
            elsif ( $placeholder->[0] eq 'ddd' ) {

                # Short day name. Wed (for example.)
                $replacement = $short_day_name[$wday];
            }
            elsif ( $placeholder->[0] eq 'dd' ) {

                # 2 digit day. 1 -> 01.
                $replacement = sprintf( '%02d', $day );
            }
            elsif ( $placeholder->[0] eq 'd' ) {

                # 1 digit day. 1 -> 1.
                $replacement = sprintf( '%d', $day );
            }
            elsif ( $placeholder->[0] eq 'hh' ) {

                # 2 digit hour.
                if ($is_12_hour) {
                    my $hour_tmp = $hour % 12;
                    $hour_tmp = 12 if $hour % 12 == 0;
                    $replacement = sprintf( '%d', $hour_tmp );
                }
                else {
                    $replacement = sprintf( '%02d', $hour );
                }
            }
            elsif ( $placeholder->[0] eq 'h' ) {

                # 1 digit hour.
                if ($is_12_hour) {
                    my $hour_tmp = $hour % 12;
                    $hour_tmp = 12 if $hour % 12 == 0;
                    $replacement = sprintf( '%2d', $hour_tmp );
                }
                else {
                    $replacement = sprintf( '%d', $hour );
                }
            }
            elsif ( $placeholder->[0] eq 'ss' ) {

                # 2 digit seconds.
                $replacement = sprintf( '%02d', $sec );
            }
            elsif ( $placeholder->[0] eq 's' ) {

                # 1 digit seconds.
                $replacement = sprintf( '%d', $sec );
            }
            elsif ( $placeholder->[0] eq 'am/pm' ) {

                # AM/PM.
                $replacement = ( $hour >= 12 ) ? 'PM' : 'AM';
            }
            elsif ( $placeholder->[0] eq 'a/p' ) {

                # AM/PM.
                $replacement = ( $hour >= 12 ) ? 'P' : 'A';
            }
            elsif ( $placeholder->[0] eq '.' ) {

                # Decimal point for seconds.
                $replacement = '.';
            }
            elsif ( $placeholder->[0] =~ /(^0+$)/ ) {

                # Milliseconds. For example h:ss.000.
                my $length = length($1);
                $replacement =
                  substr( sprintf( "%.${length}f", $msec / 1000 ), 2, $length );
            }
            elsif ( $placeholder->[0] eq '[h]' ) {

                # Hours modulus 24. 25 displays as 25 not as 1.
                $replacement = sprintf( '%d', int($number) * 24 + $hour );
            }
            elsif ( $placeholder->[0] eq '[mm]' ) {

                # Mins modulus 60. 72 displays as 72 not as 12.
                $replacement =
                  sprintf( '%d', ( int($number) * 24 + $hour ) * 60 + $min );
            }
            elsif ( $placeholder->[0] eq 'ge' ) {
                require Spreadsheet::ParseExcel::FmtJapan;
                # Japanese Nengo (aka Gengo) in initialism (abbr. name)
                 $replacement =
                  Spreadsheet::ParseExcel::FmtJapan::CnvNengo( abbr_name => @time );
            }
            elsif ( $placeholder->[0] eq 'ggge' ) {
                require Spreadsheet::ParseExcel::FmtJapan;
                # Japanese Nengo (aka Gengo) in Kanji (full name)
                 $replacement =
                  Spreadsheet::ParseExcel::FmtJapan::CnvNengo( name => @time );
            }
            elsif ( $placeholder->[0] eq '@' ) {

                # Text format.
                $replacement = $number;
            }
            elsif ( $placeholder->[0] eq ',' ) {
                next;
            }

            # Substitute the replacement string back into the template.
            substr( $result, $placeholder->[1], $placeholder->[2],
                $replacement );
        }
    }
    elsif ( ( $format_mode eq 'number' ) && ( $number =~ $qrNUMBER ) ) {

        # Process non date formats.
        if (@placeholders) {
            while ( $placeholders[-1]->[0] eq ',' ) {
                $comma_count--;
                substr(
                    $result,
                    $placeholders[-1]->[1],
                    $placeholders[-1]->[2], ''
                );
                $number /= 1000;
                pop @placeholders;
            }

            my $number_format = join( '', map { $_->[0] } @placeholders );
            my $number_result;
            my $str_length    = 0;
            my $engineering   = 0;
            my $is_decimal    = 0;
            my $is_integer    = 0;
            my $after_decimal = undef;

            for my $token ( split //, $number_format ) {
                if ( $token eq '.' ) {
                    $str_length++;
                    $is_decimal = 1;
                }
                elsif ( ( $token eq 'E' ) || ( $token eq 'e' ) ) {
                    $engineering = 1;
                }
                elsif ( $token eq '0' ) {
                    $str_length++;
                    $after_decimal++ if $is_decimal;
                    $is_integer = 1;
                }
                elsif ( $token eq '#' ) {
                    $after_decimal++ if $is_decimal;
                    $is_integer = 1;
                }
                elsif ( $token eq '?' ) {
                    $after_decimal++ if $is_decimal;
                }
            }

            $number *= 100.0 if $is_percent;

            my $data = ($is_currency) ? abs($number) : $number + 0;

            if ($is_fraction) {
                $number_result = sprintf( "%0${str_length}d", int($data) );
            }
            else {
                if ($is_decimal) {

                    if ( defined $after_decimal ) {
                        $number_result =
                          sprintf "%0${str_length}.${after_decimal}f", $data;
                    }
                    else {
                        $number_result = sprintf "%0${str_length}f", $data;
                    }

                    # Fix for Perl and sprintf not rounding up like Excel.
                    # http://rt.cpan.org/Public/Bug/Display.html?id=45626
                    if ( $data =~ /^${number_result}5/ ) {
                        $number_result =
                          sprintf "%0${str_length}.${after_decimal}f",
                          $data . '1';
                    }
                }
                else {
                    $number_result = sprintf( "%0${str_length}.0f", $data );
                }
            }

            $number_result = AddComma($number_result) if $comma_count > 0;

            my $number_length = length($number_result);
            my $decimal_pos   = -1;
            my $replacement;

            for ( my $i = @placeholders - 1 ; $i >= 0 ; $i-- ) {
                my $placeholder = $placeholders[$i];

                if ( $placeholder->[0] =~
                    /([#0]*)([\.]?)([0#]*)([eE])([\+\-])([0#]+)/ )
                {
                    substr( $result, $placeholder->[1], $placeholder->[2],
                        MakeE( $placeholder->[0], $number ) );
                }
                elsif ( $placeholder->[0] =~ /\// ) {
                    substr( $result, $placeholder->[1], $placeholder->[2],
                        MakeFraction( $placeholder->[0], $number, $is_integer )
                    );
                }
                elsif ( $placeholder->[0] eq '.' ) {
                    $number_length--;
                    $decimal_pos = $number_length;
                }
                elsif ( $placeholder->[0] eq '+' ) {
                    substr( $result, $placeholder->[1], $placeholder->[2],
                        ( $number > 0 )
                        ? '+'
                        : ( ( $number == 0 ) ? '+' : '-' ) );
                }
                elsif ( $placeholder->[0] eq '-' ) {
                    substr( $result, $placeholder->[1], $placeholder->[2],
                        ( $number > 0 )
                        ? ''
                        : ( ( $number == 0 ) ? '' : '-' ) );
                }
                elsif ( $placeholder->[0] eq '@' ) {
                    substr( $result, $placeholder->[1], $placeholder->[2],
                        $number );
                }
                elsif ( $placeholder->[0] eq '*' ) {
                    substr( $result, $placeholder->[1], $placeholder->[2], '' );
                }
                elsif (( $placeholder->[0] eq "\xA2\xA4" )
                    or ( $placeholder->[0] eq "\xA2\xA5" )
                    or ( $placeholder->[0] eq "\x81\xA2" )
                    or ( $placeholder->[0] eq "\x81\xA3" ) )
                {
                    substr(
                        $result,           $placeholder->[1],
                        $placeholder->[2], $placeholder->[0]
                    );
                }
                elsif (( $placeholder->[0] eq '(' )
                    or ( $placeholder->[0] eq ')' ) )
                {
                    substr(
                        $result,           $placeholder->[1],
                        $placeholder->[2], $placeholder->[0]
                    );
                }
                else {
                    if ( $number_length > 0 ) {
                        if ( $i <= 0 ) {
                            $replacement =
                              substr( $number_result, 0, $number_length );
                            $number_length = 0;
                        }
                        else {
                            my $real_part_length = length( $placeholder->[0] );
                            if ( $decimal_pos >= 0 ) {
                                my $format = $placeholder->[0];
                                $format =~ s/^#+//;
                                $real_part_length = length $format;
                                $real_part_length =
                                  ( $number_length <= $real_part_length )
                                  ? $number_length
                                  : $real_part_length;
                            }
                            else {
                                $real_part_length =
                                  ( $number_length <= $real_part_length )
                                  ? $number_length
                                  : $real_part_length;
                            }
                            $replacement =
                              substr( $number_result,
                                $number_length - $real_part_length,
                                $real_part_length );
                            $number_length -= $real_part_length;
                        }
                    }
                    else {
                        $replacement = '';
                    }
                    substr( $result, $placeholder->[1], $placeholder->[2],
                        "\x00" . $replacement );
                }
            }
            $replacement =
              ( $number_length > 0 )
              ? substr( $number_result, 0, $number_length )
              : '';
            $result =~ s/\x00/$replacement/;
            $result =~ s/\x00//g;
        }
    }
    else {

        # Process text formats
        my $is_text = 0;
        for ( my $i = @placeholders - 1 ; $i >= 0 ; $i-- ) {
            my $placeholder = $placeholders[$i];
            if ( $placeholder->[0] eq '@' ) {
                substr( $result, $placeholder->[1], $placeholder->[2],
                    $number );
                $is_text++;
            }
            else {
                substr( $result, $placeholder->[1], $placeholder->[2], '' );
            }
        }

        $result = $number unless $is_text;

    }    # End of placeholder substitutions.

    # Trim the leading and trailing whitespace from the results.
    $result =~ s/^\s+//;
    $result =~ s/\s+$//;

    # Fix for negative currency.
    $result =~ s/^\$\-/\-\$/;
    $result =~ s/^\$ \-/\-\$ /;

    # Return color and locale strings if required.
    if ($want_subformats) {
        return ( $result, $color, $locale );
    }
    else {
        return $result;
    }
}

#------------------------------------------------------------------------------
# AddComma (for Spreadsheet::ParseExcel::Utility)
#------------------------------------------------------------------------------
sub AddComma {
    my ($sNum) = @_;

    if ( $sNum =~ /^([^\d]*)(\d\d\d\d+)(\.*.*)$/ ) {
        my ( $sPre, $sObj, $sAft ) = ( $1, $2, $3 );
        for ( my $i = length($sObj) - 3 ; $i > 0 ; $i -= 3 ) {
            substr( $sObj, $i, 0, ',' );
        }
        return $sPre . $sObj . $sAft;
    }
    else {
        return $sNum;
    }
}

#------------------------------------------------------------------------------
# MakeFraction (for Spreadsheet::ParseExcel::Utility)
#------------------------------------------------------------------------------
sub MakeFraction {
    my ( $sFmt, $iData, $iFlg ) = @_;
    my $iBunbo;
    my $iShou;

    #1. Init
    # print "FLG: $iFlg\n";
    if ($iFlg) {
        $iShou = $iData - int($iData);
        return '' if ( $iShou == 0 );
    }
    else {
        $iShou = $iData;
    }
    $iShou = abs($iShou);
    my $sSWk;

    #2.Calc BUNBO
    #2.1 BUNBO defined
    if ( $sFmt =~ /\/(\d+)$/ ) {
        $iBunbo = $1;
        return sprintf( "%d/%d", $iShou * $iBunbo, $iBunbo );
    }
    else {

        #2.2 Calc BUNBO
        $sFmt =~ /\/(\?+)$/;
        my $iKeta = length($1);
        my $iSWk  = 1;
        my $sSWk  = '';
        my $iBunsi;
        for ( my $iBunbo = 2 ; $iBunbo < 10**$iKeta ; $iBunbo++ ) {
            $iBunsi = int( $iShou * $iBunbo + 0.5 );
            my $iCmp = abs( $iShou - ( $iBunsi / $iBunbo ) );
            if ( $iCmp < $iSWk ) {
                $iSWk = $iCmp;
                $sSWk = sprintf( "%d/%d", $iBunsi, $iBunbo );
                last if ( $iSWk == 0 );
            }
        }
        return $sSWk;
    }
}

#------------------------------------------------------------------------------
# MakeE (for Spreadsheet::ParseExcel::Utility)
#------------------------------------------------------------------------------
sub MakeE {
    my ( $sFmt, $iData ) = @_;

    $sFmt =~ /(([#0]*)[\.]?[#0]*)([eE])([\+\-][0#]+)/;
    my ( $sKari, $iKeta, $sE, $sSisu ) = ( $1, length($2), $3, $4 );
    $iKeta = 1 if ( $iKeta <= 0 );

    my $iLog10 = 0;
    $iLog10 = ( $iData == 0 ) ? 0 : ( log( abs($iData) ) / log(10) );
    $iLog10 = (
        int( $iLog10 / $iKeta ) +
          ( ( ( $iLog10 - int( $iLog10 / $iKeta ) ) < 0 ) ? -1 : 0 ) ) * $iKeta;

    my $sUe = ExcelFmt( $sKari, $iData * ( 10**( $iLog10 * -1 ) ), 0 );
    my $sShita = ExcelFmt( $sSisu, $iLog10, 0 );
    return $sUe . $sE . $sShita;
}

#------------------------------------------------------------------------------
# LeapYear (for Spreadsheet::ParseExcel::Utility)
#------------------------------------------------------------------------------
sub LeapYear {
    my ($iYear) = @_;
    return 1 if ( $iYear == 1900 );    #Special for Excel
    return ( ( ( $iYear % 4 ) == 0 )
          && ( ( $iYear % 100 ) || ( $iYear % 400 ) == 0 ) )
      ? 1
      : 0;
}

#------------------------------------------------------------------------------
# LocaltimeExcel (for Spreadsheet::ParseExcel::Utility)
#------------------------------------------------------------------------------
sub LocaltimeExcel {
    my ( $iSec, $iMin, $iHour, $iDay, $iMon, $iYear, $iwDay, $iMSec, $flg1904 )
      = @_;

    #0. Init
    $iMon++;
    $iYear += 1900;

    #1. Calc Time
    my $iTime;
    $iTime = $iHour;
    $iTime *= 60;
    $iTime += $iMin;
    $iTime *= 60;
    $iTime += $iSec;
    $iTime += $iMSec / 1000.0 if ( defined($iMSec) );
    $iTime /= 86400.0;    #3600*24(1day in seconds)
    my $iY;
    my $iYDays;

    #2. Calc Days
    if ($flg1904) {
        $iY = 1904;
        $iTime--;         #Start from Jan 1st
        $iYDays = 366;
    }
    else {
        $iY     = 1900;
        $iYDays = 366;    #In Excel 1900 is leap year (That's not TRUE!)
    }
    while ( $iY < $iYear ) {
        $iTime += $iYDays;
        $iY++;
        $iYDays = ( LeapYear($iY) ) ? 366 : 365;
    }
    for ( my $iM = 1 ; $iM < $iMon ; $iM++ ) {
        if (   $iM == 1
            || $iM == 3
            || $iM == 5
            || $iM == 7
            || $iM == 8
            || $iM == 10
            || $iM == 12 )
        {
            $iTime += 31;
        }
        elsif ( $iM == 4 || $iM == 6 || $iM == 9 || $iM == 11 ) {
            $iTime += 30;
        }
        elsif ( $iM == 2 ) {
            $iTime += ( LeapYear($iYear) ) ? 29 : 28;
        }
    }
    $iTime += $iDay;
    return $iTime;
}

my @month_days = qw(
  0 31 28 31 30 31 30 31 31 30 31 30 31
);

#------------------------------------------------------------------------------
# ExcelLocaltime (for Spreadsheet::ParseExcel::Utility)
#------------------------------------------------------------------------------
sub ExcelLocaltime {

    my ( $dObj, $flg1904 ) = @_;
    my ( $iSec, $iMin, $iHour, $iDay, $iMon, $iYear, $iwDay, $iMSec );
    my ( $iDt, $iTime, $iYDays, $iMD );

    $iDt   = int($dObj);
    $iTime = $dObj - $iDt;

    #1. Calc Days
    if ($flg1904) {
        $iYear = 1904;
        $iDt++;    #Start from Jan 1st
        $iYDays = 366;
        $iwDay = ( ( $iDt + 4 ) % 7 );
    }
    else {
        $iYear  = 1900;
        $iYDays = 366;    #In Excel 1900 is leap year (That's not TRUE!)
        $iwDay = ( ( $iDt + 6 ) % 7 );
    }
    while ( $iDt > $iYDays ) {
        $iDt -= $iYDays;
        $iYear++;
        $iYDays =
          (      ( ( $iYear % 4 ) == 0 )
              && ( ( $iYear % 100 ) || ( $iYear % 400 ) == 0 ) ) ? 366 : 365;
    }
    $iYear -= 1900;       # Localtime year is relative to 1900.

    for ( $iMon = 1 ; $iMon <= 12 ; $iMon++ ) {
        $iMD = $month_days[$iMon];
        $iMD++ if $iMon == 2 and $iYear % 4 == 0;

        last if ( $iDt <= $iMD );
        $iDt -= $iMD;
    }

    #2. Calc Time
    $iDay = $iDt;
    $iTime += ( 0.0005 / 86400.0 );
    if ($iTime >= 1.0)
    {
        $iTime -= int($iTime);
        $iwDay = ($iwDay == 6) ? 0 : $iwDay + 1;
        if ($iDay == $iMD)
        {
            if ($iMon == 12)
            {
                $iMon = 1;
                $iYear++;
            }
            else
            {
                $iMon++;
            }
            $iDay = 1;
        }
        else
        {
            $iDay++;
        }
    }

    # Localtime month is 0 based.
    $iMon  -= 1;
    $iTime *= 24.0;
    $iHour = int($iTime);
    $iTime -= $iHour;
    $iTime *= 60.0;
    $iMin = int($iTime);
    $iTime -= $iMin;
    $iTime *= 60.0;
    $iSec = int($iTime);
    $iTime -= $iSec;
    $iTime *= 1000.0;
    $iMSec = int($iTime);

    return ( $iSec, $iMin, $iHour, $iDay, $iMon, $iYear, $iwDay, $iMSec );
}

# -----------------------------------------------------------------------------
# col2int (for Spreadsheet::ParseExcel::Utility)
#------------------------------------------------------------------------------
# converts a excel row letter into an int for use in an array
sub col2int {
    my $result = 0;
    my $str    = shift;
    my $incr   = 0;

    for ( my $i = length($str) ; $i > 0 ; $i-- ) {
        my $char = substr( $str, $i - 1 );
        my $curr += ord( lc($char) ) - ord('a') + 1;
        $curr *= $incr if ($incr);
        $result += $curr;
        $incr   += 26;
    }

    # this is one out as we range 0..x-1 not 1..x
    $result--;

    return $result;
}

# -----------------------------------------------------------------------------
# int2col (for Spreadsheet::ParseExcel::Utility)
#------------------------------------------------------------------------------
### int2col
# convert a column number into column letters
# @note this is quite a brute force coarse method
#   does not manage values over 701 (ZZ)
# @arg number, to convert
# @returns string, column name
#
sub int2col {
    my $out = "";
    my $val = shift;

    do {
        $out .= chr( ( $val % 26 ) + ord('A') );
        $val = int( $val / 26 ) - 1;
    } while ( $val >= 0 );

    return scalar reverse $out;
}

# -----------------------------------------------------------------------------
# sheetRef (for Spreadsheet::ParseExcel::Utility)
#------------------------------------------------------------------------------
# -----------------------------------------------------------------------------
### sheetRef
# convert an excel letter-number address into a useful array address
# @note that also Excel uses X-Y notation, we normally use Y-X in arrays
# @args $str, excel coord eg. A2
# @returns an array - 2 elements - column, row, or undefined
#
sub sheetRef {
    my $str = shift;
    my @ret;

    $str =~ m/^(\D+)(\d+)$/;

    if ( $1 && $2 ) {
        push( @ret, $2 - 1, col2int($1) );
    }
    if ( $ret[0] < 0 ) {
        undef @ret;
    }

    return @ret;
}

# -----------------------------------------------------------------------------
# xls2csv (for Spreadsheet::ParseExcel::Utility)
#------------------------------------------------------------------------------
### xls2csv
# convert a chunk of an excel file into csv text chunk
# @args $param, sheet-colrow:colrow (1-A1:B2 or A1:B2 for sheet 1
# @args $rotate, 0 or 1 decides if output should be rotated or not
# @returns string containing a chunk of csv
#
sub xls2csv {
    my ( $filename, $regions, $rotate ) = @_;
    my $sheet = 0;

    # We need Text::CSV_XS for proper CSV handling.
    require Text::CSV_XS;

    # extract any sheet number from the region string
    $regions =~ m/^(\d+)-(.*)/;

    if ($2) {
        $sheet   = $1 - 1;
        $regions = $2;
    }

    # now extract the start and end regions
    $regions =~ m/(.*):(.*)/;

    if ( !$1 || !$2 ) {
        print STDERR "Bad Params";
        return "";
    }

    my @start = sheetRef($1);
    my @end   = sheetRef($2);
    if ( !@start ) {
        print STDERR "Bad coorinates - $1";
        return "";
    }
    if ( !@end ) {
        print STDERR "Bad coorinates - $2";
        return "";
    }

    if ( $start[1] > $end[1] ) {
        print STDERR "Bad COLUMN ordering\n";
        print STDERR "Start column " . int2col( $start[1] );
        print STDERR " after end column " . int2col( $end[1] ) . "\n";
        return "";
    }
    if ( $start[0] > $end[0] ) {
        print STDERR "Bad ROW ordering\n";
        print STDERR "Start row " . ( $start[0] + 1 );
        print STDERR " after end row " . ( $end[0] + 1 ) . "\n";
        exit;
    }

    # start the excel object now
    my $oExcel = new Spreadsheet::ParseExcel;
    my $oBook  = $oExcel->Parse($filename);

    # open the sheet
    my $oWkS = $oBook->{Worksheet}[$sheet];

    # now check that the region exists in the file
    # if not truncate to the possible region
    # output a warning msg
    if ( $start[1] < $oWkS->{MinCol} ) {
        print STDERR int2col( $start[1] )
          . " < min col "
          . int2col( $oWkS->{MinCol} )
          . " Resetting\n";
        $start[1] = $oWkS->{MinCol};
    }
    if ( $end[1] > $oWkS->{MaxCol} ) {
        print STDERR int2col( $end[1] )
          . " > max col "
          . int2col( $oWkS->{MaxCol} )
          . " Resetting\n";
        $end[1] = $oWkS->{MaxCol};
    }
    if ( $start[0] < $oWkS->{MinRow} ) {
        print STDERR ""
          . ( $start[0] + 1 )
          . " < min row "
          . ( $oWkS->{MinRow} + 1 )
          . " Resetting\n";
        $start[0] = $oWkS->{MinCol};
    }
    if ( $end[0] > $oWkS->{MaxRow} ) {
        print STDERR ""
          . ( $end[0] + 1 )
          . " > max row "
          . ( $oWkS->{MaxRow} + 1 )
          . " Resetting\n";
        $end[0] = $oWkS->{MaxRow};

    }

    my $x1 = $start[1];
    my $y1 = $start[0];
    my $x2 = $end[1];
    my $y2 = $end[0];

    my @cell_data;
    my $row = 0;

    if ( !$rotate ) {
        for ( my $y = $y1 ; $y <= $y2 ; $y++ ) {
            for ( my $x = $x1 ; $x <= $x2 ; $x++ ) {
                my $cell = $oWkS->{Cells}[$y][$x];

                my $value;
                if ( defined $cell ) {
                    $value .= $cell->value();
                }
                else {
                    $value = '';
                }

                push @{ $cell_data[$row] }, $value;
            }
            $row++;
        }
    }
    else {
        for ( my $x = $x1 ; $x <= $x2 ; $x++ ) {
            for ( my $y = $y1 ; $y <= $y2 ; $y++ ) {
                my $cell = $oWkS->{Cells}[$y][$x];

                my $value;
                if ( defined $cell ) {
                    $value .= $cell->value();
                }
                else {
                    $value = '';
                }

                push @{ $cell_data[$row] }, $value;
            }
            $row++;
        }
    }

    # Create the CSV output string.
    my $csv = Text::CSV_XS->new( { binary => 1, eol => $/ } );
    my $output = "";

    for my $row (@cell_data) {
        $csv->combine(@$row);
        $output .= $csv->string();
    }

    return $output;
}

1;

__END__

=pod

=head1 NAME

Spreadsheet::ParseExcel::Utility - Utility functions for Spreadsheet::ParseExcel.

=head1 SYNOPSIS

    use Spreadsheet::ParseExcel::Utility qw(ExcelFmt ExcelLocaltime LocaltimeExcel);

    # Convert localtime to Excel time
    my $datetime = LocaltimeExcel(11, 10, 12, 23, 2, 64); # 1964-3-23 12:10:11

    print $datetime, "\n"; # 23459.5070717593 (Excel date/time format)

    # Convert Excel Time to localtime
    my @time = ExcelLocaltime($datetime);
    print join(":", @time), "\n";   # 11:10:12:23:2:64:1:0

    # Formatting
    print ExcelFmt('yyyy-mm-dd', $datetime), "\n"; # 1964-3-23
    print ExcelFmt('m-d-yy',     $datetime), "\n"; # 3-23-64
    print ExcelFmt('#,##0',      $datetime), "\n"; # 23,460
    print ExcelFmt('#,##0.00',   $datetime), "\n"; # 23,459.51

=head1 DESCRIPTION

The C<Spreadsheet::ParseExcel::Utility> module provides utility functions for working with ParseExcel and Excel data.

=head1 Functions

C<Spreadsheet::ParseExcel::Utility> can export the following functions:

    ExcelFmt
    ExcelLocaltime
    LocaltimeExcel
    col2int
    int2col
    sheetRef
    xls2csv

These functions must be imported implicitly:

    # Just one function.
    use Spreadsheet::ParseExcel::Utility 'col2int';

    # More than one.
    use Spreadsheet::ParseExcel::Utility qw(ExcelFmt ExcelLocaltime LocaltimeExcel);


=head2 ExcelFmt($format_string, $number, $is_1904)

Excel stores data such as dates and currency values as numbers. The way these numbers are displayed is controlled by the number format string for the cell. For example a cell with a number format of C<'$#,##0.00'> for currency and a value of 1234.567 would be displayed as follows:

    '$#,##0.00' + 1234.567 = '$1,234.57'.

The C<ExcelFmt()> function tries to emulate this formatting so that the user can convert raw numbers returned by C<Spreadsheet::ParseExel> to a desired format. For example:

    print ExcelFmt('$#,##0.00', 1234.567); # $1,234.57.

The syntax of the function is:

    my $text = ExcelFmt($format_string, $number, $is_1904);

Where C<$format_string> is an Excel number format string, C<$number> is a real or integer number and C<is_1904> is an optional flag to indicate that dates should use Excel's 1904 epoch instead of the default 1900 epoch.

C<ExcelFmt()> is also used internally to convert numbers returned by the C<Cell::unformatted()> method to the formatted value returned by the C<Cell::value()> method:


    my $cell = $worksheet->get_cell( 0, 0 );

    print $cell->unformatted(), "\n"; # 1234.567
    print $cell->value(),       "\n"; # $1,234.57

The most common usage for C<ExcelFmt> is to convert numbers to dates. Dates and times in Excel are represented by real numbers, for example "1 Jan 2001 12:30 PM" is represented by the number 36892.521. The integer part of the number stores the number of days since the epoch and the fractional part stores the percentage of the day. By applying an Excel number format the number is converted to the desired string representation:

    print ExcelFmt('d mmm yyyy h:mm AM/PM', 36892.521);  # 1 Jan 2001 12:30 PM

C<$is_1904> is an optional flag to indicate that dates should use Excel's 1904 epoch instead of the default 1900 epoch. Excel for Windows generally uses 1900 and Excel for Mac OS uses 1904. The C<$is1904> flag isn't required very often by a casual user and can usually be ignored.


=head2 ExcelLocaltime($excel_datetime, $is_1904)

The C<ExcelLocaltime()> function converts from an Excel date/time number to a C<localtime()>-like array of values:

        my @time = ExcelLocaltime($excel_datetime);

        #    0     1     2      3     4       5      6      7
        my ( $sec, $min, $hour, $day, $month, $year, $wday, $msec ) = @time;

The array elements from C<(0 .. 6)> are the same as Perl's C<localtime()>. The last element C<$msec> is milliseconds. In particular it should be noted that, in common with C<localtime()>, the month is zero indexed and the year is the number of years since 1900. This means that you will usually need to do the following:

        $month++;
        $year += 1900;

See also Perl's documentation for L<localtime()|perlfunc>:

The C<$is_1904> flag is an optional. It is used to indicate that dates should use Excel's 1904 epoch instead of the default 1900 epoch.

=head2 LocaltimeExcel($sec, $min, $hour, $day, $month, $year, $wday, $msec, $is_1904)

The C<LocaltimeExcel()> function converts from a C<localtime()>-like array of values to an Excel date/time number:

    $excel_datetime = LocaltimeExcel($sec, $min, $hour, $day, $month, $year, $wday, $msec);

The array elements from C<(0 .. 6)> are the same as Perl's C<localtime()>. The last element C<$msec> is milliseconds. In particular it should be noted that, in common with C<localtime()>, the month is zero indexed and the year is the number of years since 1900. See also Perl's documentation for L<localtime()|perlfunc>:

The C<$wday> and C<$msec> elements are usually optional. This time elements can also be zeroed if they aren't of interest:

                                    # sec, min, hour, day, month, year
    $excel_datetime = LocaltimeExcel( 0,   0,   0,    1,   0,     101 );

    print ExcelFmt('d mmm yyyy', $excel_datetime);  # 1 Jan 2001

The C<$is_1904> flag is also optional. It is used to indicate that dates should use Excel's 1904 epoch instead of the default 1900 epoch.


=head2 col2int($column)

The C<col2int()> function converts an Excel column letter to an zero-indexed column number:

    print col2int('A');  # 0
    print col2int('AA'); # 26

This function was contributed by Kevin Mulholland.


=head2 int2col($column_number)

The C<int2col()> function converts an zero-indexed Excel column number to a column letter:

    print int2col(0);  # 'A'
    print int2col(26); # 'AA'

This function was contributed by Kevin Mulholland.


=head2 sheetRef($cell_string)

The C<sheetRef()> function converts an Excel cell reference in 'A1' notation to a zero-indexed C<(row, col)> pair.

    my ($row, $col) = sheetRef('A1'); # ( 0, 0 )
    my ($row, $col) = sheetRef('C2'); # ( 1, 2 )

This function was contributed by Kevin Mulholland.


=head2 xls2csv($filename, $region, $rotate)

The C<xls2csv()> function converts a section of an Excel file into a CSV text string.

    $csv_text = xls2csv($filename, $region, $rotate);

Where:

    $region = "sheet-colrow:colrow"
    For example '1-A1:B2' means 'A1:B2' for sheet 1.

    and

    $rotate  = 0 or 1 (output is rotated/transposed or not)

This function requires C<Text::CSV_XS> to be installed. It was contributed by Kevin Mulholland along with the C<xls2csv> script in the C<sample> directory of the distro.

See also the following xls2csv utilities: Ken Prows' C<xls2csv>: http://search.cpan.org/~ken/xls2csv/script/xls2csv and H.Merijn Brand's C<xls2csv> (which is part of Spreadsheet::Read): http://search.cpan.org/~hmbrand/Spreadsheet-Read/


=head1 AUTHOR

Current maintainer 0.60+: Douglas Wilson dougw@cpan.org

Maintainer 0.40-0.59: John McNamara jmcnamara@cpan.org

Maintainer 0.27-0.33: Gabor Szabo szabgab@cpan.org

Original author: Kawai Takanori kwitknr@cpan.org

=head1 COPYRIGHT

Copyright (c) 2014 Douglas Wilson

Copyright (c) 2009-2013 John McNamara

Copyright (c) 2006-2008 Gabor Szabo

Copyright (c) 2000-2006 Kawai Takanori

All rights reserved.

You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.

=cut