Calculate time difference (only working hours) in minutes between two dates

前端 未结 5 1132
长情又很酷
长情又很酷 2020-12-06 03:30

I need to calculate the number of \"active minutes\" for an event within a database. The start-time is well known.

The complication is that these active minutes sho

5条回答
  •  死守一世寂寞
    2020-12-06 04:02

    Globally, you'd need:

    1. A way to capture the end-time of the event (possibly through notification, or whatever started the event in the first place), and a table to record this beginning and end time.
    2. A helper table containing all the periods (start and end) to be counted. (And then you'd need some supporting code to keep this table up to date in the future)
    3. A stored procedure that will:
      • iterate over this helper table and find the 'active' periods
      • calculate the minutes within each active period.

    (Note that this assumes the event can last multiple days: is that really likely?)

    A different method would be to have a ticking clock inside the event, which checks every time whether the event should be counted at that time, and increments (in seconds or minutes) every time it discovers itself to be active during the relevant period. This would still require the helper table and would be less auditable (presumably).

提交回复
热议问题