Skip to content

Commit

Permalink
Made lxw_datetime_to_excel_date() function public.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Sep 7, 2020
1 parent 4b6c459 commit 13aebf9
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 19 deletions.
24 changes: 23 additions & 1 deletion include/xlsxwriter/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,32 @@ void lxw_rowcol_to_formula_abs(char *formula, const char *sheetname,

uint32_t lxw_name_to_row(const char *row_str);
uint16_t lxw_name_to_col(const char *col_str);

uint32_t lxw_name_to_row_2(const char *row_str);
uint16_t lxw_name_to_col_2(const char *col_str);

double lxw_datetime_to_excel_date(lxw_datetime *datetime, uint8_t date_1904);
/**
* @brief Converts a #lxw_datetime to an Excel datetime number.
*
* @param datetime A pointer to a #lxw_datetime struct.
*
* @return A double representing an Excel datetime.
*
* The `%lxw_datetime_to_excel_datetime()` function converts a datetime in
* #lxw_datetime to and Excel datetime number:
*
* @code
* lxw_datetime datetime = {2013, 2, 28, 12, 0, 0.0};
*
* double excel_datetime = lxw_datetime_to_excel_date(&datetime);
* @endcode
*
* See @ref working_with_dates for more details on the Excel datetime format.
*/
double lxw_datetime_to_excel_datetime(lxw_datetime *datetime);

double lxw_datetime_to_excel_date_epoch(lxw_datetime *datetime,
uint8_t date_1904);

char *lxw_strdup(const char *str);
char *lxw_strdup_formula(const char *formula);
Expand Down
2 changes: 1 addition & 1 deletion src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ lxw_core_free(lxw_core *core)
}

/*
* Convert a time_t struct to a ISO 8601 style "2010-01-01T00:00:00Z" date.
* Convert a time_t to a ISO 8601 style "2010-01-01T00:00:00Z" date.
*/
static void
_datetime_to_iso8601_date(time_t *timer, char *str, size_t size)
Expand Down
14 changes: 12 additions & 2 deletions src/utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,11 @@ lxw_name_to_col_2(const char *col_str)
}

/*
* Convert a lxw_datetime struct to an Excel serial date.
* Convert a lxw_datetime struct to an Excel serial date, with a 1900
* or 1904 epoch.
*/
double
lxw_datetime_to_excel_date(lxw_datetime *datetime, uint8_t date_1904)
lxw_datetime_to_excel_date_epoch(lxw_datetime *datetime, uint8_t date_1904)
{
int year = datetime->year;
int month = datetime->month;
Expand Down Expand Up @@ -409,6 +410,15 @@ lxw_datetime_to_excel_date(lxw_datetime *datetime, uint8_t date_1904)
return days + seconds;
}

/*
* Convert a lxw_datetime struct to an Excel serial date, for the 1900 epoch.
*/
double
lxw_datetime_to_excel_datetime(lxw_datetime *datetime)
{
return lxw_datetime_to_excel_date_epoch(datetime, LXW_FALSE);
}

