2012-09-18 23 views
0

我正在研究一个应用程序,该程序将跟踪用户的工作时间,然后从此数据生成图形...
考虑到他们可以在今天开始完成任何任务并完成它明天,我不能保存一个Date ... 现在,我保存整数字段,如hours和整数minute ...
有没有一种方法(一些宝石,也许)来帮助我处理这种问题?
还是其他方法?
感谢处理“加工时间”

我班Atendimento包含所有必要的数据

class Atendimento < ActiveRecord::Base 
    attr_accessible :horas, :minutos, :empresa, :tipo_atendimento, :observacao, :empresa_id, :tipoatendimento_id 

    belongs_to :empresa 
    belongs_to :tipo_atendimento, :foreign_key => 'tipoatendimento_id' 
    belongs_to :usuario 

    validates :horas, :minutos, presence: true 
end 

回答

0

我节省了工作时间为秒:)

1

我想补充两个日期时间列到你的数据库来跟踪的启动和停止时的时间戳。然后你可以减去这两个值来获得时间。

+0

我不是很清楚......我不能这样做,因为当只有5个留下来结束分钟,用户可以启动任务的工作时间和另一天的任务继续......他甚至可以在今天开始任务,并在一周内完成任务 –

1

而不是三个字段,你应该添加开始和结束时间作为时间戳。您可以将这些行添加到迁移文件中:

add_column :tasks, :started_at, :timestamp 
add_column :tasks, :ended_at, :timestamp 

在模型中,您可以使用这些时间戳进行计算。

class Task < ActiveRecord::Base 
    attr_accessible :started_at, :ended_at 
    validates :started_at, presence: true 
    validates :ended_at, presence: true 

    # Returns the duration of the task in seconds 
    def duration 
    ended_at - started_at 
    end 
end 
+0

请参阅我对@ Beerlington的回答 –

+0

@LuizE的评论。好的。我仍然会像这样将开始和结束时间戳记录到数据库中。那么你只需要为持续时间函数提出更复杂的逻辑。如果任务可以在周末结束,您可能还想考虑周末和假期。也许有人可以设计一些伪代码? (我甚至可以稍后再做)。 –

+0

@LuizE。我建议你看看这个问题:http://stackoverflow.com/questions/3708881/counting-regular-working-days-in-a-given-period-of-time –