|Version||r3 master||Category||Mezzanine||Submitted by||Ladislav|
|Summary||Date and time accessor functions|
In the Stackoverflow chat I noticed that newbies are not content with accessing date and time using ordinal functions. Observing the issues I propose
1) define accessor functions as listed below in the example code
2) discontinue the ordinal access to date and time attribute values as it was already done in R3 e.g. for functions or objects
3) discontinue the numeric access to date and time attribute values as it was already done in R3 e.g. for functions or objects
4) remove all the refinements of NOW except for /PRECISE, i.e., keep just the /PRECISE refinement
5) let the WORDS-OF function return the words available for date access, time access, (eventually for some other datatypes as well)
1) (pick my-date 6), (sixth my-date), (my-date/6) look unreadable compared to (my-date/date), (pick my-date 'date) or (date-of my-date)
2) date (and time) accessors are necessary anyway e.g. in (pick now + 0:35 'hour), where no refinement of NOW can help - actually, if solving the issue using function refinements we would have to add all the refinements to every function that may yield date or time values, i.e., for example to the PICK function, since (pick my-block 1) may eventually yield a date (or time) value or to the ADD function
3) the users need to know the available accessors, which is not the case at present
year-of: func [d [date!]] [d/year] month-of: func [d [date!]] [d/month] day-of: func [d [date!]] [d/day] time-of: func [d [date!]] [d/time] zone-of: func [d [date!]] [d/zone] date-of: func [d [date!]] [d/date] weekday-of: func [d [date!]] [d/weekday] yearday-of: func [d [date!]] [d/yearday] utc-of: func [d [date!]] [d/utc] hour-of: func [t [time! date!]] [t/hour] minute-of: func [t [time! date!]] [t/minute] second-of: func [t [time! date!]] [t/second] ; this is totally unintuitive and everybody needs to look it up somewhere my-date/6 pick my-date 6
|Assigned to||n/a||Fixed in||-||Last Update||11-Mar-2013 01:43|
That is a lot of words to add to the default set, especially since PICK can do everything you want already:
pick d 'year ; year-of
pick d 'month ; month-of
pick d 'day ; day-of
pick d 'time ; time-of
pick d 'zone ; zone-of
pick d 'date ; only-date-of
pick d 'weekday ; weekday-of
pick d 'yearday ; yearday-of
pick d 'utc ; utc-of
pick d 'hour ; hour-of
pick d 'minute ; minute-of
pick d 'second ; second-of
All date and time paths can be used with PICK, and it doesn't add any additional functions that people will complain about like they do the TO-* functions. This is supposed to be a bit of a general facility that we have with PICK in R3 for non-series datatypes that use path access for their sub-parts (with a notable exception of objects, for no reason that I am aware of). However, this isn't well documented yet, and doesn't seem very consistently implemented either so it's not easily discoverable, and even once you know it's hard to know all of the accessor words.
The main advantage to your approach is that it is more easily discoverable (help "-of"), and that you can assign these functions to other words to use as accessors (just like the TO-* functions and the ordinals). I don't think that dumping functions into the top-level namespace just to make what they do more discoverable by using HELP as a crutch is a sustainable strategy though.
I think that it might work better to make the PICK approach actually workable:
- Document that you can use PICK for this.
- Remind people of PICK every time they ask for functions like these.
- Make PICK act like this more consistently like this (objects!), so people are more likely to try it without asking.
- Have the WORDS-OF reflector return the words that you can use for PICK or path access in situations like this (unbound, of course).
Nice naming for your functions though, if these exist they would be good *-OF functions :)
"Have the WORDS-OF reflector return the words that you can use for PICK or path access in situations like this (unbound, of course)."
A big +1 on that suggestion in particular. Discoverability of these "accessor words" currently strikes me as really bad.
Discovery is important. When the ROUND func was being designed, there was much discussion about having a single func, with refinements, versus multiple funcs (TRUNC, CEIL, etc.). I think the refinement model is a key part of REBOL's design, though it can use abused. NOW has a lot of refinements already, and I only use a few consistently (now/date and now/time being the most used I think). I use more refinements on date! and time! values, to extract units. And while I think reflectors are valuable, as is the goal of referential transparency, like Nick, I find refinements very natural in most cases. The WORDS-OF idea may help, but that's kind of how GET-MODES works in R2, which I don't find as obvious or easy to use.
What if we keep /PRECISE, and perhaps /UTC, and remove the rest, replacing them with a single /PART refinement that takes a word or block. And add a DATE-PART function to match how NOW works. e.g.
date-part: func [ "Return one or more unit parts of a date!" date [date!] part [word! block!] "One or more of the following: date month year day time hour minute second weekday yearday utc zone" /local res ][ res: collect [ foreach word compose [(part)] [ keep pick date word ] ] either block? part [res] [pick res 1] ]
If we really want to simplify NOW, we could leave out /PART and just say to use DATE-PART for that functionality.
Like Brian, I don't like much the list of *-of words it would add to the language, for so few utility.
'now has the same refinments as a date! value + /precise and /utc. For me it's not a problem. Actually, it helps found the refinment of a date! value. So...
I agree on the WORDS-Of proposal. And if "WORDS-OF a-datatype" returns the words availables for this kind of datatype, it would be even better for discovery! ie:
It could even be used to enhance 'help output.
|DideC, you can use /utc with date values too. NOW just adds /precise, but it is missing /hour, /minute and /second (we need another ticket for that).|
/utc: I was confused by R2.
I like the 'pick proposal as it's very easy for those who want to make the *-of functions then :-)
|28-Feb-2013 18:22||DideC||Comment : 0003541||Added||-|
|25-Feb-2013 21:33||BrianH||Comment : 0003491||Modified||-|
|25-Feb-2013 21:32||BrianH||Comment : 0003500||Added||-|
|25-Feb-2013 15:10||didec||Comment : 0003499||Modified||-|
|25-Feb-2013 15:09||didec||Comment : 0003499||Modified||-|
|25-Feb-2013 15:08||didec||Comment : 0003499||Added||-|
|24-Feb-2013 22:43||Gregg||Comment : 0003498||Modified||-|
|24-Feb-2013 22:42||Gregg||Comment : 0003498||Added||-|
|24-Feb-2013 18:59||abolka||Comment : 0003492||Modified||-|
|24-Feb-2013 18:58||abolka||Comment : 0003492||Added||-|
|24-Feb-2013 18:39||BrianH||Comment : 0003491||Added||-|