Surpassing MySQL's TIME value limit of 838:59:59

前端 未结 6 543
南旧
南旧 2020-12-01 14:54

The title might be a bit confusing so allow me to explain. I\'m using a table to record my work logs. Every day I\'ll create an entry stating from what time to what time I h

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 15:31

    Counting the days separately is enough.
    Here's the concatenation I used.

    I illustrated with a fully copy/pastable example to ease the understanding of the limit we hit (TIME format's max value)
    A free unicorn is bundled to simplify comma management

    SELECT 'pq7~' AS unicorn
    #######################
    ##  Expected result  ##
    #######################
    ## Total, formatted as days:hh:mm:ss ##
      ,CONCAT(
        FLOOR(TIMESTAMPDIFF(SECOND, '2017-01-01 09:17:45', '2017-03-07 17:06:24') / 86400)
        , ':'
        , SEC_TO_TIME(TIMESTAMPDIFF(SECOND, '2017-01-01 09:17:45', '2017-03-07 17:06:24') % 86400)
      ) AS Real_expected_result
    

    #########################
    ## Calculation details ##
    #########################
    ## Extracted days from diff ##
      ,FLOOR(TIMESTAMPDIFF(SECOND, '2017-01-01 09:17:45', '2017-03-07 17:06:24') / 86400) AS Real_days
    ## Extracted Hours/minutes/seconds from diff ##
      ,SEC_TO_TIME(TIMESTAMPDIFF(SECOND, '2017-01-01 09:17:45', '2017-03-07 17:06:24') % 86400) AS Real_hours_minutes_seconds
    

    ###################################
    ## Demo of wrong values returned ##
    ###################################
      ,TIMESTAMPDIFF(SECOND, '2017-01-01 09:17:45', '2017-03-07 17:06:24') AS Real_seconds_diff
    
    ## WRONG value returned. 5.64M is truncated to 3.02 ! ##
      ,TIME_TO_SEC(SEC_TO_TIME(5644119)) AS WRONG_result
    
    ## Result is effectively limited to 838h59m59s ##
      ,SEC_TO_TIME(TIMESTAMPDIFF(SECOND, '2017-01-01 09:17:45', '2017-03-07 17:06:24')) AS Limit_hit
    
    ## Lights on said limit ##
      ,SEC_TO_TIME( 3020398) AS Limit_value_check1
      ,SEC_TO_TIME( 3020400) AS Limit_value_check2
    

提交回复
热议问题