diff --git a/crates/ruff/src/rules/flake8_datetimez/rules/call_datetime_fromtimestamp.rs b/crates/ruff/src/rules/flake8_datetimez/rules/call_datetime_fromtimestamp.rs index f0c86f3271..25c86ae1ec 100644 --- a/crates/ruff/src/rules/flake8_datetimez/rules/call_datetime_fromtimestamp.rs +++ b/crates/ruff/src/rules/flake8_datetimez/rules/call_datetime_fromtimestamp.rs @@ -8,6 +8,44 @@ use crate::rules::flake8_datetimez::rules::helpers::has_non_none_keyword; use super::helpers; +/// ## What it does +/// Checks for usage of `datetime.datetime.fromtimestamp()` without a `tz` +/// argument. +/// +/// ## Why is this bad? +/// Python datetime objects can be naive or timezone-aware. While an aware +/// object represents a specific moment in time, a naive object does not +/// contain enough information to unambiguously locate itself relative to other +/// datetime objects. Since this can lead to errors, it is recommended to +/// always use timezone-aware objects. +/// +/// `datetime.datetime.fromtimestamp(ts)` returns a naive datetime object. +/// Instead, use `datetime.datetime.fromtimestamp(ts, tz=)` to return a +/// timezone-aware object. +/// +/// ## Example +/// ```python +/// import datetime +/// +/// datetime.datetime.fromtimestamp(946684800) +/// ``` +/// +/// Use instead: +/// ```python +/// import datetime +/// +/// datetime.datetime.fromtimestamp(946684800, tz=datetime.timezone.utc) +/// ``` +/// +/// Or, for Python 3.11 and later: +/// ```python +/// import datetime +/// +/// datetime.datetime.fromtimestamp(946684800, tz=datetime.UTC) +/// ``` +/// +/// ## References +/// - [Python documentation: Aware and Naive Objects](https://docs.python.org/3/library/datetime.html#aware-and-naive-objects) #[violation] pub struct CallDatetimeFromtimestamp; @@ -20,7 +58,6 @@ impl Violation for CallDatetimeFromtimestamp { } } -/// DTZ006 pub(crate) fn call_datetime_fromtimestamp(checker: &mut Checker, call: &ast::ExprCall) { if !checker .semantic() diff --git a/crates/ruff/src/rules/flake8_datetimez/rules/call_datetime_now_without_tzinfo.rs b/crates/ruff/src/rules/flake8_datetimez/rules/call_datetime_now_without_tzinfo.rs index 9ebc9ecb27..73dd721684 100644 --- a/crates/ruff/src/rules/flake8_datetimez/rules/call_datetime_now_without_tzinfo.rs +++ b/crates/ruff/src/rules/flake8_datetimez/rules/call_datetime_now_without_tzinfo.rs @@ -8,6 +8,42 @@ use crate::rules::flake8_datetimez::rules::helpers::has_non_none_keyword; use super::helpers; +/// ## What it does +/// Checks for usage of `datetime.datetime.now()` without a `tz` argument. +/// +/// ## Why is this bad? +/// Python datetime objects can be naive or timezone-aware. While an aware +/// object represents a specific moment in time, a naive object does not +/// contain enough information to unambiguously locate itself relative to other +/// datetime objects. Since this can lead to errors, it is recommended to +/// always use timezone-aware objects. +/// +/// `datetime.datetime.now()` returns a naive datetime object. Instead, use +/// `datetime.datetime.now(tz=)` to return a timezone-aware object. +/// +/// ## Example +/// ```python +/// import datetime +/// +/// datetime.datetime.now() +/// ``` +/// +/// Use instead: +/// ```python +/// import datetime +/// +/// datetime.datetime.now(tz=datetime.timezone.utc) +/// ``` +/// +/// Or, for Python 3.11 and later: +/// ```python +/// import datetime +/// +/// datetime.datetime.now(tz=datetime.UTC) +/// ``` +/// +/// ## References +/// - [Python documentation: Aware and Naive Objects](https://docs.python.org/3/library/datetime.html#aware-and-naive-objects) #[violation] pub struct CallDatetimeNowWithoutTzinfo; @@ -18,7 +54,6 @@ impl Violation for CallDatetimeNowWithoutTzinfo { } } -/// DTZ005 pub(crate) fn call_datetime_now_without_tzinfo(checker: &mut Checker, call: &ast::ExprCall) { if !checker .semantic()