 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
alpha Guest
|
Posted: Wed May 13, 2009 3:15 pm Post subject: |
|
|
pajenn
I have too many versions of this function around, and cannot find the US time with seconds version (sorry, but my last post was euro time with seconds). If you don't mind a little kludge, this code works with US time with seconds (I tested it with several time values and it seems to work correctly).
| Code: | MsgBox % DateParse("3/15/2009 11:22:24 AM") ; = 20090315112224
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
+ (((t4 = "pm") && (t1 < 12)) ? +12.0 : 0.0) . t2 +0.0 . t3 + 0.0 ; use this one - defaults to AM
SetFormat, Float, %f%
Return, SubStr(d,1,4) SubStr(d,7,2) SubStr(d,5,2) SubStr(d,9)
} |
|
|
| Back to top |
|
 |
pajenn
Joined: 07 Feb 2009 Posts: 384
|
Posted: Wed May 13, 2009 7:21 pm Post subject: |
|
|
thank you alpha. that last one works great. _________________ Hardware: 1.8 GHz laptop with 4 GB ram, Windows XP/SP3
Software: Prevx, Privatefirewall, KeyScrambler. |
|
| Back to top |
|
 |
EdScriptNewbie
Joined: 20 Jan 2007 Posts: 110
|
Posted: Mon Jul 20, 2009 12:08 am Post subject: Trouble calling & using Dateparser |
|
|
I'm calling dateparser from my always-loaded script, as follows (prolix amateur code alert!): | Code: | ; RENAME EXCEL "WIP" FILES NOW NAMED EG "7-22-07.xls" TO "2007-07-22 WIP.xls" (or .xlsx if excel 2007)
#t::
sendinput,{AppsKey} ; Copy file name in "rename" mode
sleep,30
sendinput,m
sleep,30
sendinput,^c
OldWipFileName := %clipboard% ; take the whole filename from the clipboard
IfInString,OldWipFileName,.xlsx
stringRight,WipExtension,OldWipFileName,5 ; captures excel 2007 extension
StringTrimRight,DatePart,OldWipFileName,5 ; trim off excel 2007 extension
goto,DateParsing ; skips the triming of excel 2003 extension
IfInString,OldWipFileName,.xls
stringRight,WipExtension,OldWipFileName,4 ; captures excel 2003 extension
StringTrimRight,DatePart,OldWipFileName,4 ; trim off excel 2003 extension
DateParsing:
#include C:\Program Files\AutoHotkey\Scripts-Ed\DateParser.ahk ; Makes DateParser script available here
NewParsedWipDate := DateParse("%DatePart%") ; Use DateParser to parse date
FormatTime,NewWipDate,&NewParsedWipDate&,yyyy-MM-dd ; Create date in desired format
NewWipFileName := %NewWipDate%%A_Space%WIP%WipExtension% ; use date to make a new filename
Sendinput,%NewWipFileName% ; Complete re-naming by typing in the new filename
exit
|
It won't re-load; gives me the following reason:
---------------------------
AutoHotkey.ini
---------------------------
Error in #include file "C:\Program Files\AutoHotkey\Scripts-Ed\DateParser.ahk": A label must not point to a function.
Line#
051: IfInString,OldWipFileName,.xlsx
052: StringRight,WipExtension,OldWipFileName,5
053: StringTrimRight,DatePart,OldWipFileName,5
054: Goto,DateParsing
055: IfInString,OldWipFileName,.xls
056: StringRight,WipExtension,OldWipFileName,4
057: StringTrimRight,DatePart,OldWipFileName,4
---> 024: {
The script was not reloaded; the old version will remain in effect.
---------------------------
OK
---------------------------
I'm sure it has to do with how I call dateparser, but I don't know how I should have done it. Can someone help me?
While I'm here, will the following line create the filename I want?Thanks.
| Code: | NewWipFileName := %NewWipDate%%A_Space%WIP%WipExtension% ; use date to make a new filename
| I.e., in the format "2007-07-22 WIP.xls"?
Thanks!
_________________ ...Ed |
|
| Back to top |
|
 |
alpha Guest
|
Posted: Mon Jul 20, 2009 11:12 am Post subject: Re: Trouble calling & using Dateparser |
|
|
| EdScriptNewbie wrote: | | I'm calling dateparser from my always-loaded script, as follows (prolix amateur code alert!): |
Without looking too closely at your code, and given the error message you get, try this:
| Code: |
; RENAME EXCEL "WIP" FILES NOW NAMED EG "7-22-07.xls" TO "2007-07-22 WIP.xls" (or .xlsx if excel 2007)
#t::
sendinput,{AppsKey} ; Copy file name in "rename" mode
sleep,30
sendinput,m
sleep,30
sendinput,^c
OldWipFileName := %clipboard% ; take the whole filename from the clipboard
IfInString,OldWipFileName,.xlsx
stringRight,WipExtension,OldWipFileName,5 ; captures excel 2007 extension
StringTrimRight,DatePart,OldWipFileName,5 ; trim off excel 2007 extension
goto,DateParsing ; skips the triming of excel 2003 extension
IfInString,OldWipFileName,.xls
stringRight,WipExtension,OldWipFileName,4 ; captures excel 2003 extension
StringTrimRight,DatePart,OldWipFileName,4 ; trim off excel 2003 extension
DateParsing:
; remove this include and put it below the exit
; #include C:\Program Files\AutoHotkey\Scripts-Ed\DateParser.ahk ; Makes DateParser script available here
NewParsedWipDate := DateParse("%DatePart%") ; Use DateParser to parse date
FormatTime,NewWipDate,&NewParsedWipDate&,yyyy-MM-dd ; Create date in desired format
NewWipFileName := %NewWipDate%%A_Space%WIP%WipExtension% ; use date to make a new filename
Sendinput,%NewWipFileName% ; Complete re-naming by typing in the new filename
exit
#include C:\Program Files\AutoHotkey\Scripts-Ed\DateParser.ahk ; Makes DateParser script available here
|
If you still get that error, please include the version (of the many in this thread) that you are using. |
|
| Back to top |
|
 |
alpha Guest
|
Posted: Mon Jul 20, 2009 11:29 am Post subject: Re: Trouble calling & using Dateparser |
|
|
| alpha wrote: | Without looking too closely at your code, and given the error message you get, try this:
| Code: |
....
IfInString,OldWipFileName,.xlsx
stringRight,WipExtension,OldWipFileName,5 ; captures excel 2007 extension
StringTrimRight,DatePart,OldWipFileName,5 ; trim off excel 2007 extension
goto,DateParsing ; skips the triming of excel 2003 extension
IfInString,OldWipFileName,.xls
stringRight,WipExtension,OldWipFileName,4 ; captures excel 2003 extension
StringTrimRight,DatePart,OldWipFileName,4 ; trim off excel 2003 extension
DateParsing: |
|
Actually, after posting I did look at your code. You had better spend some time with the help file - specifically the parts on IF statements and blocks.
Indentation does not control program flow - braces do.
| Code: |
...
IfInString,OldWipFileName,.xlsx
{
stringRight,WipExtension,OldWipFileName,5 ; captures excel 2007 extension
StringTrimRight,DatePart,OldWipFileName,5 ; trim off excel 2007 extension
goto,DateParsing ; skips the triming of excel 2003 extension
}
IfInString,OldWipFileName,.xls
{
stringRight,WipExtension,OldWipFileName,4 ; captures excel 2003 extension
StringTrimRight,DatePart,OldWipFileName,4 ; trim off excel 2003 extension
}
DateParsing: |
|
|
| Back to top |
|
 |
Icarus
Joined: 24 Nov 2005 Posts: 851
|
Posted: Sun Aug 16, 2009 6:56 pm Post subject: |
|
|
Titan, this is great, thanks a lot.
And Chris, thanks for linking from the help file.
As always the AHK help file is a thing of beauty. _________________ Sector-Seven - Freeware tools built with AutoHotkey |
|
| Back to top |
|
 |
Icarus
Joined: 24 Nov 2005 Posts: 851
|
Posted: Mon Aug 17, 2009 3:00 pm Post subject: |
|
|
Actually, I also have some problem with the hour returning as "25" as was mentioned earlier in this thread.
This: Mon, 17 Aug 2009 13:23:33 GMT
Returned this: 200908172523
EDIT:
Also, as far as I could figure out, there is no adjustment to local time, so this is losing data - the above example will render a timestamp that cannot be compared to anything other than stamps that came from the same source.
Did I miss anything? _________________ Sector-Seven - Freeware tools built with AutoHotkey |
|
| Back to top |
|
 |
alpha Guest
|
Posted: Mon Aug 17, 2009 3:41 pm Post subject: |
|
|
| Icarus wrote: | Actually, I also have some problem with the hour returning as "25" as was mentioned earlier in this thread.
This: Mon, 17 Aug 2009 13:23:33 GMT
Returned this: 200908172523
EDIT:
Also, as far as I could figure out, there is no adjustment to local time, so this is losing data - the above example will render a timestamp that cannot be compared to anything other than stamps that came from the same source.
Did I miss anything? | Yes, there is no provision for local/gmt corrections. Also, none of code in this thread allow for "Mon, ..." or GMT in the passed string. Further more, none of the variations of this function support seconds, except those I posted.
Your example Mon, 17 Aug 2009 13:23:33 GMT if passed as 17 Aug 2009 13:23:33 will work with the euro format version that I posted here http://www.autohotkey.com/forum/post-268817.html#268817
The US version is posted here http://www.autohotkey.com/forum/post-268830.html#268830
Your test case works with the euro version if you limit the string to the only the required parts.
| Code: | MsgBox % DateParse("17 Aug 2009 13:23:33") ; = 20090817132333
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
+ (((t4 = "pm") && (t1 < 12)) ? +12.0 : 0.0) . t2 +0.0 . t3 + 0.0 ; use this one - defaults to AM
SetFormat, Float, %f%
Return, d
} |
|
|
| Back to top |
|
 |
