Red / rebol: subtracting dates returns days, how can I change this?

When playing with a date type, I was wondering why subtraction always returns days, how can I make it return minutes (or seconds, etc.), which may be possible in the second example

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

- is it just arbitrary or can I influence what comes back? I tried a little with refinement, but would be grateful for pushing in the direction, rebole / red path.

Perhaps there is a more significant thing under this question: what is the “rule” of what returns subtraction, common sense, some discussion and agreement, or is it determined only by those who implement it? (e.g., the result of subtraction: 1.1.1.1 - 1, 200x200 - 100...)

+4
source share
2 answers

You can use difference:

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

To get specific details, use the path syntax:

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

Gives minutes (second component)

== 1951:42:11
== 42
+3
source

✓ Check out Francois Vanseveren's Date-Time Script at REBOL.org

If you download it, i.e.

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

You can do it:

>> ? 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.

>> ? nowTo see / refine for dates

>> ? 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

Example:

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

Source: https://habr.com/ru/post/1656601/


All Articles