# -*- coding: utf-8 -*-

require 'helper'
require 'write_xlsx'
require 'stringio'

class TestConvertDateTime02 < Minitest::Test
  def test_convert_date_time
    data_for_test.split("\n").each do |line|
      workbook = WriteXLSX.new(StringIO.new)
      workbook.set_1904(false) if line =~ /Excel 1900/
      workbook.set_1904(true)  if line =~ /Excel 1904/
      worksheet = workbook.add_worksheet('')

      next unless line =~ /\S/
      next if     line =~ /^#/

      date, expected = line.split[3, 2]
      expected = expected.to_i

      result = worksheet.convert_date_time(date)
      result = -1 if result.nil?

      assert_equal(expected, result, line)
    end
  end

  def data_for_test
    <<EOS
#
# The following data was generated by Excel.
#

Excel 1900  1      1899-12-31T    0
Excel 1900  2      1900-01-00T    0
Excel 1900  3      1900-01-01T    1
Excel 1900  4      1900-02-27T    58
Excel 1900  5      1900-02-28T    59
Excel 1900  6      1900-02-29T    60
Excel 1900  7      1900-03-01T    61
Excel 1900  8      1900-03-02T    62
Excel 1900  9      1900-03-11T    71
Excel 1900  10     1900-04-08T    99
Excel 1900  11     1900-09-12T    256
Excel 1900  12     1901-05-03T    489
Excel 1900  13     1901-10-13T    652
Excel 1900  14     1902-02-15T    777
Excel 1900  15     1902-06-06T    888
Excel 1900  16     1902-09-25T    999
Excel 1900  17     1902-09-27T    1001
Excel 1900  18     1903-04-26T    1212
Excel 1900  19     1903-08-05T    1313
Excel 1900  20     1903-12-31T    1461
Excel 1900  21     1904-01-01T    1462
Excel 1900  22     1904-02-28T    1520
Excel 1900  23     1904-02-29T    1521
Excel 1900  24     1904-03-01T    1522
Excel 1900  25     1907-02-27T    2615
Excel 1900  26     1907-02-28T    2616
Excel 1900  27     1907-03-01T    2617
Excel 1900  28     1907-03-02T    2618
Excel 1900  29     1907-03-03T    2619
Excel 1900  30     1907-03-04T    2620
Excel 1900  31     1907-03-05T    2621
Excel 1900  32     1907-03-06T    2622
Excel 1900  33     1999-01-01T    36161
Excel 1900  34     1999-01-31T    36191
Excel 1900  35     1999-02-01T    36192
Excel 1900  36     1999-02-28T    36219
Excel 1900  37     1999-03-01T    36220
Excel 1900  38     1999-03-31T    36250
Excel 1900  39     1999-04-01T    36251
Excel 1900  40     1999-04-30T    36280
Excel 1900  41     1999-05-01T    36281
Excel 1900  42     1999-05-31T    36311
Excel 1900  43     1999-06-01T    36312
Excel 1900  44     1999-06-30T    36341
Excel 1900  45     1999-07-01T    36342
Excel 1900  46     1999-07-31T    36372
Excel 1900  47     1999-08-01T    36373
Excel 1900  48     1999-08-31T    36403
Excel 1900  49     1999-09-01T    36404
Excel 1900  50     1999-09-30T    36433
Excel 1900  51     1999-10-01T    36434
Excel 1900  52     1999-10-31T    36464
Excel 1900  53     1999-11-01T    36465
Excel 1900  54     1999-11-30T    36494
Excel 1900  55     1999-12-01T    36495
Excel 1900  56     1999-12-31T    36525
Excel 1900  57     2000-01-01T    36526
Excel 1900  58     2000-01-31T    36556
Excel 1900  59     2000-02-01T    36557
Excel 1900  60     2000-02-29T    36585
Excel 1900  61     2000-03-01T    36586
Excel 1900  62     2000-03-31T    36616
Excel 1900  63     2000-04-01T    36617
Excel 1900  64     2000-04-30T    36646
Excel 1900  65     2000-05-01T    36647
Excel 1900  66     2000-05-31T    36677
Excel 1900  67     2000-06-01T    36678
Excel 1900  68     2000-06-30T    36707
Excel 1900  69     2000-07-01T    36708
Excel 1900  70     2000-07-31T    36738
Excel 1900  71     2000-08-01T    36739
Excel 1900  72     2000-08-31T    36769
Excel 1900  73     2000-09-01T    36770
Excel 1900  74     2000-09-30T    36799
Excel 1900  75     2000-10-01T    36800
Excel 1900  76     2000-10-31T    36830
Excel 1900  77     2000-11-01T    36831
Excel 1900  78     2000-11-30T    36860
Excel 1900  79     2000-12-01T    36861
Excel 1900  80     2000-12-31T    36891
Excel 1900  81     2001-01-01T    36892
Excel 1900  82     2001-01-31T    36922
Excel 1900  83     2001-02-01T    36923
Excel 1900  84     2001-02-28T    36950
Excel 1900  85     2001-03-01T    36951
Excel 1900  86     2001-03-31T    36981
Excel 1900  87     2001-04-01T    36982
Excel 1900  88     2001-04-30T    37011
Excel 1900  89     2001-05-01T    37012
Excel 1900  90     2001-05-31T    37042
Excel 1900  91     2001-06-01T    37043
Excel 1900  92     2001-06-30T    37072
Excel 1900  93     2001-07-01T    37073
Excel 1900  94     2001-07-31T    37103
Excel 1900  95     2001-08-01T    37104
Excel 1900  96     2001-08-31T    37134
Excel 1900  97     2001-09-01T    37135
Excel 1900  98     2001-09-30T    37164
Excel 1900  99     2001-10-01T    37165
Excel 1900  100    2001-10-31T    37195
Excel 1900  101    2001-11-01T    37196
Excel 1900  102    2001-11-30T    37225
Excel 1900  103    2001-12-01T    37226
Excel 1900  104    2001-12-31T    37256
Excel 1900  105    2400-01-01T    182623
Excel 1900  106    2400-01-31T    182653
Excel 1900  107    2400-02-01T    182654
Excel 1900  108    2400-02-29T    182682
Excel 1900  109    2400-03-01T    182683
Excel 1900  110    2400-03-31T    182713
Excel 1900  111    2400-04-01T    182714
Excel 1900  112    2400-04-30T    182743
Excel 1900  113    2400-05-01T    182744
Excel 1900  114    2400-05-31T    182774
Excel 1900  115    2400-06-01T    182775
Excel 1900  116    2400-06-30T    182804
Excel 1900  117    2400-07-01T    182805
Excel 1900  118    2400-07-31T    182835
Excel 1900  119    2400-08-01T    182836
Excel 1900  120    2400-08-31T    182866
Excel 1900  121    2400-09-01T    182867
Excel 1900  122    2400-09-30T    182896
Excel 1900  123    2400-10-01T    182897
Excel 1900  124    2400-10-31T    182927
Excel 1900  125    2400-11-01T    182928
Excel 1900  126    2400-11-30T    182957
Excel 1900  127    2400-12-01T    182958
Excel 1900  128    2400-12-31T    182988
Excel 1900  129    4000-01-01T    767011
Excel 1900  130    4000-01-31T    767041
Excel 1900  131    4000-02-01T    767042
Excel 1900  132    4000-02-29T    767070
Excel 1900  133    4000-03-01T    767071
Excel 1900  134    4000-03-31T    767101
Excel 1900  135    4000-04-01T    767102
Excel 1900  136    4000-04-30T    767131
Excel 1900  137    4000-05-01T    767132
Excel 1900  138    4000-05-31T    767162
Excel 1900  139    4000-06-01T    767163
Excel 1900  140    4000-06-30T    767192
Excel 1900  141    4000-07-01T    767193
Excel 1900  142    4000-07-31T    767223
Excel 1900  143    4000-08-01T    767224
Excel 1900  144    4000-08-31T    767254
Excel 1900  145    4000-09-01T    767255
Excel 1900  146    4000-09-30T    767284
Excel 1900  147    4000-10-01T    767285
Excel 1900  148    4000-10-31T    767315
Excel 1900  149    4000-11-01T    767316
Excel 1900  150    4000-11-30T    767345
Excel 1900  151    4000-12-01T    767346
Excel 1900  152    4000-12-31T    767376
Excel 1900  153    4321-01-01T    884254
Excel 1900  154    4321-01-31T    884284
Excel 1900  155    4321-02-01T    884285
Excel 1900  156    4321-02-28T    884312
Excel 1900  157    4321-03-01T    884313
Excel 1900  158    4321-03-31T    884343
Excel 1900  159    4321-04-01T    884344
Excel 1900  160    4321-04-30T    884373
Excel 1900  161    4321-05-01T    884374
Excel 1900  162    4321-05-31T    884404
Excel 1900  163    4321-06-01T    884405
Excel 1900  164    4321-06-30T    884434
Excel 1900  165    4321-07-01T    884435
Excel 1900  166    4321-07-31T    884465
Excel 1900  167    4321-08-01T    884466
Excel 1900  168    4321-08-31T    884496
Excel 1900  169    4321-09-01T    884497
Excel 1900  170    4321-09-30T    884526
Excel 1900  171    4321-10-01T    884527
Excel 1900  172    4321-10-31T    884557
Excel 1900  173    4321-11-01T    884558
Excel 1900  174    4321-11-30T    884587
Excel 1900  175    4321-12-01T    884588
Excel 1900  176    4321-12-31T    884618
Excel 1900  177    9999-01-01T    2958101
Excel 1900  178    9999-01-31T    2958131
Excel 1900  179    9999-02-01T    2958132
Excel 1900  180    9999-02-28T    2958159
Excel 1900  181    9999-03-01T    2958160
Excel 1900  182    9999-03-31T    2958190
Excel 1900  183    9999-04-01T    2958191
Excel 1900  184    9999-04-30T    2958220
Excel 1900  185    9999-05-01T    2958221
Excel 1900  186    9999-05-31T    2958251
Excel 1900  187    9999-06-01T    2958252
Excel 1900  188    9999-06-30T    2958281
Excel 1900  189    9999-07-01T    2958282
Excel 1900  190    9999-07-31T    2958312
Excel 1900  191    9999-08-01T    2958313
Excel 1900  192    9999-08-31T    2958343
Excel 1900  193    9999-09-01T    2958344
Excel 1900  194    9999-09-30T    2958373
Excel 1900  195    9999-10-01T    2958374
Excel 1900  196    9999-10-31T    2958404
Excel 1900  197    9999-11-01T    2958405
Excel 1900  198    9999-11-30T    2958434
Excel 1900  199    9999-12-01T    2958435
Excel 1900  200    9999-12-31T    2958465

