extracting date information from string

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

extracting date information from string

Post by rodneyt » Tue Aug 30, 2022 10:50 am

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9723
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: extracting date information from string

Post by dunbarx » Tue Aug 30, 2022 2:17 pm

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):

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)   ...
or something like:

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9723
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: extracting date information from string

Post by dunbarx » Tue Aug 30, 2022 2:37 pm

Try this. Make a field with a bunch of random text, and insert a date somewhere. Then in a button script:

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
Craig
Last edited by dunbarx on Tue Aug 30, 2022 2:51 pm, edited 3 times in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9723
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: extracting date information from string

Post by dunbarx » Tue Aug 30, 2022 2:42 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9723
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: extracting date information from string

Post by dunbarx » Tue Aug 30, 2022 3:00 pm

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:

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
And this still needs work. Too many particular character strings will slip by, and it still does not distinguish between multiple date instances.

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9444
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: extracting date information from string

Post by richmond62 » Tue Aug 30, 2022 4:04 pm

You're not feeling like Richmond, who has just walked 5 miles inside Frankfurt airport to find his flight to Sofia has been delayed. 8)

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9847
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: extracting date information from string

Post by FourthWorld » Tue Aug 30, 2022 6:08 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: extracting date information from string

Post by rodneyt » Tue Aug 30, 2022 10:32 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9723
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: extracting date information from string

Post by dunbarx » Tue Aug 30, 2022 10:53 pm

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

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: extracting date information from string

Post by rodneyt » Wed Aug 31, 2022 12:06 am

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

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: extracting date information from string

Post by rodneyt » Wed Aug 31, 2022 12:44 am

Another interesting option to play with: https://forum.keyboardmaestro.com/t/con ... -dd/3155/4

stam
Posts: 2729
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: extracting date information from string

Post by stam » Wed Aug 31, 2022 4:21 am

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 ;) )

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: extracting date information from string

Post by rodneyt » Wed Aug 31, 2022 9:33 am

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

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9444
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: extracting date information from string

Post by richmond62 » Wed Aug 31, 2022 11:01 am

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"

?

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9847
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: extracting date information from string

Post by FourthWorld » Wed Aug 31, 2022 11:41 am

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"

?
Which RFC or other common standard defines those formats?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”