/* Simple strdup() implementation since it isn't ANSI C. */
char *
lxw_strdup(const char *str)
Expand Down
16 changes: 9 additions & 7 deletions src/worksheet.c
Original file line number Diff line number Diff line change
Expand Up @@ -6935,7 +6935,7 @@ worksheet_write_datetime(lxw_worksheet *self,
if (err)
return err;

excel_date = lxw_datetime_to_excel_date(datetime, LXW_EPOCH_1900);
excel_date = lxw_datetime_to_excel_date_epoch(datetime, LXW_EPOCH_1900);

cell = _new_number_cell(row_num, col_num, excel_date, format);

Expand Down Expand Up @@ -9042,16 +9042,18 @@ worksheet_data_validation_range(lxw_worksheet *self, lxw_row_t first_row,
|| validation->validate == LXW_VALIDATION_TYPE_TIME) {
if (is_between) {
copy->value_number =
lxw_datetime_to_excel_date(&validation->minimum_datetime,
LXW_EPOCH_1900);
lxw_datetime_to_excel_date_epoch(&validation->
minimum_datetime,
LXW_EPOCH_1900);
copy->maximum_number =
lxw_datetime_to_excel_date(&validation->maximum_datetime,
LXW_EPOCH_1900);
lxw_datetime_to_excel_date_epoch(&validation->
maximum_datetime,
LXW_EPOCH_1900);
}
else {
copy->value_number =
lxw_datetime_to_excel_date(&validation->value_datetime,
LXW_EPOCH_1900);
lxw_datetime_to_excel_date_epoch(&validation->value_datetime,
LXW_EPOCH_1900);
}
}

Expand Down
10 changes: 5 additions & 5 deletions test/unit/helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@


#define TEST_ROWCOL_TO_FORMULA_ABS(sheet, row1, col1, row2, col2, exp) \
lxw_rowcol_to_formula_abs(got, sheet, row1, col1, row2, col2); \
lxw_rowcol_to_formula_abs(got, sheet, row1, col1, row2, col2); \
ASSERT_STR(exp, got);


Expand All @@ -86,7 +86,7 @@
datetime->min = _min; \
datetime->sec = _sec; \
\
got = lxw_datetime_to_excel_date(datetime, 0); \
got = lxw_datetime_to_excel_datetime(datetime); \
\
ASSERT_DOUBLE(exp, got); \
free(datetime);
Expand All @@ -97,7 +97,7 @@
datetime->month = _month; \
datetime->day = _day; \
\
got = lxw_datetime_to_excel_date(datetime, 0); \
got = lxw_datetime_to_excel_datetime(datetime); \
\
ASSERT_DOUBLE(exp, got); \
free(datetime);
Expand All @@ -108,7 +108,7 @@
datetime->month = _month; \
datetime->day = _day; \
\
got = lxw_datetime_to_excel_date(datetime, 1); \
got = lxw_datetime_to_excel_date_epoch(datetime, 1); \
\
ASSERT_DOUBLE(exp, got); \
free(datetime);
Expand All @@ -122,7 +122,7 @@
datetime->min = _min; \
datetime->sec = _sec; \
\
got = lxw_datetime_to_excel_date(datetime, 0); \
got = lxw_datetime_to_excel_datetime(datetime); \
\
ASSERT_DOUBLE(exp, got); \
free(datetime);
2 changes: 1 addition & 1 deletion test/unit/worksheet/test_worksheet_conditional_format10.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ CTEST(worksheet, worksheet_condtional_format10) {

conditional_format->type = LXW_CONDITIONAL_TYPE_CELL;
conditional_format->criteria = LXW_CONDITIONAL_CRITERIA_GREATER_THAN;
conditional_format->value = lxw_datetime_to_excel_date(&datetime1, LXW_FALSE);
conditional_format->value = lxw_datetime_to_excel_datetime(&datetime1);
conditional_format->format = NULL;
worksheet_conditional_format_range(worksheet, RANGE("A1:A4"), conditional_format);

Expand Down
4 changes: 2 additions & 2 deletions test/unit/worksheet/test_worksheet_conditional_format11.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ CTEST(worksheet, worksheet_condtional_format11) {

conditional_format->type = LXW_CONDITIONAL_TYPE_CELL;
conditional_format->criteria = LXW_CONDITIONAL_CRITERIA_BETWEEN;
conditional_format->min_value = lxw_datetime_to_excel_date(&datetime1, LXW_FALSE);;
conditional_format->max_value = lxw_datetime_to_excel_date(&datetime2, LXW_FALSE);;
conditional_format->min_value = lxw_datetime_to_excel_datetime(&datetime1);
conditional_format->max_value = lxw_datetime_to_excel_datetime(&datetime2);
worksheet_conditional_format_range(worksheet, RANGE("A1:A4"), conditional_format);


Expand Down

0 comments on commit 13aebf9

Please sign in to comment.