Skip to content

Commit

Permalink
Add dayfirst and yearfirst arguments to parse method (#3)
Browse files Browse the repository at this point in the history
* Add dayfirst and yearfirst arguments to parse method

* Bump version to v0.2.0
  • Loading branch information
m1so authored May 22, 2022
1 parent 74021a1 commit 3eaa53e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 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 = "fastdatetime"
version = "0.1.2"
version = "0.2.0"
edition = "2021"

[lib]
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "fastdatetime"
version = "0.1.2"
version = "0.2.0"
description = "Like datetime, but fast"
keywords = ["datetime", "parsing", "iso8601", "rfc3339"]
requires-python = ">=3.7"
Expand Down
2 changes: 1 addition & 1 deletion python/fastdatetime/fastdatetime.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ from datetime import datetime

def strptime(date_string: str, format: str) -> datetime: ...

def parse(date_string: str) -> datetime: ...
def parse(date_string: str, *, dayfirst: bool = False, yearfirst: bool = False) -> datetime: ...
26 changes: 22 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
use chrono::format::{parse, Parsed, StrftimeItems};

use pyo3::prelude::*;
use pyo3::{once_cell::GILOnceCell, prelude::*};
use time_fmt::parse::{parse_date_time_maybe_with_zone, TimeZoneSpecifier};
use time_tz::{Offset, TimeZone};

mod datetime_utils;
mod interop;

#[pyfunction]
static DEFAULT_PARSER: GILOnceCell<dtparse::Parser> = GILOnceCell::new();

#[pyfunction(date_string, "/", "*", dayfirst = "false", yearfirst = "false")]
#[pyo3(name = "parse")]
fn parse_from_py(date_string: &str) -> PyResult<interop::DateTimeWrapper> {
let (datetime, _offset) = dtparse::parse(date_string)
fn parse_from_py(
py: Python<'_>,
date_string: &str,
dayfirst: Option<bool>,
yearfirst: Option<bool>,
) -> PyResult<interop::DateTimeWrapper> {
let (datetime, _offset, _tokens) = DEFAULT_PARSER
.get_or_init(py, || dtparse::Parser::default())
.parse(
date_string,
dayfirst,
yearfirst,
false,
false,
None,
false,
&std::collections::HashMap::new(),
)
.map_err(|parse_error| pyo3::exceptions::PyValueError::new_err(parse_error.to_string()))?;

Ok(interop::DateTimeWrapper::NaiveDateTime(datetime))
Expand Down

0 comments on commit 3eaa53e

Please sign in to comment.