Date format: Difference between revisions

Undid revision 312192, which was submitted citing claims that appear to be unsubstantiated and did not demonstrably improve what it replaced. The code being reverted to is verified to be working and reliable on MacOS versions Yosemite to Big Sur.
(→‎{{header|Terraform}}: Add implementation.)
(Undid revision 312192, which was submitted citing claims that appear to be unsubstantiated and did not demonstrably improve what it replaced. The code being reverted to is verified to be working and reliable on MacOS versions Yosemite to Big Sur.)
Line 471:
 
=={{header|AppleScript}}==
AppleScript <pre>date</pre> objects are very simple to manipulate, provided one is familiar with the slightly unintuitive nature of how they should be handled in order to be robust and produce the desired result across all systems. Despite the official documentation claiming otherwise, AppleScript is not able to parse date strings that don’t strictly adhere to the date and time format strings specified in System Preferences—thus it is a setting that is specific to one’s system, and won’t naturally port.
<lang applescript>set {year:y, month:m, day:d, weekday:w} to (current date)
 
AppleScript provides properties and classes specifically for manipulating or constructing date objects that are not dependent on anything outwith the AppleScript language. In most cases, the <lang applescript>current date</lang> command provides a starting point, from which one gets current or sets new values for its <pre>day</pre>, <pre>month</pre>, <pre>year</pre>, and <pre>time</pre> properties and property classes to determine or affect the date a particular date object refers to.
tell (y * 10000 + m * 100 + d) as text to set shortFormat to text 1 thru 4 & "-" & text 5 thru 6 & "-" & text 7 thru 8
set longFormat to (w as text) & (", " & m) & (space & d) & (", " & y)
 
Formatting is usually achieved by extracting individual date components, performing text manipulations (e.g. to prepend leading zeroes) on each of these, then concatenating these for the final result. The <pre>«class isot»</pre> class doesn’t have a vernacular form and can only be accessed using its raw chevron syntactic form, which others may recognise from similar AppleScript classes such as <pre>«class furl»</pre>, <pre>«class utf8»</pre> and so forth. They can provide some useful—and, often, more reliable—features that can save the headache suffered by many when learning how the <pre>file</pre>, <pre>POSIX file</pre>, and <pre>alias</pre> classes differ between one another, and then also with the way the same class can be implemented in ways or yield unexpected results when used in a context specific to a scriptable application.
return (shortFormat & linefeed & longFormat)</lang>
 
<pre>«class isot»</pre> and <pre>date</pre> class objects can coerce to and from one another. The <pre>«class isot»</pre> is an opaque data type wrapping the hexbytes of an ISO-8601 formatted datetime string. Sadly, it doesn’t permit one to use it to convert a string into hexbytes, but it is ideal for taking a <pre>date</pre> reference via the <pre>«class isot»</pre> intermediary form and coercing this to <pre>string</pre> class (whose chevron syntax form is <pre>«class TEXT»</pre>), but it will '''not''' coerce onto other AppleScript classes that are normally used interchangeably to represent strings, namely <pre>text</pre> (<pre>«class ctxt »</pre>) or <pre>unicode text</pre> (<pre>«class utxt»</pre>).
 
Both scripts provided below contributed separately by different users each make use of the features discussed above. The first demonstrates the simplest and most direct solution, but simplicity forces specificity, which limits what other algorithmic problems it could usefully or easily be adapted to solve. The latter borrows a functional approach and favours an initial investment to create the atomic handlers that pay off by providing the reusable building blocks of identical code that will be the composable functions all other scripts are assembled from sparing much of the need to write new code to solve the vast majority of problems on this site. Generality, versatility and adaptability of this approach can be proven mathematically.
 
<lang applescript>settell {year:y,(the month:m, day:d, weekday:w} to (current date)
set shortdate to text 1 thru 10 of (it as «class isot» as string)
set longdate to the contents of [its weekday, ", ", ¬
(its month), " ", day, ", ", year] as text
end tell
 
log the shortdate
log the longdate</lang>
 
{{output}}
<lang applescriptpre>"2020-09-11
Friday, September 11, 2020"</langpre>
 
 
Anonymous user