extracting date information from string
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
extracting date information from string
For an app Iḿ building I am interested in being able to examine a string (for example the clipboard contents) and see if I can identify a date. The date information could be part of say a copied event invitation or snippet of content from a web page, so the date could be formatted in a variety of different ways. It would be helpful to also be able to identify timezone indicators if these are included.
Is anyone aware of a standard libray or behaviour to do this - .e.g pass in a string, and get back a date object for a validate date contained within.
Seems like a common problem so I thought I would ask in case someone has run across this before.
Rodney
Is anyone aware of a standard libray or behaviour to do this - .e.g pass in a string, and get back a date object for a validate date contained within.
Seems like a common problem so I thought I would ask in case someone has run across this before.
Rodney
-
- VIP Livecode Opensource Backer
- Posts: 9723
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: extracting date information from string
Hmmm.
You would have to analyze the string for telltale sequences of characters, for example "xxx3/5/22yyy". You could do this by a character examination, something like (pseudo):
or something like:
That sort of thing. It is tedious, as can be seen from the above, and you have to consider the many ways that dates can be presented and the fact that the number of chars that represent a data can vary, but it seems that such a method is possible.
Craig
You would have to analyze the string for telltale sequences of characters, for example "xxx3/5/22yyy". You could do this by a character examination, something like (pseudo):
Code: Select all
repeat with y = 1 to the number of chars of yourString
if char y of yourString = "/" and char (y - 1) of yourString is an integer or\
(char (y-1) of yourString is an integer and char (y-2) of yourString is an integer) ...
Code: Select all
repeat with y = 1 to the number of chars of yourString
if char y of yourString is an integer and char y to (y+ 6) of yourString is a date
Craig
-
- VIP Livecode Opensource Backer
- Posts: 9723
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: extracting date information from string
Try this. Make a field with a bunch of random text, and insert a date somewhere. Then in a button script:
Craig
Code: Select all
on mouseup
put fld 1 into yourString
repeat with y = 1 to the length of yourString
if (charToNum(char y of yourString) > 47 and charToNum(char y of yourString) < 58) or char y of yourString = "/" then
put char y of yourString after temp
put y & space after accum
end if
end repeat
put word 1 of accum into startChar
answer temp & return & "Starts at" && startChar
end mouseup
Last edited by dunbarx on Tue Aug 30, 2022 2:51 pm, edited 3 times in total.
-
- VIP Livecode Opensource Backer
- Posts: 9723
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: extracting date information from string
I see that the handler in my post just above goes off the rails a bit if there is more that one date embedded in the text. But that can be remedied if this method seems attractive. Also, it catches
@Rodneyt Is it attractive? Do any of these ideas seem useful?
Craig
@Rodneyt Is it attractive? Do any of these ideas seem useful?
Craig
-
- VIP Livecode Opensource Backer
- Posts: 9723
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: extracting date information from string
I feel again like Richmond.
The fact that, say, "5" is considered by LC as a valid date makes my little handler problematic. You would have to:
And this still needs work. Too many particular character strings will slip by, and it still does not distinguish between multiple date instances.
Craig
The fact that, say, "5" is considered by LC as a valid date makes my little handler problematic. You would have to:
Code: Select all
on mouseup
put fld 1 into yourString
repeat with y = 1 to the length of yourString
if (charToNum(char y of yourString) > 47 and charToNum(char y of yourString) < 58) or char y of yourString = "/" then
put char y of yourString after temp
put y & space after accum
end if
end repeat
if temp is not an integer and temp is a date then
put word 1 of accum into startChar
answer temp & return & "Starts at" && startChar
end if
end mouseup
Craig
-
- Livecode Opensource Backer
- Posts: 9444
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: extracting date information from string
You're not feeling like Richmond, who has just walked 5 miles inside Frankfurt airport to find his flight to Sofia has been delayed.
-
- VIP Livecode Opensource Backer
- Posts: 9847
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: extracting date information from string
Good to see you pop back here lately.
Time and date expressions are notoriously cumbersome because there are so many different formats.
Attempting to support all of them is a noble pursuit, but impractical, at least efficiently.
Do you find commonalities among the snippets you're interested in? Perhaps we could start with those, hopefully building in extensibility options so new parsers could be added over time as needed.
Time and date expressions are notoriously cumbersome because there are so many different formats.
Attempting to support all of them is a noble pursuit, but impractical, at least efficiently.
Do you find commonalities among the snippets you're interested in? Perhaps we could start with those, hopefully building in extensibility options so new parsers could be added over time as needed.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: extracting date information from string
I might also investigate CLI-based options, threads like this in Stackoverflow https://stackoverflow.com/questions/107 ... bash-shell
Agree, the task is notorious, but notorious is as Livecode does Many are called few are chosen etc
Rod
Agree, the task is notorious, but notorious is as Livecode does Many are called few are chosen etc
Rod
-
- VIP Livecode Opensource Backer
- Posts: 9723
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: extracting date information from string
Rodneyt.
That StackOverFlow thread looks like it only validates a date string, not find one in a haystack. LC does that in one line.
As Richard said, are you dealing with one form of date? Two? Ten? And might you be presented with more than one type in the source string?
You already have mine, which seems to work, albeit with only one instance of one type. You would have to test several times (or several different ways) to cover other formats, but that is not a problem as long as you know what they are. And it is simple to find multiple instances if they exist.
In other words, what are you missing, and/or what are you not telling us?
Craig
That StackOverFlow thread looks like it only validates a date string, not find one in a haystack. LC does that in one line.
As Richard said, are you dealing with one form of date? Two? Ten? And might you be presented with more than one type in the source string?
You already have mine, which seems to work, albeit with only one instance of one type. You would have to test several times (or several different ways) to cover other formats, but that is not a problem as long as you know what they are. And it is simple to find multiple instances if they exist.
In other words, what are you missing, and/or what are you not telling us?
Craig
Re: extracting date information from string
Hi Craig,
Thanks for your message - Iĺl let you know more once I start experimenting with this feature. It was more of an curiosity question to see if anyone has dealt with this issue before. Thanks for your script snippet - I will definitely check that out.
Rod
Thanks for your message - Iĺl let you know more once I start experimenting with this feature. It was more of an curiosity question to see if anyone has dealt with this issue before. Thanks for your script snippet - I will definitely check that out.
Rod
Re: extracting date information from string
Another interesting option to play with: https://forum.keyboardmaestro.com/t/con ... -dd/3155/4
Re: extracting date information from string
Constructing a handler in LC to detect “dates” is never going to easy because the possible variations are as ridiculous as human language (and that’s just the separators!)
MacOS seems to do a good (but not foolproof) job of it and the AppleScript referring to data detectors may help - but is Mac-only.
A quick search on regex shows this can possibly help, at least with numerical dates - have a look here : https://stackoverflow.com/questions/512 ... alid-dates
I found the 3rd answer (40 upvotes) the most useful as it breaks down the regex logically to show how to construct searches for valid delimiters, days, months and years. I guess the month search could be modified to include 3-letter and full month names.
This would allow you to build regex searches for each format.
I suspect that if you did use regex you’d need to do sequential (probably recursive) searches in LC for the various formats and occurrences.
As others have said, this is certainly not an easy task… and nigh-on impossible to make foolproof (given the ingenuity of fools )
MacOS seems to do a good (but not foolproof) job of it and the AppleScript referring to data detectors may help - but is Mac-only.
A quick search on regex shows this can possibly help, at least with numerical dates - have a look here : https://stackoverflow.com/questions/512 ... alid-dates
I found the 3rd answer (40 upvotes) the most useful as it breaks down the regex logically to show how to construct searches for valid delimiters, days, months and years. I guess the month search could be modified to include 3-letter and full month names.
This would allow you to build regex searches for each format.
I suspect that if you did use regex you’d need to do sequential (probably recursive) searches in LC for the various formats and occurrences.
As others have said, this is certainly not an easy task… and nigh-on impossible to make foolproof (given the ingenuity of fools )
Re: extracting date information from string
In my case I'm building a MacOS (only) app, so the Applescript solution is interesting, given how well it seems to work in testing
-
- Livecode Opensource Backer
- Posts: 9444
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: extracting date information from string
Craig . . .
Semi back in the saddle . . .
With your example how will I differentiate between:
"187<804granny/05/1952%334" and
"7758÷664july/11/1924£9977"
?
Semi back in the saddle . . .
With your example how will I differentiate between:
"187<804granny/05/1952%334" and
"7758÷664july/11/1924£9977"
?
-
- VIP Livecode Opensource Backer
- Posts: 9847
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: extracting date information from string
Which RFC or other common standard defines those formats?richmond62 wrote: ↑Wed Aug 31, 2022 11:01 am...how will I differentiate between:
"187<804granny/05/1952%334" and
"7758÷664july/11/1924£9977"
?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn