Python returns 5 digit timestamp, what is this?
Question: I'm a PHP programmer doing a bit of Python (3.4) just because it's way easier
to do it in Python. My script converts a .xlsx file, into many .csv files (one
.csv per sheet).

Here is the code:

    
    
    wb = xlrd.open_workbook(filepath)
    
    for i in range(0, wb.nsheets):
    
        sh = wb.sheet_by_index(i)
        sheet_name = sh.name
        sheet_name = sheet_name.replace(" ", "_");
        fp = open(sheet_name+'.csv', 'at', encoding='utf8')
        wr = csv.writer(fp, quoting=csv.QUOTE_ALL)
    
        for row_num in range(sh.nrows):
            wr.writerow(sh.row_values(row_num))
    
        fp.close()
    

Full code here: <https://github.com/xtrimsky/xlsx_to_csv>

This works well, except that I have a field that is a date, in excel it shows
for example: 01/01/2009

But the final csv, contains a number that is 39814. What is this please, what
can I do with it ? 01/02/2009 is 39815.

Is it a number I can use to find the unix timestamp ? Or is it an issue and I
should change my script ? I would feel safer if it would just return the
string "01/01/2009".

Can someone please help me understand what I am dealing with ?


Answer: If 39814 maps to 2009-1-1 and 39815 maps to 2009-1-2, then it looks like the
ordinal is counting the number of days since 1899-12-30:

    
    
    In [57]: DT.date(1899,12,30) + DT.timedelta(days=39814)
    Out[57]: datetime.date(2009, 1, 1)
    

See [Why 1899-12-30!?](http://stackoverflow.com/questions/10559767/how-to-
convert-ms-excel-date-from-float-to-date-format-in-ruby)

To convert the Excel number to a Unix timestamp, you could use the
[`timetuple`
method](https://docs.python.org/2/library/datetime.html#datetime.date.timetuple)
to convert the `datetime.date` object to a timetuple, and then
[time.mktime](https://docs.python.org/2/library/time.html#time.mktime) to
convert it to a timestamp (seconds since the Epoch):

    
    
    In [80]: import time
    
    In [81]: time.mktime((DT.datetime(1899,12,30) + DT.timedelta(days=39814)).timetuple())
    Out[81]: 1230786000.0
    

