 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
JDP Guest
|
Posted: Sun Feb 03, 2008 4:38 pm Post subject: wrap from 19xx vs 20xx |
|
|
if date is 1/2/09 you need to decide if that should be 1909 or 2009.
here is some code that addresses that:
| Code: |
DateParse(str) {
static e2 = "i)(?:(\d{1,2}+)[\s\.\-\/,]+)?(\d{1,2}|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\w*)[\s\.\-\/,]+(\d{2,4})"
str := RegExReplace(str, "((?:" . SubStr(e2, 42, 47) . ")\w*)(\s*)(\d{1,2})\b", "$3$2$1", "", 1)
If RegExMatch(str, "i)^\s*(?:(\d{4})([\s\-:\/])(\d{1,2})\2(\d{1,2}))?"
. "(?:\s*[T\s](\d{1,2})([\s\-:\/])(\d{1,2})(?:\6(\d{1,2})\s*(?:(Z)|(\+|\-)?"
. "(\d{1,2})\6(\d{1,2})(?:\6(\d{1,2}))?)?)?)?\s*$", i)
d3 := i1, d2 := i3, d1 := i4, t1 := i5, t2 := i7, t3 := i8
Else If !RegExMatch(str, "^\W*(\d{1,2}+)(\d{2})\W*$", t)
RegExMatch(str, "i)(\d{1,2})\s*:\s*(\d{1,2})(?:\s*(\d{1,2}))?(?:\s*([ap]m))?", t)
, RegExMatch(str, e2, d)
f = %A_FormatFloat%
SetFormat, Float, 02.0
;deal with wrap around of century based on past-future and offset to allow future
futureYearsAllowed=10
FormatTime, thisYear,,yyyy
StringLeft,thisCen,thisYear,2
lastCen:=thisCen-1
StringRight,thisYr,thisYear,2
thisYr+=futureYearsAllowed
d := (d3 ? (StrLen(d3) = 2 ? (d3>thisYr ? lastCen:thisCen) : "") . d3 : A_YYYY)
. ((d2 := d2 + 0 ? d2 : (InStr(e2, SubStr(d2, 1, 3)) - 40) // 4 + 1.0) > 0
? d2 + 0.0 : A_MM). ((d1 += 0.0) ? d1 : A_DD) . t1
+ (t1 = 12 ? t4 = "am" ? -12.0 : 0.0 : t4 = "am" ? 0.0 : 12.0) . t2 + 0.0 . t3 + 0.0
SetFormat, Float, %f%
Return, d
} |
|
|
| Back to top |
|
 |
EdScriptNewbie
Joined: 20 Jan 2007 Posts: 48
|
Posted: Sun Feb 03, 2008 9:36 pm Post subject: What do the last two posts do? |
|
|
i'm working to understand time stuff; what is the objective of the previous two posts? THanks _________________ ...Ed |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5031 Location: /b/
|
Posted: Sun Feb 03, 2008 9:51 pm Post subject: Re: What do the last two posts do? |
|
|
| EdScriptNewbie wrote: | | i'm working to understand time stuff; what is the objective of the previous two posts? THanks | empyrean5 posted my code for supporting American date formats whereas JDP's modification is a smarter algorithm to detect the millennia for a two digit year such as 08 (2008). If you do not need either of these features the script on my first post should suffice for regular use. _________________ Chat (IRC) • PlusNet • Scripts • IronAHK • Contact by email not private message. |
|
| Back to top |
|
 |
EdScriptNewbie
Joined: 20 Jan 2007 Posts: 48
|
Posted: Sun Feb 03, 2008 10:06 pm Post subject: |
|
|
thanks! ...Ed _________________ ...Ed |
|
| Back to top |
|
 |
pleasure2code Guest
|
Posted: Sat Feb 21, 2009 7:40 pm Post subject: |
|
|
Below transformation seems not to work
| Code: | | time := DateParse("2009-Feb-20 11:23:19") |
Any suggestions? |
|
| Back to top |
|
 |
Big Digger
Joined: 07 Feb 2009 Posts: 15
|
Posted: Sat Mar 07, 2009 10:40 pm Post subject: |
|
|
| Code: | DateParse("Sat, 07 Mar 2009 13:43:58 GMT")
DateParse("07 Mar 2009 13:43:58")
DateParse("07 Mar 2009 13:43") |
returns for me 200903072543 |
|
| Back to top |
|
 |
DeWild1
Joined: 30 Apr 2006 Posts: 326 Location: Shigle Springs
|
Posted: Tue Mar 31, 2009 5:41 pm Post subject: |
|
|
| Titan wrote: | | For such a trivial mod users can refer to the info on this thread. Incorporating it to my official release breaks the simplicity of a single parameter and introduces culture variation dependence which can be complicated given the extra parsing rules, word translations, etc. |
Ya, but if you could put it on the first post, you could save a simpleton like me a few pages of reading!!  _________________ CPULOCK.com
virusSWAT.com
GuaranteedPCFIX.com
911PCFIX.com |
|
| Back to top |
|
 |
animeaime
Joined: 04 Nov 2008 Posts: 1046
|
Posted: Sun Apr 12, 2009 8:42 pm Post subject: |
|
|
I just found your DateParse function, and it works great, thanks for it. I found some scenarios that I want some input on.
Edit:
I forgot to mention that I ran these scenarios on April 12th, 2009. This should clear up confusion when viewed on a later date.
Scenario 1
| Code: | Date := "January 2nd, 2009"
FormatTime, FormattedTime, % DateParse(Date), MMMM dd, yyyy
;outputs "April 12, 2009"
MsgBox, % FormattedTime |
Scenario 2
| Code: | Date := "January 08, 2009 9:05:27"
FormatTime, FormattedTime, % DateParse(Date), MMMM dd, yyyy h:mm:ss
;outputs "January 08, 2009 9:05:00"
MsgBox, % FormattedTime
|
Scenario 3
In this case, I would expect 09 to be the day. It should have been 2009 if meant to be the year (which your function does support).
| Code: | Date := "January 09 9:05:27"
FormatTime, FormattedTime, % DateParse(Date), MMMM dd h:mm:ss
;outputs "April 12 9:05:00"
MsgBox, % FormattedTime |
_________________ As always, if you have any further questions, don't hesitate to ask.
Add OOP to your scripts via the Class Library. Check out my scripts. |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5031 Location: /b/
|
Posted: Sun Apr 12, 2009 9:43 pm Post subject: |
|
|
| animeaime wrote: | | Date := "January 09 9:05:27" |
| animeaime wrote: | | Date := "January 2nd, 2009" | My regex was written only for the British date notation which is day-month-year.
| animeaime wrote: | | Date := "January 08, 2009 9:05:27" | Seems like the code "(?:(Z)|(\+|\-)?" . "(\d{1,2})\6(\d{1,2})" was supposed to handle seconds and time zone offsets as per ISO 8601 but I didn't bother completing this. _________________ Chat (IRC) • PlusNet • Scripts • IronAHK • Contact by email not private message. |
|
| Back to top |
|
 |
animeaime
Joined: 04 Nov 2008 Posts: 1046
|
Posted: Sun Apr 12, 2009 10:40 pm Post subject: |
|
|
Given that, would you be offended if I released my own version of DateParse?
(Edit) I removed the exta, as I realized, I presumed things that I shouldn't have. _________________ As always, if you have any further questions, don't hesitate to ask.
Add OOP to your scripts via the Class Library. Check out my scripts. |
|
| Back to top |
|
 |
Joy2DWorld
Joined: 04 Dec 2006 Posts: 537 Location: Galil, Israel
|
Posted: Mon Apr 13, 2009 9:23 am Post subject: |
|
|
| animeaime wrote: | Given that, would you be offended if I released my own version of DateParse?
(Edit) I removed the exta, as I realized, I presumed things that I shouldn't have. |
hopefully we all learn from & teach each other. Multiple approaches of same idea in different ways seems beneficial all around. _________________ Joyce Jamce |
|
| Back to top |
|
 |
animeaime
Joined: 04 Nov 2008 Posts: 1046
|
Posted: Mon Apr 13, 2009 9:27 am Post subject: |
|
|
Thanks, I always worry that it might come off as "territorial" - like I was trying to outwrite someone else's existing code. _________________ As always, if you have any further questions, don't hesitate to ask.
Add OOP to your scripts via the Class Library. Check out my scripts. |
|
| Back to top |
|
 |
mikek
Joined: 21 Nov 2008 Posts: 28 Location: Monterey, California
|
Posted: Fri May 01, 2009 8:43 pm Post subject: Re: American Date Format |
|
|
| empyrean5 wrote: | I needed to make the following change to make American Date Format mm/dd/yyyy hh:mm.... work:
| Code: | DateParse(str) {
static e2 = "i)(?:(\d{1, ; [...]
str := RegExReplace(str, "(\d{1,2})(\D+)(\d{1,2})(.*)", "$3$2$1$4") |
|
This works great! Thank you, Titan, and empyrean5 for the modification.
- Mike |
|
| Back to top |
|
 |
pajenn
Joined: 07 Feb 2009 Posts: 349
|
Posted: Wed May 13, 2009 1:57 pm Post subject: |
|
|
I'm in need of the function addressed by this script (US version), however, it doesn't work when I add in seconds. For example,
| Code: | MsgBox % DateParse("3/15/2009 11:22 AM")
;MsgBox message = 200903151122 (correct)
MsgBox % DateParse("3/15/2009 11:22:24 AM")
;MsgBox message = 200903152322 (incorrect) |
Is there something I can do to enable the seconds? _________________ Hardware: 1.8 GHz laptop with 4 GB ram
Software: Windows XP/SP3, Rising Antivirus, Online Armor firewall, Dragon NaturallySpeaking Pro + Natlink (with Vocola/Unimacro) and AHK for macro support. |
|
| Back to top |
|
 |
alpha Guest
|
Posted: Wed May 13, 2009 2:31 pm Post subject: |
|
|
| pajenn wrote: | I'm in need of the function addressed by this script (US version), however, it doesn't work when I add in seconds. For example,
| Code: | MsgBox % DateParse("3/15/2009 11:22 AM")
;MsgBox message = 200903151122 (correct)
MsgBox % DateParse("3/15/2009 11:22:24 AM")
;MsgBox message = 200903152322 (incorrect) |
Is there something I can do to enable the seconds? |
Yes, dig in and change the regex. That is what I did (it about drove me nuts getting it right).
Here is the code I used. I works with your example
| Code: | MsgBox % DateParse("3/15/2009 11:22:24 AM") ; result = 20091503112224
DateParse(str) {
static e2 = "i)(?:(\d{1,2}+)[\s\.\-\/,]+)?(\d{1,2}|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\w*)[\s\.\-\/,]+(\d{2,4})"
str := RegExReplace(str, "((?:" . SubStr(e2, 42, 47) . ")\w*)(\s*)(\d{1,2})\b", "$3$2$1", "", 1)
If RegExMatch(str, "i)^\s*(?:(\d{4})([\s\-:\/])(\d{1,2})\2(\d{1,2}))?"
. "(?:\s*[T\s](\d{1,2})([\s\-:\/])(\d{1,2})(?:\6(\d{1,2})\s*(?:(Z)|(\+|\-)?"
. "(\d{1,2})\6(\d{1,2})(?:\6(\d{1,2}))?)?)?)?\s*$", i)
d3 := i1, d2 := i3, d1 := i4, t1 := i5, t2 := i7, t3 := i8, t4 := t1 >11 ? "pm" : "am"
Else If !RegExMatch(str, "x)^\W* (\d{1,2}?) (\d{2}) (\d{2}+)? (?:\s*([ap]m))? \W* $", t)
RegExMatch(str, "ix)(\d{1,2}) \s* : \s* (\d{1,2}) (?:(?:\s*:\s*) (\d{1,2}))? (?:\s*([ap]m))?", t)
, RegExMatch(str, e2, d)
f = %A_FormatFloat%
SetFormat, Float, 02.0
d := (d3 ? (StrLen(d3) = 2 ? 20 : "") . d3 : A_YYYY)
. ((d2 := d2 + 0 ? d2 : (InStr(e2, SubStr(d2, 1, 3)) - 40) // 4 + 1.0) > 0
? d2 + 0.0 : A_MM). ((d1 += 0.0) ? d1 : A_DD) . t1
SetFormat, Float, %f%
Return, d
}
|
|
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|