#
# Excel 1904 date system
#
Excel 1904  201    1904-01-01T    0
Excel 1904  202    1904-01-31T    30
Excel 1904  203    1904-02-01T    31
Excel 1904  204    1904-02-29T    59
Excel 1904  205    1904-03-01T    60
Excel 1904  206    1904-03-31T    90
Excel 1904  207    1904-04-01T    91
Excel 1904  208    1904-04-30T    120
Excel 1904  209    1904-05-01T    121
Excel 1904  210    1904-05-31T    151
Excel 1904  211    1904-06-01T    152
Excel 1904  212    1904-06-30T    181
Excel 1904  213    1904-07-01T    182
Excel 1904  214    1904-07-31T    212
Excel 1904  215    1904-08-01T    213
Excel 1904  216    1904-08-31T    243
Excel 1904  217    1904-09-01T    244
Excel 1904  218    1904-09-30T    273
Excel 1904  219    1904-10-01T    274
Excel 1904  220    1904-10-31T    304
Excel 1904  221    1904-11-01T    305
Excel 1904  222    1904-11-30T    334
Excel 1904  223    1904-12-01T    335
Excel 1904  224    1904-12-31T    365
Excel 1904  225    1907-02-27T    1153
Excel 1904  226    1907-02-28T    1154
Excel 1904  227    1907-03-01T    1155
Excel 1904  228    1907-03-02T    1156
Excel 1904  229    1907-03-03T    1157
Excel 1904  230    1907-03-04T    1158
Excel 1904  231    1907-03-05T    1159
Excel 1904  232    1907-03-06T    1160
Excel 1904  233    1999-01-01T    34699
Excel 1904  234    1999-01-31T    34729
Excel 1904  235    1999-02-01T    34730
Excel 1904  236    1999-02-28T    34757
Excel 1904  237    1999-03-01T    34758
Excel 1904  238    1999-03-31T    34788
Excel 1904  239    1999-04-01T    34789
Excel 1904  240    1999-04-30T    34818
Excel 1904  241    1999-05-01T    34819
Excel 1904  242    1999-05-31T    34849
Excel 1904  243    1999-06-01T    34850
Excel 1904  244    1999-06-30T    34879
Excel 1904  245    1999-07-01T    34880
Excel 1904  246    1999-07-31T    34910
Excel 1904  247    1999-08-01T    34911
Excel 1904  248    1999-08-31T    34941
Excel 1904  249    1999-09-01T    34942
Excel 1904  250    1999-09-30T    34971
Excel 1904  251    1999-10-01T    34972
Excel 1904  252    1999-10-31T    35002
Excel 1904  253    1999-11-01T    35003
Excel 1904  254    1999-11-30T    35032
Excel 1904  255    1999-12-01T    35033
Excel 1904  256    1999-12-31T    35063
Excel 1904  257    2000-01-01T    35064
Excel 1904  258    2000-01-31T    35094
Excel 1904  259    2000-02-01T    35095
Excel 1904  260    2000-02-29T    35123
Excel 1904  261    2000-03-01T    35124
Excel 1904  262    2000-03-31T    35154
Excel 1904  263    2000-04-01T    35155
Excel 1904  264    2000-04-30T    35184
Excel 1904  265    2000-05-01T    35185
Excel 1904  266    2000-05-31T    35215
Excel 1904  267    2000-06-01T    35216
Excel 1904  268    2000-06-30T    35245
Excel 1904  269    2000-07-01T    35246
Excel 1904  270    2000-07-31T    35276
Excel 1904  271    2000-08-01T    35277
Excel 1904  272    2000-08-31T    35307
Excel 1904  273    2000-09-01T    35308
Excel 1904  274    2000-09-30T    35337
Excel 1904  275    2000-10-01T    35338
Excel 1904  276    2000-10-31T    35368
Excel 1904  277    2000-11-01T    35369
Excel 1904  278    2000-11-30T    35398
Excel 1904  279    2000-12-01T    35399
Excel 1904  280    2000-12-31T    35429
Excel 1904  281    2001-01-01T    35430
Excel 1904  282    2001-01-31T    35460
Excel 1904  283    2001-02-01T    35461
Excel 1904  284    2001-02-28T    35488
Excel 1904  285    2001-03-01T    35489
Excel 1904  286    2001-03-31T    35519
Excel 1904  287    2001-04-01T    35520
Excel 1904  288    2001-04-30T    35549
Excel 1904  289    2001-05-01T    35550
Excel 1904  290    2001-05-31T    35580
Excel 1904  291    2001-06-01T    35581
Excel 1904  292    2001-06-30T    35610
Excel 1904  293    2001-07-01T    35611
Excel 1904  294    2001-07-31T    35641
Excel 1904  295    2001-08-01T    35642
Excel 1904  296    2001-08-31T    35672
Excel 1904  297    2001-09-01T    35673
Excel 1904  298    2001-09-30T    35702
Excel 1904  299    2001-10-01T    35703
Excel 1904  300    2001-10-31T    35733
Excel 1904  301    2001-11-01T    35734
Excel 1904  302    2001-11-30T    35763
Excel 1904  303    2001-12-01T    35764
Excel 1904  304    2001-12-31T    35794
Excel 1904  305    2400-01-01T    181161
Excel 1904  306    2400-01-31T    181191
Excel 1904  307    2400-02-01T    181192
Excel 1904  308    2400-02-29T    181220
Excel 1904  309    2400-03-01T    181221
Excel 1904  310    2400-03-31T    181251
Excel 1904  311    2400-04-01T    181252
Excel 1904  312    2400-04-30T    181281
Excel 1904  313    2400-05-01T    181282
Excel 1904  314    2400-05-31T    181312
Excel 1904  315    2400-06-01T    181313
Excel 1904  316    2400-06-30T    181342
Excel 1904  317    2400-07-01T    181343
Excel 1904  318    2400-07-31T    181373
Excel 1904  319    2400-08-01T    181374
Excel 1904  320    2400-08-31T    181404
Excel 1904  321    2400-09-01T    181405
Excel 1904  322    2400-09-30T    181434
Excel 1904  323    2400-10-01T    181435
Excel 1904  324    2400-10-31T    181465
Excel 1904  325    2400-11-01T    181466
Excel 1904  326    2400-11-30T    181495
Excel 1904  327    2400-12-01T    181496
Excel 1904  328    2400-12-31T    181526
Excel 1904  329    4000-01-01T    765549
Excel 1904  330    4000-01-31T    765579
Excel 1904  331    4000-02-01T    765580
Excel 1904  332    4000-02-29T    765608
Excel 1904  333    4000-03-01T    765609
Excel 1904  334    4000-03-31T    765639
Excel 1904  335    4000-04-01T    765640
Excel 1904  336    4000-04-30T    765669
Excel 1904  337    4000-05-01T    765670
Excel 1904  338    4000-05-31T    765700
Excel 1904  339    4000-06-01T    765701
Excel 1904  340    4000-06-30T    765730
Excel 1904  341    4000-07-01T    765731
Excel 1904  342    4000-07-31T    765761
Excel 1904  343    4000-08-01T    765762
Excel 1904  344    4000-08-31T    765792
Excel 1904  345    4000-09-01T    765793
Excel 1904  346    4000-09-30T    765822
Excel 1904  347    4000-10-01T    765823
Excel 1904  348    4000-10-31T    765853
Excel 1904  349    4000-11-01T    765854
Excel 1904  350    4000-11-30T    765883
Excel 1904  351    4000-12-01T    765884
Excel 1904  352    4000-12-31T    765914
Excel 1904  353    4321-01-01T    882792
Excel 1904  354    4321-01-31T    882822
Excel 1904  355    4321-02-01T    882823
Excel 1904  356    4321-02-28T    882850
Excel 1904  357    4321-03-01T    882851
Excel 1904  358    4321-03-31T    882881
Excel 1904  359    4321-04-01T    882882
Excel 1904  360    4321-04-30T    882911
Excel 1904  361    4321-05-01T    882912
Excel 1904  362    4321-05-31T    882942
Excel 1904  363    4321-06-01T    882943
Excel 1904  364    4321-06-30T    882972
Excel 1904  365    4321-07-01T    882973
Excel 1904  366    4321-07-31T    883003
Excel 1904  367    4321-08-01T    883004
Excel 1904  368    4321-08-31T    883034
Excel 1904  369    4321-09-01T    883035
Excel 1904  370    4321-09-30T    883064
Excel 1904  371    4321-10-01T    883065
Excel 1904  372    4321-10-31T    883095
Excel 1904  373    4321-11-01T    883096
Excel 1904  374    4321-11-30T    883125
Excel 1904  375    4321-12-01T    883126
Excel 1904  376    4321-12-31T    883156
Excel 1904  377    9999-01-01T    2956639
Excel 1904  378    9999-01-31T    2956669
Excel 1904  379    9999-02-01T    2956670
Excel 1904  380    9999-02-28T    2956697
Excel 1904  381    9999-03-01T    2956698
Excel 1904  382    9999-03-31T    2956728
Excel 1904  383    9999-04-01T    2956729
Excel 1904  384    9999-04-30T    2956758
Excel 1904  385    9999-05-01T    2956759
Excel 1904  386    9999-05-31T    2956789
Excel 1904  387    9999-06-01T    2956790
Excel 1904  388    9999-06-30T    2956819
Excel 1904  389    9999-07-01T    2956820
Excel 1904  390    9999-07-31T    2956850
Excel 1904  391    9999-08-01T    2956851
Excel 1904  392    9999-08-31T    2956881
Excel 1904  393    9999-09-01T    2956882
Excel 1904  394    9999-09-30T    2956911
Excel 1904  395    9999-10-01T    2956912
Excel 1904  396    9999-10-31T    2956942
Excel 1904  397    9999-11-01T    2956943
Excel 1904  398    9999-11-30T    2956972
Excel 1904  399    9999-12-01T    2956973
Excel 1904  400    9999-12-31T    2957003


