words of string or text item delimiters

Hi,

Does this script work internationally in removing colons and hyphens?

set cd to current date
set ci to cd as «class isot» as string
(words of ci) as string
--> "20131015T150400"

If so, does anyone know if this is the best way in removing the colons and hyphens?

Thanks,
kel

Because the definition of what constitutes a word varies, it’s rarely a good way to do anything.

Hi Shane,

That makes sense. I was just reading Adam’s post on delimiters and got different results:

words of "Now*is the.time&for all_good (folks) to learn-AppleScript"
--> {"Now", "*", "is", "the.time", "&", "for", "all", "_", "good", "folks", "to", "learn-AppleScript"}
-- My result
--> {"Now", "is", "the.time", "for", "all_good", "folks", "to", "learn", "AppleScript"}

Thanks a lot,
kel

Because it’s a fixed width you don’t need delimiters per se. It’s maybe hard to read but can be done in a single line:

tell ((current date) as «class isot» as string) to return text 1 thru 4 & text 6 thru 7 & text 9 thru 13 & text 15 thru 16 & text 18 thru 19

As all values in text item delimiters are considered since Snow Leopard, this is an alternative


set currentDate to current date
set isotDateString to currentDate as «class isot» as string
set {TID, text item delimiters} to {text item delimiters, {":", "-"}}
set isotItems to text items of isotDateString
set text item delimiters to ""
set trimmedIsotDateString to isotItems as text
set text item delimiters to TID

:wink:

set currentDate to current date
set isotDateString to currentDate as «class isot» as string
set {TID, text item delimiters} to {text item delimiters, {":", "T", "-"}}
set isotItems to text items of isotDateString
set text item delimiters to TID

McUsr, TS want’s to keep the ‘t’ in the string :wink:

Isn’t it safer to use this Nigel Garvey’s code :


on dateTimeStamp()
tell (current date) to return (((its year) * 10000 + (its month) * 100 + (its day)) as text) & "T" & text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text)
# return (do shell script "date +%Y%m%dT%H%M%S")
end dateTimeStamp

The no-shell versioin is 22 times faster than the shell one.

KOENIG Yvan (VALLAURIS, France) mercredi 16 octobre 2013 15:39:58

I didn’t see that! :slight_smile:

Hi everybody,

All versions look great. I didn’t think about doing it DJ Bazzie Wazzie’s way.

It makes me want to bring out the timer! :smiley: Looks like a battle between concatenation with range reference form vs. AS text item delimiters with coercion vs. date properties with concatenation. I can’t decide which way to bet on. I’d bet that Nigel timed his version. :slight_smile:

Edited: I forgot that Yvan said that about the time. The shell call has too much overhead for just a few dates, but for many dates I don’t know also.

Thanks for the ideas everybody,
kel

I timed also the Nigel’s version and got the same results than Nigel.
This is why I replaced the shell script by Nigel’s vesion in the handler which is embedded in my library.

Yvan KOENIG (VALLAURIS, France) mercredi 16 octobre 2013 20:41:17

Here’s an interesting thing from wikipedia:

From ISO #something

Does anyone know what mutual agreement means?

I think I know what that means. It’s mainly about the communication. They’re just saying that it’s ok to use either way, with or without the T. Why do they have to make things so complicated?

I think mutual agreement in the sense here, is that two parties can agree upon a specific protocol amongst themselves. Not a general usage between all users of the general scheme.

Oh I see. You’re saying that they’re saying that the users can agree to a certain protocol and it’s ok. That sounds like it.

gl,
kel

ps. that was a really bright gibbous moon last night. :slight_smile:

Wow!!! Nigel’s script is amazingly fast! :cool:

The first time I saw it I thought: what a mess, such an awful instruction will be a snail.
Then I saw the timing posted by Nigel and had difficulties to trut it so I mage my own test running the two scheme in a 10,000 iterations lup and I was forced to recognize that it’s extraordinary fast.
So, I typed a # in front of the shell instruction and inserted the “awful code”.
For some usages ” mainly file naming ” I drop the “T” which brings no useful information.

It’s one of the gems which I use daily.

Yvan KOENIG (VALLAURIS, France) jeudi 17 octobre 2013 20:42:24

Now I really do need to time it! :slight_smile: I’m a little busy right now though.

Thanks for all the input,
kel

Mine is not fair, if anyone wants to time it, because it will be cached (i think it has to be). I can do a million repeats still in a blink of an eye. On the other hand, considering the programming language where AppleScript is developed in, it doesn’t amaze me that my version is the fastest solution. But still, AppleScript is always against all odds.

So much stuff is cached these days, in varying places, that a lot of the timing tests done by repeating X times and dividing by X produce misleading results, IMO.