tafia / calamine

A pure Rust Excel/OpenDocument SpreadSheets file reader: rust on metal sheets
MIT License
1.6k stars 155 forks source link

Feature request: format-aware version of `as_datetime()` #400

Open Expurple opened 5 months ago

Expurple commented 5 months ago

I tried to use as_datetime() to find and parse dates in an xlsx sheet, but it also coerces numbers that aren't formatted like dates. I'd really like to see a smarter, format-aware version of this method that doesn't do this.

I've read some similar issues and I know that doing this correctly is hard. But I found that simply matching DataType::DateTime(..) | DataType::DateTimeIso(..) before calling as_datetime() gives the right result for my use case. Do you approve a new method that has the same logic for these two variants and returns None for DataType::Int and DataType::Float? If yes, how would you name it? After you answer, I can implement and submit it myself.

And we should probably do the same thing for as_date() and as_time().

tafia commented 4 months ago

I'd say this is more a documentation issue here. Lot of excel files show floats or strings instead of dates and there cannot be a one size fits all solution I'm afraid.

Matching over 2 variants is simple enough that we may leave it as it is. Or else add another to_datetime as opposed to as_datetime which encompasses some conversion.