tags:

views:

44

answers:

1

I would like to distinguish date at midnight (i.e. '12/05/2010 00:00:00') from date without time specified (i.e. '12/05/2010').
This value is provided by user, sometimes it is only date which is important, sometimes it is date and time and later the application is expected to show the time only when it was explicitly provided. So for the following inputs I would expect the following outputs:

  • for '12/05/2010' I would expect '12/05/2010'
  • for '12/05/2010 09:23' I would expect '12/05/2010 09:23'
  • for '12/05/2010 00:00' I would expect '12/05/2010 00:00'

I know I can model it in two separate columns, date and time, but I was wondering was there any way of handling this in single date column?

+1  A: 

It is possible to indicate that a date was supplied without the time component by negating the year to store that fact.

  User supplied value       Inserted value

  12/05/2010                12/05/2010 BC
  12/05/2010 09:23          12/05/2010 09:23
  2/05/2010 00:00           2/05/2010 00:00

When selecting the date value if the year is a BC year then the user omitted the time component.

Having said that, my actual advice is to use a dedicated column to save a flag regarding the presence of the time component or not. I would not use this negation technique myself.

Janek Bogucki
+1 An extra flag column, for example indication_time_inserted, with possible values 'Y' and 'N', is the way to go.
Rob van Wijk
@Janek: I'm not following this. Can you please explain what you mean by "by negating the year to store that fact"?
Charles
This is an interesting, albeit ill-advised, use of magic values :)
Jeffrey Kemp
@Charles When the time component was not supplied by the user and the year was 2010AD store 2010BC instead. Do not use this technique.
Janek Bogucki
@Janek. I see. I suppose another (equally dodgy) option would be to use a magic value for the seconds part - as long as the application never provides anything other than zero for seconds. So 00:00:00 means the application specified midnight, but 00:00:33 means no time was specified. There was a time when this sort of technique was considered completely acceptable. :)
Charles
Nice idea (BC/AD), but I agree it is not something I would like to use. Potentially I can spot leap year calculation problem as well, 29th February may be a correct date in 2012 but maybe not in -2012, not sure ;)
Swiety