#
# The following dates are invalid.
#

#
# Excel 1900 date system
#
Excel 1900  401    0000-12-30T    -1 # Below year range.
Excel 1900  402    1000-12-30T    -1 # Below year range.
Excel 1900  403    1899-12-30T    -1 # Below year range.
Excel 1900  404    2002-02-29T    -1 # False leap-day.
Excel 1900  405    2000-00-00T    -1 # No month or day.
Excel 1900  406    2000-01-00T    -1 # No day.
Excel 1900  407    2000-00-01T    -1 # No month.
Excel 1900  408    2000-13-01T    -1 # Month out of range.
Excel 1900  409    2000-12-32T    -1 # Day out of range.
Excel 1900  410   10000-01-01T    -1 # Year out of range.

#
# Excel 1904 date system
#
Excel 1904  411    1899-12-31T    -1 # Below year range.
Excel 1904  412    1900-01-01T    -1 # Below year range.
Excel 1904  413    1903-12-31T    -1 # Below year range.
Excel 1904  414    2001-02-29T    -1 # False leap-day.
Excel 1904  415    2000-00-00T    -1 # No month or day.
Excel 1904  416    2000-01-00T    -1 # No day.
Excel 1904  417    2000-00-01T    -1 # No month.
Excel 1904  418    2000-13-01T    -1 # Month out of range.
Excel 1904  419    2000-12-32T    -1 # Day out of range.
Excel 1904  420   10000-01-01T    -1 # Year out of range.
EOS
  end
end
