加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 在任意时间范围内查找最佳日/月/年间隔的算法?

发布时间:2020-12-17 02:11:57 所属栏目:百科 来源:网络整理
导读:如果您有时间表,请说: 2009年3月19日 – 2011年7月15日 是否有一种算法可以将时间框架分解为: March 19,2009 - March 31,2009 # complete daysApril 1,2009 - December 31,2009 # complete monthsJanuary 1,2010 - December 31,2010 # complete yearsJanua
如果您有时间表,请说:

2009年3月19日 – 2011年7月15日

是否有一种算法可以将时间框架分解为:

March 19,2009  - March 31,2009    # complete days
April 1,2009   - December 31,2009 # complete months
January 1,2010 - December 31,2010 # complete years
January 1,2011 - June 30,2011     # complete months
July 1,2011    - July 15,2011     # complete days

更具体地说,给定任意时间帧,甚至到第二个时间帧,是否有一种算法可以将其划分为最大数量的任意大小的间隔?

因此,可能不是将上述日期范围划分为几天/几个月/几年,而是将其划分为5天和18个月的块,这样就是随机的.这种算法有正式的名称吗?一个ruby的例子很棒,但任何语言都可以.

我已经能够破解一些硬编码的Ruby来处理日/月/年的例子:

> https://gist.github.com/1165914

…但似乎应该有一个算法来抽象它来处理任何间隔细分.也许它只是归结为简单的数学.

解决方法

这可能是作弊,但您可以使用active_support提供的日期函数大大简化代码.下面是我使用active_support提出的一些代码.算法非常简单.弄清楚第一个月的最后一天,弄清楚上个月的第一天.然后打印第一个月,将中间分成几年并打印出来,然后打印上个月.当然,由于边缘情况,这种简单算法在许多方面都有所下降.以下算法尝试优雅地处理每个边缘情况.我希望它有所帮助.

require 'active_support/all'

# Set the start date and end date
start_date = Date.parse("March 19,2009")
end_date = Date.parse("July 15,2011")

if end_date < start_date
  # end date is before start date,we are done
elsif end_date == start_date
  # end date is the same as start date,print it and we are done
  print start_date.strftime("%B %e,%Y")
elsif start_date.year == end_date.year && start_date.month == end_date.month
  # start date and end date are in the same month,print the dates and we
  # are done
  print start_date.strftime("%B %e,%Y")," - ",end_date.strftime("%B %e,"n"
else
  # start date and end date are in different months
  first_day_of_next_month = Date.new((start_date + 1.month).year,(start_date + 1.month).month,1);
  first_day_of_end_month = Date.new(end_date.year,end_date.month,1);

  # print out the dates of the first month
  if (first_day_of_next_month - 1.day) == start_date
    print start_date.strftime("%B %e,"n"
  else
    print start_date.strftime("%B %e,(first_day_of_next_month - 1.day).strftime("%B %e,"n"
  end

  # now print the inbetween dates
  if first_day_of_next_month.year == (first_day_of_end_month - 1.day).year &&
    (first_day_of_end_month - 1.day) > first_day_of_next_month
    # start date and end date are in the same year,just print the inbetween
    # dates 
    print first_day_of_next_month.strftime("%B %e,(first_day_of_end_month - 1.day).strftime("%B %e,%Y") + "n"
  elsif first_day_of_next_month.year < (first_day_of_end_month - 1.day).year
    # start date and end date are in different years so we need to split across
    # years
    year_iter = first_day_of_next_month.year

    # print out the dates from the day after the first month to the end of the
    # year
    print first_day_of_next_month.strftime("%B %e,Date.new(first_day_of_next_month.year,12,31).strftime("%B %e,"n"
    year_iter += 1

    # print out the full intermediate years
    while year_iter < end_date.year
      print Date.new(year_iter,1,1).strftime("%B %e,Date.new(year_iter,"n"
      year_iter += 1
    end

    # print from the begining of the last year until the last day before the the
    # end month
    print Date.new(first_day_of_end_month.year,"n"
  end

  # finally print out the days of the last month
  if first_day_of_end_month == end_date
    print end_date.strftime("%B %e,"n"
  else
    print first_day_of_end_month.strftime("%B %e,"n"
  end
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读