Title: Python datetime — utcnow() DST Bug That Shifts Dates
Open Graph Title: Python datetime — utcnow() DST Bug That Shifts Dates
Description: datetime.utcnow() returns naive timestamps that shift dates during DST transitions. A one-hour offset crossed date boundaries in production.
Open Graph Description: datetime.utcnow() returns naive timestamps that shift dates during DST transitions. A one-hour offset crossed date boundaries in production.
Opengraph URL: https://thecodeforge.io/python/datetime-module-python/
Domain: thecodeforge.io
{
"@context": "https://schema.org",
"@type": "TechArticle",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://thecodeforge.io/python/datetime-module-python/"
},
"headline": "Python datetime — utcnow() DST Bug That Shifts Dates",
"description": "datetime.utcnow() returns naive timestamps that shift dates during DST transitions. A one-hour offset crossed date boundaries in production.",
"keywords": "Python datetime — utcnow() DST Bug That Shifts Dates, Python, Beginner, Python Libraries, datetime.utcnow() returns naive timestamps that shift dates during DST transitions. A one-hour offset crossed date boundaries in production.",
"proficiencyLevel": "Beginner",
"author": {
"@type": "Person",
"@id": "https://thecodeforge.io/about/#naren",
"name": "Naren",
"url": "https://thecodeforge.io/about/"
},
"publisher": {
"@type": "Organization",
"@id": "https://thecodeforge.io/#org",
"name": "TheCodeForge",
"url": "https://thecodeforge.io",
"logo": {"@type": "ImageObject", "url": "https://thecodeforge.io/assets/og-image.png"}
},
"datePublished": "2026-03-17T00:00:00+00:00",
"dateModified": "2026-10-31T00:00:00+00:00",
"url": "https://thecodeforge.io/python/datetime-module-python/",
"inLanguage": "en",
"educationalLevel": "Beginner",
"image": "https://thecodeforge.io/assets/og-image.png",
"isPartOf": {"@id": "https://thecodeforge.io/#website"}
}
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{"@type": "ListItem", "position": 1, "name": "Home", "item": "https://thecodeforge.io/"},
{"@type": "ListItem", "position": 2, "name": "Python", "item": "https://thecodeforge.io/python/"},
{"@type": "ListItem", "position": 3, "name": "Python datetime — utcnow() DST Bug That Shifts Dates", "item": "https://thecodeforge.io/python/datetime-module-python/"}
]
}
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "What is the difference between datetime.now() and datetime.utcnow()?",
"acceptedAnswer": {
"@type": "Answer",
"text": "datetime.now() returns the current local time as a naive datetime (no timezone info). datetime.utcnow() returns UTC as a naive datetime. Both are naive — they carry no timezone information. The correct approach is datetime.now(timezone.utc) which returns an aware UTC datetime."
}
},
{
"@type": "Question",
"name": "How do I get the timestamp (seconds since epoch) from a datetime?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Use datetime_obj.timestamp(). This returns a float representing seconds since the Unix epoch (1970-01-01 00:00:00 UTC). For the reverse: datetime.fromtimestamp(ts, tz=timezone.utc) to convert a timestamp back to an aware datetime."
}
},
{
"@type": "Question",
"name": "Why does adding timedelta(days=30) not equal one month?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Because months have variable lengths (28–31 days). timedelta(days=30) is exactly 30 days, which may cross a month boundary incorrectly. Use dateutil.relativedelta(months=1) to add exactly one calendar month."
}
},
{
"@type": "Question",
"name": "Is it safe to use datetime.now() for logging timestamps?",
"acceptedAnswer": {
"@type": "Answer",
"text": "No, unless your entire system runs in a single timezone and you never compare logs globally. For distributed systems, use datetime.now(timezone.utc) to get an aware UTC timestamp that is unambiguous."
}
}
]
}
| og:type | website |
| og:image | https://thecodeforge.io/assets/og-image.png |
| og:image:width | 1200 |
| og:image:height | 630 |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: max-image-preview:large