ISO 8601 to time_t conversion in C or vice versa
Asked Answered
W

2

7

According to ISO 8601, time can be specified in different formats.

My program does not know which exact ISO 8601 format will be specified.

In this case, how can I convert it to time_t?

strptime requires you to specify format (which I do not know before hand in my case).

Ultimate goal: is to compare 2 time_t timestamps.

Edit 0: Because my goal is to compare 2 time stamps, time_t to ISO 8601 conversion will also work.

Wolcott answered 7/7, 2011 at 22:56 Comment(3)
It doesn't sound difficult to write a parser for ISO 8601 dates :)Ironwork
And write a custom parser is what I had to do when I needed this. Unfortunately, I can't post it - sorry. But like pmg said, it's not rocket science.Dariadarian
@MichaelBurr what if it's used in a rocket? Then it could be rocket science :wink:Anthracene
Q
7

If you are on a UNIX like machine, try getdate. The interface is funky but works pretty good.

Which formats are recognized is controlled by the file named by the environment variable DATEMSK. This file should contain lines of valid format strings which could be passed to strptime.

Quart answered 7/7, 2011 at 23:0 Comment(4)
Awesome. Sometimes I really wish I had more than one +1 to give.Mountford
@Mountford it's called bounty ;)Koran
@Hexa, good point -- pity it takes so long to be able to post one. I'm impressed now. :)Mountford
@Byron: Thanks much. It seems that getdate is doing the trick :)Wolcott
K
2

You say you need to compare two time_t values, ultimately. But how are you obtaining those values? If they are sent as strings, then Byron Whitlock's answer is plausible if your machine supports getdate(). If they are already time_t, then you can compare the time values. If you need to compare a time_t generated locally with a string generated remotely, then you are caught between a rock and a hard place; you have to know somehow which ISO 8601 style is used when the data is sent.

Note that ISO 8601 expects that the two systems exchanging information will agree on the notation they are going to use, and in particular that they will agree on which fields can be left out (if any) and whether punctuation will separate the fields. If you know the (single) format, or can configure your system so that for each data source, you now the ISO 8601 format that will be used, then the POSIX strptime() function can handle pretty much everything - though you need to know how your code is going to handle any undefined fields.

The title of the standard is, in full: "ISO 8601:2004 Data elements and interchange formats — Information interchange — Representation of dates and times". Its section §3.7 Mutual agreement says:

Some of the representations identified in this International Standard are only allowed by mutual agreement of the partners in information interchange. Such agreement should ensure that fields in which the representation may occur are not allowed to hold other representations that cannot be unambiguously distinguished from the agreed representation.

Kenaz answered 8/7, 2011 at 0:28 Comment(5)
One could just try a sequence of format strings with strptime until you find one that succeeds...Shippen
@R..: wince - yes, but could you not get false positives? And would you be sure everything was consumed by the conversion? With enough work, you probably could use this, but I think it is better to know what you're supposed to be dealing with, though (and the standard agrees).Kenaz
Thanks a lot, Jonathan. This is very educating. Let me work a bit more and will update here.Wolcott
@JonathanLeffler, I think you are quoting the standard out of context, ISO 8601:2004 defines three date representations using two formats, basic and extended. 3.7 refers to expanded representations and reduced accuracy. Most of the parts of the ISO 8601:2004 standard can be implemented without prior agreement and is also explicitly documented in several sections such as 4.3.2 and 4.3.2.Psychotic
@chansen: Well, since this is only one fragment of the standard, of course I'm quoting it without context. Nevertheless, I don't think it misrepresents what the standard says — or means. The standard defines a number of formats; parties can certainly agree to use those standard formats and this minimizes the risk of confusion and amount of work. This is the sensible way to go. But even that requires mutual agreement. It also recognizes that if some pair of companies have a need for a different format, they can organize it themselves, which is barely necessary to say, but it is said anyway.Kenaz

© 2022 - 2024 — McMap. All rights reserved.