TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The surprising struggle to get a Unix Epoch time from a UTC string in C or C++

131 pointsby PascalW4 months ago

15 comments

chikere2324 months ago
Is it a struggle though?<p>They needed to have a locale matching the language of the localised time string they wanted to parse, they needed to use strptime to parse the string, they needed to use timegm() to convert the result to seconds when seen as UTC. The man pages pretty much describe these things.<p>The interface or these things could certainly be nicer, but most of the things they bring up as issues aren&#x27;t even relevant for the task they&#x27;re trying to do. Why do they talk about daylight savings time being confusing when they&#x27;re only trying to deal with UTC which doesn&#x27;t have it?
评论 #42760980 未加载
评论 #42769135 未加载
评论 #42764909 未加载
评论 #42760858 未加载
d_burfoot4 months ago
My personal rule for time processing: use the language-provided libraries for ONLY 2 operations: converting back and forth between a formatted time string with a time zone, and a Unix epoch timestamp. Perform all other time processing in your own code based on those 2 operations, and whenever you start with a new language or framework, just learn those 2.<p>I&#x27;ve wasted so many dreary hours trying to figure out crappy time processing APIs and libraries. Never again!
评论 #42764798 未加载
1970-01-014 months ago
13 more years to go until the 2038 problem.<p>Surely we&#x27;ll have everything patched up by then..
评论 #42760503 未加载
评论 #42768408 未加载
评论 #42771183 未加载
account424 months ago
The concept of a process-wide locale was a mistake. All locale-dependent functons should be explicit. Yes that means some programs won&#x27;t respect your locale because the author didn&#x27;t care to add support but at least they won&#x27;t break in unexpected ways because some functions magically work differently between the user&#x27;s and developers system.
评论 #42770044 未加载
评论 #42772363 未加载
评论 #42772341 未加载
jonstewart4 months ago
The headline doesn’t match the article. As it points out, C++20 has a very nice, and portable, time library. I quibble with the article here, though: in 2025, C++20 is widely available.
评论 #42761924 未加载
评论 #42762992 未加载
zX41ZdbW4 months ago
The first rule of thumb is to never use functions from glibc (gmtime, localtime, mktime, etc) because half of them are non-thread-safe, and another half use a global mutex, and they are unreasonably slow. The second rule of thumb is to never use functions from C++, because iostreams are slow, and a stringstream can lead to a silent data loss if an exception is thrown during memory allocation.<p>ClickHouse has the &quot;parseDateTimeBestEffort&quot; function: <a href="https:&#x2F;&#x2F;clickhouse.com&#x2F;docs&#x2F;en&#x2F;sql-reference&#x2F;functions&#x2F;type-conversion-functions#parsedatetimebesteffort" rel="nofollow">https:&#x2F;&#x2F;clickhouse.com&#x2F;docs&#x2F;en&#x2F;sql-reference&#x2F;functions&#x2F;type-...</a> and here is its source code: <a href="https:&#x2F;&#x2F;github.com&#x2F;ClickHouse&#x2F;ClickHouse&#x2F;blob&#x2F;74d8551dadf735b97fae310f718b14598e55f8b5&#x2F;src&#x2F;IO&#x2F;parseDateTimeBestEffort.cpp">https:&#x2F;&#x2F;github.com&#x2F;ClickHouse&#x2F;ClickHouse&#x2F;blob&#x2F;74d8551dadf735...</a>
评论 #42766822 未加载
p0w3n3d4 months ago
I think that time handling is the most hard thing in the world of programming.<p>Explanation: you can learn heap sort or FFT or whatever algorithm there is and implement it. But writing your own calendar from scratch, that will do for example chron job on 3 am in the day of DST transition, that works in every TZ, is a work for many people and many months if not years...
评论 #42761647 未加载
评论 #42770576 未加载
blindriver4 months ago
I used the ICU packages when I needed to do something like this but it&#x27;s been a decade since I coded in C++.<p><a href="https:&#x2F;&#x2F;unicode-org.github.io&#x2F;icu&#x2F;userguide&#x2F;datetime&#x2F;" rel="nofollow">https:&#x2F;&#x2F;unicode-org.github.io&#x2F;icu&#x2F;userguide&#x2F;datetime&#x2F;</a>
havermeyer4 months ago
The Abseil time library makes time and date parsing and manipulation a lot nicer in C++: <a href="https:&#x2F;&#x2F;abseil.io&#x2F;docs&#x2F;cpp&#x2F;guides&#x2F;time" rel="nofollow">https:&#x2F;&#x2F;abseil.io&#x2F;docs&#x2F;cpp&#x2F;guides&#x2F;time</a>
rstuart41334 months ago
For those skimmimg the problem is mktime() returns local time, and they want it in UTC. So you need to subtract the timezone used, but the timezone varies by date you feed mktime() and there is no easy way to determime it.<p>If you are happy for the time to perhaps be wrong around the hours timezone changes, this is an easy hack:<p><pre><code> import time def time_mktime_utc(_tuple): result = time.mktime(_tuple[:-1] + (0,)) return result * 2 - time.mktime(time.gmtime(result)) </code></pre> If you are just using it for display this is usually fine as time zone changes are usually timed to happen when nobody is looking.
评论 #42765256 未加载
评论 #42762391 未加载
评论 #42772373 未加载
TZubiri4 months ago
Fun fact, http 1 used to pass expirations and dates in string format.<p>[Missing scene]<p>&quot; We are releasing Http1.1 specifications whereby expirations are passed as seconds to expire instead of dates as strings.&quot;
richrichie4 months ago
&gt; give us some truly excellent code that we really don’t deserve<p>Why such self flagellation?
TZubiri4 months ago
This comment section is so nerdy I love it.
udidnmem4 months ago
You cannot since it&#x27;s missing time zone
评论 #42759592 未加载
sylware4 months ago
Until you understand that the core of unix time is the &quot;day&quot;, in the end, you only need to know the first leap year (If I recall properly it is 1972), then you have to handle the &quot;rules&quot; of leap years, and you will be ok (wikipedia I think, don&#x27;t use google anymore since they now force javascript upon new web engines).<p>I did write such code in RISC-V assembly (for a custom command line on linux to output the statx syscall output). Then, don&#x27;t be scared, with a bit of motivation, you&#x27;ll figure it out.
评论 #42761137 未加载