Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 复杂的时间转换和阶段划分_Ruby On Rails_Date_Methods_Model_Calculation - Fatal编程技术网

Ruby on rails 复杂的时间转换和阶段划分

Ruby on rails 复杂的时间转换和阶段划分,ruby-on-rails,date,methods,model,calculation,Ruby On Rails,Date,Methods,Model,Calculation,我目前正在开发一个应用程序,用户可以为他们的硕士论文创建论文计划。通过这样做,用户正在创建一个属于用户的论文,该用户通过过滤器依次创建论文 论文有开始和结束日期。每个论文阶段都有一个开始日期和一个结束日期,它最终导致一个持续时间,并根据百分比值进行设置。例如,第一阶段是“寻找主题”阶段,需要整个时间跨度的13%(基于我客户的经验值)。如果论文开始于1月1日(01-01-2021),结束于4月1日(最终提交截止日期),则第一阶段将占用该时间的13%,并根据该持续时间计算结束日期 在我的应用程序中,

我目前正在开发一个应用程序,用户可以为他们的硕士论文创建论文计划。通过这样做,用户正在创建一个属于用户的
论文
,该用户通过过滤器依次创建
论文

论文
有开始和结束日期。每个论文阶段都有一个开始日期和一个结束日期,它最终导致一个持续时间,并根据百分比值进行设置。例如,第一阶段是“寻找主题”阶段,需要整个时间跨度的13%(基于我客户的经验值)。如果论文开始于1月1日(01-01-2021),结束于4月1日(最终提交截止日期),则第一阶段将占用该时间的13%,并根据该持续时间计算结束日期

在我的应用程序中,我有一个进度条,它以百分比的形式向用户显示基于上述地址行为的进度

现在我有一个错误,在某些日期,即使最后期限是在接下来的两天内,有时进度条显示,论文只完成了46%(随机数,因为它与当前日期一起工作,我无法跟踪)。因此,进度条百分比并非在所有情况下都准确

我的方法是将天(用户只能输入日期而不能输入时间)转换为小时,因此我可以使用更详细的时间跨度:

thesis.rb(属于为提高可读性而删除的作业)

课堂论文=Date.today
0
elsif Date.parse(“#{self.start_Date}”)=Date.today
((100/时间跨度整天)。到整天)*天一天#以天为单位的输出
elsif Date.parse(“#{self.end_Date}”)
class Thesis < ApplicationRecord
  before_create :assign_phases

  # The problem when working with time is the conversion from days to hours, since rails is not
  # capable of doing so on its own. thats why there are some extra vars like timespan_whole_days which is 
  # converting the timespan_whole (measured in hours for a more precise date creation in assign_phases) to
  # days later.


  def timespan_whole
    startdate = DateTime.parse("#{self.start_date}")
    enddate = DateTime.parse("#{self.end_date}")

    ((enddate - startdate).to_i.days) / 1.hour # output in hours
  end


  def assign_phases
    if self.new_record?
    # Initialization
      percentile = (self.timespan_whole / 100).round(6).hours #percentile has been twentypercent before. 100 has been 5 ###################### was round(2)

      self.thesisphases.new(order: 1, start_date: self.start_date)
      self.thesisphases.new(order: 2, start_date: (self.start_date + (percentile * 13)))
      self.thesisphases.new(order: 3, start_date: (self.start_date + (percentile * 33)))
      self.thesisphases.new(order: 4, start_date: (self.start_date + (percentile * 63)))
      self.thesisphases.new(order: 5, start_date: (self.start_date + (percentile * 78)))
      self.thesisphases.new(order: 6, start_date: (self.start_date + (percentile * 98)))   

    else
    end

  end

  def daysgone
    startdate = Date.parse("#{self.start_date}")
    enddate = Date.parse("#{self.end_date}")

    ((enddate - startdate) - ((enddate - startdate) - (Date.today - startdate))).to_f ###################### was .to_i
  end


  def progress_percentage
    timespan_whole_days = timespan_whole / 24 #converting hours in days
  
    if Date.parse("#{self.start_date}") >= Date.today
      0
    elsif Date.parse("#{self.start_date}") <= Date.today && Date.parse("#{self.end_date}") >= Date.today
      ((100 / timespan_whole_days).to_f ) * daysgone # output in days
    elsif Date.parse("#{self.end_date}") <= Date.today
      100
    end

  end

end