3

Should I be storing a Delphi TDateTIme, or perhaps convert to Unix timestamp first? Or mayeb eeven as a string?

How should I declare the column in MySql? As a Double, or DateTime (or Integer if I use Unix timestamp)?

Whta is "correct", or what is easiest if I want to be able to display a string with "yyyy mm dd hh:mm:ss" (or similar) and also to be able to get an elapsed time from comparing two values?

Btw, the program will only ever be used in one tiemzone - which does not have daylight savings time.

I am confused and can't seem to find this discussed anywhere. Any helpful URLs?

Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551

6 Answers6

9
  1. Should I be storing a Delphi TDateTIme, or perhaps convert to Unix timestamp first?

    Typically, you should do neither: the data types in the database layer ought to be meaningful (as possible) on their own and not depend on your application to interpret them. As @Jim DeLaHunt says, this enables the database to easily manipulate/interpret them from SQL as required (and also enables you to easily access the same data from another application codebase in the future).

    MySQL has five temporal types, only two of which store both a date and a time: DATETIME and TIMESTAMP.

    As others have alluded, the difference comes down to whether you wish to store the timezone - although I find that quite a confusing way of looking at it:

    • TIMESTAMP uses the session's time_zone variable to convert input into a UTC timestamp and then back again for output: it's useful for specifying an exact moment in time;

    • DATETIME simply stores the date and time without regard to timezone, much like taking a photograph of a calendar and clock: it's useful for specifying an event that occurs in the same local time globally.

  2. How should I declare the column in MySql? As a Double, or DateTime (or Integer if I use Unix timestamp)?

    Just as you would declare any other column, you specify the relevant data type after the column name.

    Beware that TIMESTAMP has additional features, such as automatic update, which you may wish to disable in your column declaration if so desired.

  3. Whta is "correct", or what is easiest if I want to be able to display a string with "yyyy mm dd hh:mm:ss" (or similar) and also to be able to get an elapsed time from comparing two values?

    Using one of the above temporal types, you will be able to do all of this (using date functions as required). The default output of TIMESTAMP and DATETIME types is a string in 'YYYY-MM-DD HH:MM:SS' format.

    In particular, the "elapsed time" from comparing two values could for example be obtained with MySQL's TIMEDIFF() function:

    SELECT TIMEDIFF(end, start) AS elapsed
    FROM   my_table
    WHERE  ...
    
Community
  • 1
  • 1
eggyal
  • 122,705
  • 18
  • 212
  • 237
4

MySQL should have a native date format which is to be preferred as this allows one to use the SQL date functions (year, month, etc).

I have in one of my database tables a field which is defined as a Delphi datetime - in retrospect this was a mistake as it is very difficult to filter on this field or display the values. Both these actions require special handling in the calling program as opposed to in the database.

No'am Newman
  • 6,395
  • 5
  • 38
  • 50
2

I'd let MySQL handle date/times as it wants to, i.e. as a datetime field. It has a huge range (not epoch limited).

See here : http://dev.mysql.com/doc/refman/5.0/en/datetime.html

FreudianSlip
  • 2,870
  • 25
  • 24
1

This link might be useful to you. It depends whether you want to use timezones, but even you don't need them now, reconsider if it is not possible to use them in the future.

I would use DATETIME. It is easier to read by human.

Kalicz
  • 346
  • 3
  • 13
1

You can read all about the MySQL Date and Time types in the MySQL Reference Manual, 11.3. Date and Time Types and about functions in 12.7. Date and Time Functions.

My experience, with MySQL and SQL Server, though not with Delphi per se, is that you are well served to store date and time values in the database's date and time format. This lets you use the database's date and time functions, and take advantage of its optimised implementation of date and time features. If you store datetime values as something generic like a Double or an Integer Unix timestamp, you'll lack the optimisations and have to implement your own equivalent of date and time functions.

Update based on OP's edited question:

The DateFormat() function can give you strings like "yyyy mm dd hh:mm:ss" using a format string like "%Y %m %d %k:%i:%s".

The TimeDiff() function will give you a time interval type giving the difference between two datetime values.

Jim DeLaHunt
  • 10,960
  • 3
  • 45
  • 74
0

There are a number of post describing the best way to save the datetime in mysql such as

MySQL Integer vs DateTime index and MySQL DATETIME vs TIMESTAMP vs INT performance and benchmarking with MyISAM

If you decided convert your datetime into a Unix timestamp and save it as a integer

Look out for the following:

  • The Delphi function DateTimeToUnix( StrToDateTime('2013-11-22 16:34:45'));
    Uses the LOCAL TIME ZONE for the conversation

  • The MYSQL function UNIX_TIMESTAMP('2013-11-22 16:34:45') Uses the GMT TIME ZONE for the conversation

Community
  • 1
  • 1
Charles Faiga
  • 11,665
  • 25
  • 102
  • 139