Icarus
Joined: 24 Nov 2005 Posts: 851
|
Posted: Mon Aug 17, 2009 3:58 pm Post subject: |
|
|
Thanks alpha,
The datestamp I used is one from Yahoo RSS feeds, and is pretty common.
I believe we should have a function that is similar to the functions available in other languages (like PHP) which converts dates from/to any standard string and considering timezone in the process.
Titan's code (and all the following variations) seem to favor short code over readability, so it is not that easy to modify.
I will probably have to take it as a base, and prepare my own version.
Thanks again. _________________ Sector-Seven - Freeware tools built with AutoHotkey |
|
| Back to top |
|
 |
Guest
|
Posted: Sun Mar 28, 2010 2:32 pm Post subject: Re: doing date math with Outlook's "Wed 6/27/2007" |
|
|
| EdScriptNewbie wrote: | I hope it's ok that I put this here; somehow if I understood any of this thread about dates I probably would not need to ask this at all.
I want to select the date field in an Outlook Task, collect the date (into a variable?), then add to or subtract from that date a certain number of days, and then, write the resulting new date into the field in a format it can use. The Outlook Task date field is in this format: "Wed 6/27/2007."
The following might make this easier:
1) the day name part of it is always three letters followed by a space, and
2) if you select the whole field and just paste in 6/28/2007, then when you tab onward, Outlook in its wisdom figures out how to make the field say "Thu 6/28/2007" all by itself. |
|
|
| Back to top |
|
 |
