Skip to content

Commit c706e59

Browse files
Merge pull request #254 from phenobarbital/new-exports
using ciso for iso-formatted datetimes to avoid dropping timezone inf…
2 parents 6a1cb09 + b1c045f commit c706e59

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

datamodel/converters.pyx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,17 @@ cpdef datetime.datetime to_datetime(object obj):
194194
except ValueError:
195195
pass
196196
try:
197-
return rc.to_datetime(obj)
197+
return ciso8601.parse_datetime(obj)
198198
except ValueError:
199199
pass
200200
try:
201-
return ciso8601.parse_datetime(obj)
201+
return rc.to_datetime(obj)
202202
except ValueError:
203203
raise ValueError(
204204
f"Can't convert invalid data *{obj}* to datetime"
205205
)
206206

207+
207208
cpdef object to_integer(object obj):
208209
"""to_integer.
209210

datamodel/rs_parsers/src/lib.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use pyo3::exceptions::PyValueError;
33
use pyo3::PyTypeInfo;
44
use pyo3::wrap_pyfunction;
55
use pyo3::types::{PyDate, PyDateTime, PyAny, PyString, PyBool, PyBytes, PyInt, PyFloat, PyList};
6-
use chrono::{Datelike, Timelike, NaiveDate, NaiveDateTime, DateTime, Utc};
6+
use chrono::{Datelike, Timelike, NaiveDate, NaiveDateTime, DateTime};
77
use speedate::Date as SpeeDate;
88
use speedate::DateTime as SpeeDateTime;
99
use uuid::Uuid;
@@ -66,7 +66,7 @@ fn to_list(py: Python, py_type: Py<PyAny>, input_list: Py<PyList>) -> PyResult<P
6666
let mut result_list: Vec<PyObject> = Vec::new();
6767

6868
for item in input_list.iter() {
69-
let converted_item = Python::with_gil(|py| {
69+
let converted_item = Python::with_gil(|_py: Python<'_>| {
7070
let py_type = py_type.clone();
7171
let item_obj: PyObject = item.into();
7272
py_type.call1((item_obj,)).map(|obj| obj.into())
@@ -266,7 +266,6 @@ fn to_datetime(py: Python, input: &str, custom_format: Option<&str>) -> PyResult
266266
return Err(PyValueError::new_err("Input string is empty"));
267267
}
268268

269-
// Attempt parsing using Speedate
270269
// Attempt parsing using Speedate
271270
if let Ok(parsed_datetime) = SpeeDateTime::parse_str(input) {
272271
return Ok(PyDateTime::new(
@@ -282,14 +281,6 @@ fn to_datetime(py: Python, input: &str, custom_format: Option<&str>) -> PyResult
282281
)?.into());
283282
}
284283

285-
// Try parsing as ISO 8601 datetime with timezone.
286-
if let Ok(datetime) = DateTime::parse_from_rfc3339(input) {
287-
let datetime_utc = datetime.with_timezone(&Utc);
288-
return Ok(
289-
PyDateTime::from_timestamp(py, datetime_utc.timestamp() as f64, None,)?.into()
290-
);
291-
}
292-
293284
// Define custom formats to try, including the optional format.
294285
let mut formats = vec![
295286
"%Y-%m-%d", // ISO 8601 date

datamodel/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
'simple library based on python +3.8 to use Dataclass-syntax'
77
'for interacting with Data'
88
)
9-
__version__ = '0.10.13'
9+
__version__ = '0.10.14'
1010
__copyright__ = 'Copyright (c) 2020-2024 Jesus Lara'
1111
__author__ = 'Jesus Lara'
1212
__author_email__ = '[email protected]'

examples/timezone.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from datetime import datetime
2+
from datamodel import BaseModel, Field
3+
4+
5+
class DateTest(BaseModel):
6+
date_field: datetime = Field(required=True)
7+
8+
9+
if __name__ == '__main__':
10+
data = {'date_field': "2025-03-24T11:10:22-05:00"}
11+
date_test = DateTest(**data)
12+
print(date_test.date_field)
13+
print(date_test.to_dict())
14+
print(date_test.to_json())

0 commit comments

Comments
 (0)