Live data from Hacker News

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

berthub.eu

11–20 of 106 posts

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

#12
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?

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

#13
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.

ClickHouse has the "parseDateTimeBestEffort" function: https://clickhouse.com/docs/en/sql-reference/functions/type-... and here is its source code: https://github.com/ClickHouse/ClickHouse/blob/74d8551dadf735...

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

#14

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 r…

What's a man page? [cit]

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

#16

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 r…

It 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/ (Time, Clock, and Calendar Programming In C by Eric S. Raymond)

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

#17
post #14

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 r…

What's a man page? [cit]

"manual pages", type "man man" in your terminal.

https://man7.org/linux/man-pages/man1/man.1.html

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

#18
post #4

Until you understand that the core of unix time is the "day", 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 "rules" of leap years, and you will be ok (wikipedia I think, don't use google anymore since they now force javascript upon new web engines). I did write such code in RISC-V assembly (for a custom command line on linux to output the statx sy…

The core of the UNIX time is seconds since epoch, nothing else. 'Day' has no special place at all. There are calendars for converting to and from dates, including Western-style, but the days in those calendars vary in length because of daylight saving switches and leap seconds for example.

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

#19
post #3

Earlier quoted context omitted.

UTC is a timezone, though. Or am I misunderstanding what you're saying?

UTC would be marked as +Z Without any marking, it could be anything

No '+'.

Noon UTC is "12:00Z".

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

#20
post #15
post #9

[flagged]

And the fox said: "These grapes are sour".

That makes no sense in this context. What's the situation you're imagining where they wanted to use C but something else prevented them so they made up an excuse to call C bad?

You know sometimes people just dislike things, right?

Post reply on HN