keybored
Joined: 18 Jun 2006 Posts: 343 Location: Phoenix, AZ
|
Posted: Wed Apr 20, 2011 3:38 pm Post subject: EnvAdd problem |
|
|
This function results in this date "20110425000000" which I am having trouble adjusting via EnvAdd. Can anyone tell me how best do work with that?
| Code: | v_String = 25-APR-2011
v_Date := DateParse(v_String) |
| Code: | v_Date = 20110425000000
EnvAdd, v_Date, 1, Days
MsgBox, %v_Date%
FormatTime, v_AdjustedDate, v_Date, yyyy/MM/dd
Msgbox, %v_AdjustedDate%
return
|
|
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Thu Apr 21, 2011 7:13 am Post subject: |
|
|
| Code: | v_Date = 20110425000000
EnvAdd, v_Date, 1, Days
MsgBox, %v_Date%
FormatTime, v_AdjustedDate, %v_Date%, yyyy/MM/dd
Msgbox, %v_AdjustedDate%
return |
 |
|
| Back to top |
|
 |
keybored
Joined: 18 Jun 2006 Posts: 343 Location: Phoenix, AZ
|
Posted: Thu Apr 21, 2011 10:11 am Post subject: Do'h |
|
|
Thanks SKAN!
Right now I am so glad I can laugh at myself. |
|
| Back to top |
|
 |
keybored
Joined: 18 Jun 2006 Posts: 343 Location: Phoenix, AZ
|
Posted: Wed Jun 01, 2011 7:41 pm Post subject: Error using AHK_L |
|
|
Anyone else have trouble with this using AHK_L?
I get the below error;
Error: Unsupported use of "."
---> 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 + (t1 = 12 ? t4 = "am" ? -12.0 : 0.0 : t4 = "am" ? 0.0 : 12.0) . t2 + 0.0 . t3 + 0.0 |
|
| Back to top |
|
 |
nimda
Joined: 26 Dec 2010 Posts: 3844 Location: Awesometown, USA
|
|
| 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
|