CURRENT_TIMESTAMP in milliseconds

后端 未结 19 884
失恋的感觉
失恋的感觉 2020-12-07 23:48

Is there any way to get milliseconds out of a timestamp in MySql or PostgreSql (or others just out of curiosity)?

SELECT CURRENT_TI         


        
相关标签:
19条回答
  • 2020-12-08 00:24

    Easiest way I found to receive current time in milliseconds in MySql:

    SELECT (UNIX_TIMESTAMP(NOW(3)) * 1000)
    

    Since MySql 5.6.

    0 讨论(0)
  • 2020-12-08 00:28

    Do as follows for milliseconds:

    select round(date_format(CURTIME(3), "%f")/1000)
    

    You can get microseconds by the following:

    select date_format(CURTIME(6), "%f")
    
    0 讨论(0)
  • 2020-12-08 00:29

    None of these responses really solve the problem in postgreSQL, i.e :

    getting the unix timestamp of a date field in milliseconds

    I had the same issue and tested the different previous responses without satisfying result.

    Finally, I found a really simple way, probably the simplest :

    SELECT (EXTRACT (EPOCH FROM <date_column>::timestamp)::float*1000 as unix_tms
    FROM <table>
    

    namely :

    • We extract the pgSQL EPOCH, i.e. unix timestamp in floatting seconds from our column casted in timestamp prudence (in some complexe queries, pgSQL could trow an error if this cast isn't explicit. See )
    • then we cast it in float and multiply it by 1000 to get the value in milliseconds
    0 讨论(0)
  • 2020-12-08 00:30

    To get the Unix timestamp in seconds in MySQL:

    select UNIX_TIMESTAMP();
    

    Details: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp

    Not tested PostgreSQL, but according to this site it should work: http://www.raditha.com/postgres/timestamp.php

    select round( date_part( 'epoch', now() ) );
    
    0 讨论(0)
  • 2020-12-08 00:30

    The main misunderstanding in MySQL with timestamps is that MySQL by default both returns and stores timestamps without a fractional part.

    SELECT current_timestamp()  => 2018-01-18 12:05:34
    

    which can be converted to seconds timestamp as

    SELECT UNIX_TIMESTAMP(current_timestamp()) => 1516272429
    

    To add fractional part:

    SELECT current_timestamp(3) => 2018-01-18 12:05:58.983
    

    which can be converted to microseconds timestamp as

    SELECT CAST( 1000*UNIX_TIMESTAMP(current_timestamp(3)) AS UNSIGNED INTEGER) ts => 1516272274786
    

    There are few tricks with storing in tables. If your table was created like

        CREATE TABLE `ts_test_table` (
          `id` int(1) NOT NULL,
          `not_fractional_timestamp` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    

    than MySQL will NOT store fractional part within it:

        id, not_fractional_timestamp
        1,  2018-01-18 11:35:12
    

    If you want to add fractional part into your table, you need to create your table in another way:

        CREATE TABLE `ts_test_table2` (
          `id` int(1) NOT NULL,
          `some_data` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL,
          `fractional_timestamp` timestamp(3) NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    

    that leads to required result:

        id, some_data, fractional_timestamp
        1,  8,         2018-01-18 11:45:40.811
    

    current_timestamp() function is allowed to receive value up to 6, but I've found out (at least in my installed MySQL 5.7.11 version on Windows) that fraction precision 6 leads to the same constant value of 3 digits at the tail, in my case 688

        id, some_data, fractional_timestamp
        1,  2,         2018-01-18 12:01:54.167688
        2,  4,         2018-01-18 12:01:58.893688
    

    That means that really usable timestamp precision of MySQL is platform-dependent:

    • on Windows: 3
    • on Linux: 6
    0 讨论(0)
  • 2020-12-08 00:30

    Poster is asking for an integer value of MS since Epoch, not a time or S since Epoch.

    For that, you need to use NOW(3) which gives you time in fractional seconds to 3 decimal places (ie MS precision): 2020-02-13 16:30:18.236

    Then UNIX_TIMESTAMP(NOW(3)) to get the time to fractional seconds since epoc: 1581611418.236

    Finally, FLOOR(UNIX_TIMESTAMP(NOW(3))*1000) to get it to a nice round integer, for ms since epoc: 1581611418236

    Make it a MySQL Function:

    CREATE FUNCTION UNIX_MS() RETURN BIGINT DETERMINISTIC
    BEGIN
        RETURN FLOOR(UNIX_TIMESTAMP(NOW(3))*1000);
    END
    

    Now run SELECT UNIX_MS();

    Note: this was all copied by hand so if there are mistakes feel free to fix ;)

    0 讨论(0)
提交回复
热议问题