Skip to content

Commit

Permalink
slightly improve builtin_date_format fn
Browse files Browse the repository at this point in the history
  • Loading branch information
tafia committed May 29, 2023
1 parent 430c2f4 commit 7028307
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "calamine"
version = "0.19.1"
version = "0.20.0"
authors = ["Johann Tuffe <[email protected]>"]
repository = "https://github.com/tafia/calamine"
documentation = "https://docs.rs/calamine"
Expand Down
73 changes: 34 additions & 39 deletions src/formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,40 @@ pub fn is_custom_date_format(format: &str) -> bool {
}

pub fn is_builtin_date_format_id(id: &[u8]) -> bool {
match id {
// mm-dd-yy
b"14" |
&[14, 0] |
// d-mmm-yy
b"15" |
&[15, 0] |
// d-mmm
b"16" |
&[16, 0] |
// mmm-yy
b"17" |
&[17, 0] |
// h:mm AM/PM
b"18" |
&[18, 0] |
// h:mm:ss AM/PM
b"19" |
&[19, 0] |
// h:mm
b"20" |
&[20, 0] |
// h:mm:ss
b"21" |
&[21, 0] |
// m/d/yy h:mm
b"22" |
&[22, 0] |
// mm:ss
b"45" |
&[45, 0] |
// [h]:mm:ss
b"46" |
&[46, 0] |
// mmss.0
b"47" |
&[47, 0] => true,
_ => false
}
matches!(
id,
// mm-dd-yy
b"14" |
// d-mmm-yy
b"15" |
// d-mmm
b"16" |
// mmm-yy
b"17" |
// h:mm AM/PM
b"18" |
// h:mm:ss AM/PM
b"19" |
// h:mm
b"20" |
// h:mm:ss
b"21" |
// m/d/yy h:mm
b"22" |
// mm:ss
b"45" |
// [h]:mm:ss
b"46" |
// mmss.0
b"47"
)
}

/// Check if code corresponds to builtin date format
///
/// See `is_builtin_date_format_id`
pub fn is_builtin_date_format_code(code: u16) -> bool {
matches!(code, 14..=22 | 45..=47)
}

/// Ported from openpyxl, MIT License
Expand Down
4 changes: 2 additions & 2 deletions src/xls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::marker::PhantomData;
use log::debug;

use crate::cfb::{Cfb, XlsEncoding};
use crate::formats::{is_builtin_date_format_id, is_custom_date_format};
use crate::formats::{is_builtin_date_format_code, is_custom_date_format};
#[cfg(feature = "picture")]
use crate::utils::read_usize;
use crate::utils::{push_column, read_f64, read_i32, read_u16, read_u32};
Expand Down Expand Up @@ -345,7 +345,7 @@ impl<RS: Read + Seek> Xls<RS> {
.into_iter()
.map(|fmt| match formats.get(&fmt) {
Some(s) => *s,
None if is_builtin_date_format_id(&fmt.to_le_bytes()) => CellFormat::Date(is_1904),
None if is_builtin_date_format_code(fmt) => CellFormat::Date(is_1904),
_ => CellFormat::Other,
})
.collect();
Expand Down

0 comments on commit 7028307

Please sign in to comment.