5 ms·
Is it a struggle though? They needed to have a locale matching the language of the localised time string they wanted to parse, they needed to use strptime to p
by chikere232 2y ago
Is it a struggle though?
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.
The interface or these things could certainly be nicer, but most of the things they bring up as issues aren't even relevant for the task they're trying to do. Why do they talk about daylight savings time being confusing when they're only trying to deal with UTC which doesn't have it?
- pif 2y agoWhat's a man page? [cit]
- johnisgood 2y ago"manual pages", type "man man" in your terminal. https://man7.org/linux/man-pages/man1/man.1.html https://man7.org/linux/man-pages/man1/man.1.html
- TZubiri 2y agoNever type up man man, it might make the internet implpode.
- cstrahan 2y ago[dead]
- amelius 2y agoIt's where people went for programming information before ChatGPT and even before StackOverflow.
- werdnapk 2y agoIt's where people went for information "even before" the internet.
- pif 2y agoI'm sorry the sarcasm was not evident. I learnt to program when men were men, and man was man.
- johnisgood 2y agoIt is not. int main(void) { struct tm tm = {0}; const char *time_str = "Mon, 20 Jan 2025 06:07:07 GMT"; const char *fmt = "%a, %d %b %Y %H:%M:%S GMT"; // Parse the time string if (strptime(time_str, fmt, &tm) == NULL) { fprintf(stderr, "Error parsing time\n"); return 1; } // Convert to Unix timestamp (UTC) time_t timestamp = timegm(&tm); if (timestamp == -1) { fprintf(stderr, "Error converting to timestamp\n"); return 1; } printf("Unix timestamp: %ld\n", timestamp); return 0; } It is a C99 code snippet that parses the UTC time string and safely converts it to a Unix timestamp and it follows best practices from the SEI CERT C standard, avoiding locale and timezone issues by using UTC and timegm(). You can avoids pitfalls of mktime() by using timegm() which directly works with UTC time. Where is the struggle? Am I misunderstanding it? Oh by the way, must read: https://www.catb.org/esr/time-programming/ https://www.catb.org/esr/time-programming/ (Time, Clock, and Calendar Programming In C by Eric S. Raymond)
- paxcoder 2y agoI can't find `timegm` neither in the C99 standard draft nor in POSIX.1-2024. The first sentence of your link reads: >The C/Unix time- and date-handling API is a confusing jungle full of the corpses of failed experiments and various other traps for the unwary, many of them resulting from design decisions that may have been defensible when the originals were written but appear at best puzzling today.
- chikere232 2y agohttps://man7.org/linux/man-pages/man3/timegm.3.html https://man7.org/linux/man-pages/man3/timegm.3.html It's not posix, but it's pretty available
- johnisgood 2y agoYeah, you're correct that `timegm` is neither part of the C99 standard nor officially specified in POSIX.1-2024 but it is widely supported in practice on many platforms, including glibc, musl, and BSD systems which makes it a pragmatic choice in environments where it is available. Additionally, it is easy to implement it in a portable way when unavailable. So, while `timegm` is not standardized in C99 or POSIX, it is a practical solution in most real-world environments, and alternatives exist for portability, and thus: handling time in C is not inherently a struggle. As for the link, it says "You may want to bite the bullet and use timegm(3), even though it’s nominally not portable.", but see what I wrote above.
- deleted 2y ago[deleted]
- michaelt 2y ago> Is it a struggle though? It’s twelve lines or more, if you include the imports and error handling. Spreadsheets and SQL will coerce a string to a date without even being asked to. You might want something more structured than that, but you should be able to do it in far less than 12 lines. C has many clunky elements like this, which makes working with it like pulling teeth.
- sitzkrieg 2y agoalmost like C is logically operating at a lower level than spreadsheets or SQL or something
- oguz-ismail 2y ago> you should be able to do it in far less than 12 lines In C++, maybe. In C, not necessarily. If you're not willing to reinvent the wheel why'd you choose C anyway?
- Suppafly 2y ago>Spreadsheets and SQL will coerce a string to a date without even being asked to. But only when you don't want them to, when you do want them to do it it's still a pain.
- stonogo 2y agoSpreadsheets and SQL will coerce a string to a date because someone programmed them to in C or C++.