<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/the-portable-way-to-extract-date-and-time-from-a-file-s-mtime-in-c--mx0vimtmg" -->

---
title: The Portable Way to Extract Date and Time from a File's...
description: Extracting year, month, day, hour, minute, and second from a file's modification time in C++ is harder than it looks due to three portability layers:...
canonical: https://daily.dev/posts/the-portable-way-to-extract-date-and-time-from-a-file-s-mtime-in-c--mx0vimtmg
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: The Portable Way to Extract Date and Time from a File's mtime in C++ | daily.dev
og:description: Extracting year, month, day, hour, minute, and second from a file's modification time in C++ is harder than it looks due to three portability layers:...
og:url: https://daily.dev/posts/the-portable-way-to-extract-date-and-time-from-a-file-s-mtime-in-c--mx0vimtmg
og:image: https://api.daily.dev/og/posts/mX0viMtmG.png
og:image:alt: The Portable Way to Extract Date and Time from a File's mtime in C++
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# The Portable Way to Extract Date and Time from a File's mtime in C++

**[Sandor Dargo](https://daily.dev/sources/sandordargo)** · 9 min read · 2 upvotes · 0 comments

## Summary

Extracting year, month, day, hour, minute, and second from a file's modification time in C++ is harder than it looks due to three portability layers: file_clock epochs differ across implementations (Unix epoch on libc++, Windows FILETIME on MSVC, a future 2174 epoch on GCC), conversion APIs are split (to_sys() vs to_utc()), and clock_cast was missing from libc++ until recently. Now that clock_cast is available on all three major implementations, the clean solution is to use clock_cast<system_clock> followed by year_month_day and hh_mm_ss calendar types. For display-only needs, std::format works directly on file_time_type. A manual clock correlation fallback exists for older toolchains.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.sandordargo.com/blog/2026/07/22/extracting-datetime-from-mtime>

## Questions this post answers

### How do I portably convert a std::filesystem::file_time_type to year, month, day, hour, minute, and second in C++20?

Use std::chrono::clock_cast to convert the file_time_type to a system_clock time point, then use std::chrono::year_month_day and std::chrono::hh_mm_ss to extract the calendar components. Floor the sys_time to days for the date, and take the difference for the time of day. This works identically on libc++, libstdc++, and MSVC now that all three ship clock_cast, requiring no platform-specific ifdef branching.

_Developers wiring up cross-platform timestamp code can track chrono portability fixes like clock_cast on daily.dev._

### Why does GCC's libstdc++ file_clock epoch start in 2174 instead of 1970?

Libstdc++ represents file_clock time as a signed 64-bit integer with nanosecond resolution, which covers roughly plus or minus 292 years. Centering that range at 2174 instead of 1970 lets it cover 1882 through 2466, matching the range ext4 filesystems need to represent. As a result, current dates have negative time_since_epoch values on GCC.

_Anyone debugging odd file_clock arithmetic across compilers can follow these epoch quirks on daily.dev._

### Why does my code calling file_clock::to_sys() fail to compile on MSVC?

C++20 requires file_clock to provide either to_sys()/from_sys() or to_utc()/from_utc(), but not both, and implementations chose oppositely: GCC and Clang provide to_sys(), while MSVC only provides to_utc(). Code calling to_sys() therefore compiles on GCC and Clang but fails on MSVC, and vice versa for to_utc(). Using std::chrono::clock_cast avoids this split entirely.

_Teams hitting platform-specific chrono compile errors can keep up with these gaps via daily.dev._

## Similar posts on daily.dev

- [September 23, 2026](https://daily.dev/posts/september-23-2026-r0obhdm3j) · The Old New Thing · 0 upvotes · 0 comments
- [Time in C\+\+: Additional clocks in C\+\+20](https://daily.dev/posts/time-in-c-additional-clocks-in-c-20-tvjsm1ztd) · Sandor Dargo · 4 upvotes · 1 comments
- [C\+\+20 changes to std::chrono you might not be aware of: clocks and more](https://daily.dev/posts/c-20-changes-to-std-chrono-you-might-not-be-aware-of-clocks-and-more-c9r3ugq6k) · Meeting C\+\+ · 2 upvotes · 0 comments
- [Time in C\+\+: C\+\+20 Brought Us Time Zones](https://daily.dev/posts/time-in-c-c-20-brought-us-time-zones-gcuwlclgg) · Sandor Dargo · 1 upvotes · 1 comments
- [A faster way to convert a timestamp to Hour, Min, Sec](https://daily.dev/posts/a-faster-way-to-convert-a-timestamp-to-hour-min-sec-5afqsbet9) · Lobsters · 6 upvotes · 0 comments

---

Tags: [#c++](https://daily.dev/tags/c++)

[View this post on daily.dev](https://daily.dev/posts/the-portable-way-to-extract-date-and-time-from-a-file-s-mtime-in-c--mx0vimtmg)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"The Portable Way to Extract Date and Time from a File's mtime in C++","url":"https://daily.dev/posts/the-portable-way-to-extract-date-and-time-from-a-file-s-mtime-in-c--mx0vimtmg","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/the-portable-way-to-extract-date-and-time-from-a-file-s-mtime-in-c--mx0vimtmg"},"datePublished":"2026-07-22T07:26:18.850Z","dateModified":"2026-09-13T19:53:45.246Z","description":"Extracting year, month, day, hour, minute, and second from a file's modification time in C++ is harder than it looks due to three portability layers:...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/bc56fe31dfb8fc6b112ce220016c1fe6?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/bc56fe31dfb8fc6b112ce220016c1fe6?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Sandor Dargo","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Sandor Dargo","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/48d17993e5864d66951193405fa786bb","url":"https://daily.dev/sources/sandordargo"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/the-portable-way-to-extract-date-and-time-from-a-file-s-mtime-in-c--mx0vimtmg","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":2},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"c++","timeRequired":"PT9M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Sandor Dargo","item":"https://daily.dev/sources/sandordargo"},{"@type":"ListItem","position":3,"name":"The Portable Way to Extract Date and Time from a File's mtime in C++"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/the-portable-way-to-extract-date-and-time-from-a-file-s-mtime-in-c--mx0vimtmg#faq","mainEntity":[{"@type":"Question","name":"How do I portably convert a std::filesystem::file_time_type to year, month, day, hour, minute, and second in C++20?","acceptedAnswer":{"@type":"Answer","text":"Use std::chrono::clock_cast to convert the file_time_type to a system_clock time point, then use std::chrono::year_month_day and std::chrono::hh_mm_ss to extract the calendar components. Floor the sys_time to days for the date, and take the difference for the time of day. This works identically on libc++, libstdc++, and MSVC now that all three ship clock_cast, requiring no platform-specific ifdef branching. Developers wiring up cross-platform timestamp code can track chrono portability fixes like clock_cast on daily.dev."}},{"@type":"Question","name":"Why does GCC's libstdc++ file_clock epoch start in 2174 instead of 1970?","acceptedAnswer":{"@type":"Answer","text":"Libstdc++ represents file_clock time as a signed 64-bit integer with nanosecond resolution, which covers roughly plus or minus 292 years. Centering that range at 2174 instead of 1970 lets it cover 1882 through 2466, matching the range ext4 filesystems need to represent. As a result, current dates have negative time_since_epoch values on GCC. Anyone debugging odd file_clock arithmetic across compilers can follow these epoch quirks on daily.dev."}},{"@type":"Question","name":"Why does my code calling file_clock::to_sys() fail to compile on MSVC?","acceptedAnswer":{"@type":"Answer","text":"C++20 requires file_clock to provide either to_sys()/from_sys() or to_utc()/from_utc(), but not both, and implementations chose oppositely: GCC and Clang provide to_sys(), while MSVC only provides to_utc(). Code calling to_sys() therefore compiles on GCC and Clang but fails on MSVC, and vice versa for to_utc(). Using std::chrono::clock_cast avoids this split entirely. Teams hitting platform-specific chrono compile errors can keep up with these gaps via daily.dev."}}]}
```

