[GH-ISSUE #483] Add a way to configure the output file to 1904 excel #376

Closed
opened 2026-05-05 12:13:32 -06:00 by gitea-mirror · 7 comments
Owner

Originally created by @DocJesus on GitHub (Jun 27, 2025).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/483

Originally assigned to: @jmcnamara on GitHub.

Hello,

Unless I am mistaken I noticed that it is not possible to set an output file to 1904 via the lib, despite seeing in the code this enum in common.h:

#define LXW_EPOCH_1900            0
#define LXW_EPOCH_1904            1

I understand that excel would read such status in xl/workbook.xml file in the attribute workbookPr at "date1904" (see exemple below)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
    <fileVersion appName="xl" lastEdited="4" lowestEdited="4" rupBuild="4505"/>
    <workbookPr defaultThemeVersion="124226" date1904="1"/>
    <bookViews>
        <workbookView xWindow="240" yWindow="15" windowWidth="16095" windowHeight="9660"/>
    </bookViews>
    <sheets>
        <sheet name="Planning" sheetId="1" r:id="rId1"/>
        <sheet name="Informations" sheetId="2" r:id="rId2"/>
    </sheets>
    <calcPr calcId="124519" fullCalcOnLoad="1"/>
</workbook>

(will give me a file that has the settings "Use 1904 date system" box ticked)

One way I have to solve this would be manually to unzip the excel file I created, then change the workbook.xml file and zip it back, but wouldn't it be better with a build-in method ?

such method exists in python :
workbook = xlsxwriter.Workbook(filename, {'date_1904': True})

Please feel free to correct me and guide me if it exists a way to do this with the current version of the lib

Many thanks

Originally created by @DocJesus on GitHub (Jun 27, 2025). Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/483 Originally assigned to: @jmcnamara on GitHub. Hello, Unless I am mistaken I noticed that it is not possible to set an output file to 1904 via the lib, despite seeing in the code this enum in common.h: ``` #define LXW_EPOCH_1900 0 #define LXW_EPOCH_1904 1 ``` I understand that excel would read such status in xl/workbook.xml file in the attribute workbookPr at "date1904" (see exemple below) ``` <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> <fileVersion appName="xl" lastEdited="4" lowestEdited="4" rupBuild="4505"/> <workbookPr defaultThemeVersion="124226" date1904="1"/> <bookViews> <workbookView xWindow="240" yWindow="15" windowWidth="16095" windowHeight="9660"/> </bookViews> <sheets> <sheet name="Planning" sheetId="1" r:id="rId1"/> <sheet name="Informations" sheetId="2" r:id="rId2"/> </sheets> <calcPr calcId="124519" fullCalcOnLoad="1"/> </workbook> ``` (will give me a file that has the settings "Use 1904 date system" box ticked) One way I have to solve this would be manually to unzip the excel file I created, then change the workbook.xml file and zip it back, but wouldn't it be better with a build-in method ? such method exists in python : `workbook = xlsxwriter.Workbook(filename, {'date_1904': True})` Please feel free to correct me and guide me if it exists a way to do this with the current version of the lib Many thanks
gitea-mirror 2026-05-05 12:13:32 -06:00
Author
Owner

@jmcnamara commented on GitHub (Jun 27, 2025):

Thanks for the request. I can look into adding it. Most of the infrastructure is already there and, as you point out, I implemented it in the Python and Perl versions.

May I ask why you want to use the 1904 epoch?

<!-- gh-comment-id:3012437380 --> @jmcnamara commented on GitHub (Jun 27, 2025): Thanks for the request. I can look into adding it. Most of the infrastructure is already there and, as you point out, I implemented it in the Python and Perl versions. May I ask why you want to use the 1904 epoch?
Author
Owner

@DocJesus commented on GitHub (Jun 27, 2025):

excel cannot handle negative time, one way to do it is to use a negativ number like "-0.29" and set its format to "[hh]:mm".
Such operation is possible only in 1904 epoch

<!-- gh-comment-id:3012449943 --> @DocJesus commented on GitHub (Jun 27, 2025): excel cannot handle negative time, one way to do it is to use a negativ number like "-0.29" and set its format to "[hh]:mm". Such operation is possible only in 1904 epoch
Author
Owner

@jmcnamara commented on GitHub (Jun 29, 2025):

I've added support for the 1904 epoch to main. It can be enabled via the following workbook function:

    workbook_use_1904_epoch(workbook);

It should be called before workbook_add_worksheet().

Here is an example:

#include "xlsxwriter.h"

int main() {
    lxw_workbook  *workbook  = workbook_new("test_date_1904_02.xlsx");
    workbook_use_1904_epoch(workbook);

    lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);

    lxw_format *format = workbook_add_format(workbook);
    format_set_num_format_index(format, 14);

    lxw_datetime datetime1 = {1904,  1,  1, 0, 0, 0.0};
    lxw_datetime datetime2 = {1906,  9, 27, 0, 0, 0.0};
    lxw_datetime datetime3 = {1917,  9,  9, 0, 0, 0.0};
    lxw_datetime datetime4 = {1931,  5, 19, 0, 0, 0.0};
    lxw_datetime datetime5 = {2177, 10, 15, 0, 0, 0.0};
    lxw_datetime datetime6 = {4641, 11, 27, 0, 0, 0.0};

    worksheet_set_column(worksheet, 0, 0, 12, NULL);

    worksheet_write_datetime(worksheet, CELL("A1"), &datetime1, format);
    worksheet_write_datetime(worksheet, CELL("A2"), &datetime2, format);
    worksheet_write_datetime(worksheet, CELL("A3"), &datetime3, format);
    worksheet_write_datetime(worksheet, CELL("A4"), &datetime4, format);
    worksheet_write_datetime(worksheet, CELL("A5"), &datetime5, format);
    worksheet_write_datetime(worksheet, CELL("A6"), &datetime6, format);

    return workbook_close(workbook);
}

