You shouldn’t really care
Does MySQL store TIMESTAMP as a unix timestamp in seconds, e.g. 1541645714
Or is it actually stored in the ISO 8601 format 2018-11-08T02:55:14+00:00
You don't care. The internal representation is an implementation detail unrelated to your work. And that implementation could change in future versions.
What a database accepts for inputs or generates for outputs is not directly related to its internal representation.
All you should care about is the documented behavior, the SQL standard, and the delta between them.
In the case of date-time types, the SQL standard barely touches the subject, defining a few types and defining very little behavior. So the capabilities of databases vary widely.
Excerpts from MySQL documentation:
The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval.
fractional seconds part in up to microseconds (6 digits) precision … is discarded
Y2038 Problem
If you are curious about the implementation, look at the source code.
But without even looking at source, after reading this documentation we have two clues about the TIMESTAMP
type:
- Any fractional second dropped.
- The upper limit is 2038-01-19T03:14:07Z.
So we know the type is likely stored as a 32-bit count of whole seconds since the epoch reference date used commonly in Unix and some other systems: the first moment of 1970 in UTC, 1970-01-01T00:00Z. Such a 32-bit count will reach its maximum on that date, in a phenomenon known in the industry as the Y2038 Problem.
If date-time handling is important to your project, you might consider using a database with more robust date-time handling, such as Postgres.
Smart objects, not dumb strings
You should be using the date-time types within your programming language to exchange date-time values with your database. Avoid communicating with mere strings.
Leading the industry in this regard is the java.time framework in Java 8 and later, with a back-port available for Java 6 & 7.
OffsetDateTime odt = OffsetDateTime.now( ZoneOffset.UTC ) ;
Strin sql = "SELECT * FROM event_ WHERE when_ >= ? ;" ;
…
myPreparedStatement.setObject( 1 , odt ) ;
Retrieval.
OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ; // In Postgres and some other databases, the offset-from-UTC will be zero, for UTC itself.
ZonedDateTime zdt = odt.atZoneSameInstant( ZoneId.of( "America/Montreal" ) ) ; // Adjust from UTC into a time zone. Same moment, same point on the timeline, different wall-clock time.
And proving my point… Amadan on other Answer points out this documentation I presented above is now outdated, from MySQL 5.5. The implementation did change radically in Version 8.