-3

I am planning to write a module in C under Linux environment to read time from user defined date and time string. The user may enter time as shown below ( not limited to ). He may skip date and enter only time as well.

DD/MM/YY HH-MI-SS
DD/MM/YYYY HH/MI/SS
YYYY-MM-DD-HH-MI-SS-MSC
YYYY-DDD-HH-MI-SS
HH-MI-SS
HH/MI/SS
.......

How to offer such a facility to the user ? The user should have the flexibility to choose his expression. The module should have the ability to extract the Date and time details from that expression. In the 1st step , he should define his expression. In the second step , he should enter time as per his expression.

Soumajit
  • 95
  • 1
  • 8
  • How would you distinguish `DD/MM/YY` from `HH/MM/SS`? – Drew McGowen Aug 01 '14 at 15:48
  • 2
    and what's `01/02/03`? Jan 2nd, 2003? March 2nd, 2001? Garbage in, garbage out. – Marc B Aug 01 '14 at 15:50
  • That one is 1st February 2003. The user could be explained about the usage. They won't make such mistakes. – Soumajit Aug 01 '14 at 16:17
  • 1
    can I have some of your users please? I'd love a user who never made a mistake, even when entering dates in ISO 8601 format, let alone the number of formats you've supplied. You really should try to stick to the standard format – Tom Tanner Aug 01 '14 at 16:32
  • I meant first user should define his own format by playing with the strings. Then in the second step he should stick to his own format while entering the values. – Soumajit Aug 01 '14 at 16:37

1 Answers1

0

If you know ahead of time what the format is (because the user has told you), then the standard strftime() function can be used to handle all the options you show. You'd have to translate the notation you used into the %X notation used by strftime(), but that's mostly mechanical (except that your MM is ambiguous between month and minute).

If you don't know ahead of time what the format is, then life is a lot harder. In particular, given a single value, there's no reliable way for the code to distinguish between:

DD/MM/YY
MM/DD/YY
HH/MI/SS

Given enough sample data, you can probably distinguish between them, but you do need enough samples.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Thanks Jonathan. Sorry it was a mistake. It should be HH-MM-SS-MSC. I meant suppose user can be told the meanings of DD/MM/YY/HH/MM/SS/MSC, then the user may enter correct time. – Soumajit Aug 01 '14 at 16:30
  • Jonathan , I have slightly modified the Qt. I can use MI for Minutes and MM for month. – Soumajit Aug 01 '14 at 16:51
  • One final wrinkle: standard C versions of `strftime()` don't recognize sub-second intervals — milliseconds, microseconds, nanoseconds are all unknown to it (because it works with `struct tm` and that does not have a sub-second component). In fact, the standard C `` doesn't have a way of representing sub-second values really. The `clock()` function returns a number of ticks, but that's about it. You'd have to write some sort of wrapper to `strftime()` to handle sub-second times. – Jonathan Leffler Aug 01 '14 at 17:57