【问题标题】:red/rebol: Subtracting dates returns days, how can I change that?red/rebol:减去日期返回天数,我该如何更改?
【发布时间】:2016-10-03 16:08:01
【问题描述】:

使用日期类型,我想知道为什么减法总是返回天数,我怎样才能让它返回分钟(或秒等),这在第二个示例中是可能的

>> 24-dec-2016 - now
== 82
>> 24-dec-2016/0:00 - now
== 82

这只是任意的还是我可以影响返回的内容?我尝试了一些改进,但希望能推动一个方向,即 rebol/red 方式。

也许在这个问题下面还有一个更重要的东西:减法返回的“规则”是什么、常识、某种讨论和协议,还是只是由谁来执行? (例如减法的结果:1.1.1.1 - 1, 200x200 - 100, ...)

【问题讨论】:

    标签: date types rebol red


    【解决方案1】:

    你可以使用difference:

    difference 24-dec-2016 now
    == 1952:06:01
    

    要获取特定部分,请使用路径语法:

    time-diff: difference 24-dec-2016 now
    time-diff/2
    

    给出分钟(第二部分)

    == 1951:42:11
    == 42
    

    【讨论】:

    • 进一步,你可以对整数做 ! time-diff 获取秒数。
    【解决方案2】:

    ✓ 查看Francois Vanzeveren's Date-Time Script on REBOL.org

    如果你加载它,即,

    do http://www.rebol.org/download-a-script.r?script-name=date-time.r
    

    那么你可以这样做:

    >> ? date-dif
    USAGE:
      DATE-DIF date1 date2 /y /m /d /ym /md /yd
    
    DESCRIPTION:
    
      Returns the difference between two dates.
    
       DATE-DIF is a function value.
    
    ARGUMENTS:
       date1 -- (Type: date)
       date2 -- (Type: date)
    REFINEMENTS:
       /y -- Returns the number of complete years between @date1
    and @date2.
       /m -- Returns the number of complete months between @date
    1 and @date2.
       /d -- Returns the number of complete days between @date1
    and @date2.
       /ym -- Returns the number of full months between @date1 a
    nd @date2,
          not including the difference in years.
       /md -- Returns the number of full days between @date1 and
    @date2,
          not including the difference in months.
       /yd -- Returns the number of full days between @date1 and
    @date2,
          not including the difference in years.
    

    >> ? now 查看日期的/改进

    >> ? now
    USAGE:
        NOW /year /month /day /time /zone /date /weekday /yearday
    /precise
    
    DESCRIPTION:
         Returns the current local date and time.
         NOW is a native value.
    
    REFINEMENTS:
         /year -- Returns the year only.
         /month -- Returns the month only.
         /day -- Returns the day of the month only.
         /time -- Returns the time only.
         /zone -- Returns the time zone offset from GMT only.
         /date -- Returns date only.
         /weekday -- Returns day of the week as integer (Monday is
     day 1).
         /yearday -- Returns day of the year (Julian)
         /precise -- Use nanosecond precision
    

    例子:

    >> d: 27-7-1973
    == 27-Jul-1973
    >> d/day
    == 27
    >> d/month
    == 7
    >> d/year
    == 1973
    

    【讨论】:

      猜你喜欢
      • 2022-11-04
      • 1970-01-01
      • 2019-11-29
      • 1970-01-01
      • 1970-01-01
      • 2020-12-23
      • 2019-01-07
      • 2010-10-01
      • 2011-01-16
      相关资源
      最近更新 更多