If you get a chance please try it and let me know how you get on.

<!-- gh-comment-id:3016722512 --> @jmcnamara commented on GitHub (Jun 29, 2025): I've added support for the 1904 epoch to main. It can be enabled via the following workbook function: ```C workbook_use_1904_epoch(workbook); ``` It should be called before `workbook_add_worksheet()`. Here is an example: ```C #include "xlsxwriter.h" int main() { lxw_workbook *workbook = workbook_new("test_date_1904_02.xlsx"); workbook_use_1904_epoch(workbook); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); lxw_format *format = workbook_add_format(workbook); format_set_num_format_index(format, 14); lxw_datetime datetime1 = {1904, 1, 1, 0, 0, 0.0}; lxw_datetime datetime2 = {1906, 9, 27, 0, 0, 0.0}; lxw_datetime datetime3 = {1917, 9, 9, 0, 0, 0.0}; lxw_datetime datetime4 = {1931, 5, 19, 0, 0, 0.0}; lxw_datetime datetime5 = {2177, 10, 15, 0, 0, 0.0}; lxw_datetime datetime6 = {4641, 11, 27, 0, 0, 0.0}; worksheet_set_column(worksheet, 0, 0, 12, NULL); worksheet_write_datetime(worksheet, CELL("A1"), &datetime1, format); worksheet_write_datetime(worksheet, CELL("A2"), &datetime2, format); worksheet_write_datetime(worksheet, CELL("A3"), &datetime3, format); worksheet_write_datetime(worksheet, CELL("A4"), &datetime4, format); worksheet_write_datetime(worksheet, CELL("A5"), &datetime5, format); worksheet_write_datetime(worksheet, CELL("A6"), &datetime6, format); return workbook_close(workbook); } ``` If you get a chance please try it and let me know how you get on.
Author
Owner

@DocJesus commented on GitHub (Jun 30, 2025):

thank you very much I will keep you in touch of my tests

<!-- gh-comment-id:3017969756 --> @DocJesus commented on GitHub (Jun 30, 2025): thank you very much I will keep you in touch of my tests
Author
Owner

@DocJesus commented on GitHub (Jun 30, 2025):

I have updated my library and tried the workbook_use_1904_epoch method, it works well

<!-- gh-comment-id:3018182220 --> @DocJesus commented on GitHub (Jun 30, 2025): I have updated my library and tried the workbook_use_1904_epoch method, it works well
Author
Owner

@jmcnamara commented on GitHub (Jun 30, 2025):

I have updated my library and tried the workbook_use_1904_epoch method, it works well

Thanks for letting me know. I'll roll this into a release in the next 1-2 days.

<!-- gh-comment-id:3018207894 --> @jmcnamara commented on GitHub (Jun 30, 2025): > I have updated my library and tried the workbook_use_1904_epoch method, it works well Thanks for letting me know. I'll roll this into a release in the next 1-2 days.
Author
Owner

@jmcnamara commented on GitHub (Jun 30, 2025):

Available in v1.2.3.

<!-- gh-comment-id:3021133248 --> @jmcnamara commented on GitHub (Jun 30, 2025): Available in v1.2.3.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/libxlsxwriter#376
No description provided.