AutoHotkey Community

It is currently May 26th, 2012, 8:26 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 68 posts ]  Go to page Previous  1, 2, 3, 4, 5  Next
Author Message
 Post subject:
PostPosted: May 13th, 2009, 4:15 pm 
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)
}


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: May 13th, 2009, 8:21 pm 
Offline

Joined: February 7th, 2009, 11:28 pm
Posts: 384
thank you alpha. that last one works great.

_________________
Hardware: 1.8 GHz laptop with 4 GB ram, Windows XP/SP3
Software: Prevx, Privatefirewall, KeyScrambler.


Report this post
Top
 Profile  
Reply with quote  
PostPosted: July 20th, 2009, 1:08 am 
Offline

Joined: January 20th, 2007, 7:47 pm
Posts: 110
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


Report this post
Top
 Profile  
Reply with quote  
PostPosted: July 20th, 2009, 12:12 pm 
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.


Report this post
Top
  
Reply with quote  
PostPosted: July 20th, 2009, 12:29 pm 
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:


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 16th, 2009, 7:56 pm 
Offline

Joined: November 24th, 2005, 8:16 am
Posts: 851
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 17th, 2009, 4:00 pm 
Offline

Joined: November 24th, 2005, 8:16 am
Posts: 851
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 17th, 2009, 4:41 pm 
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
}


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 17th, 2009, 4:58 pm 
Offline

Joined: November 24th, 2005, 8:16 am
Posts: 851
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


Report this post
Top
 Profile  
Reply with quote  
PostPosted: March 28th, 2010, 3:32 pm 
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.


Report this post
Top
  
Reply with quote  
 Post subject: EnvAdd problem
PostPosted: April 20th, 2011, 4:38 pm 
Offline

Joined: June 18th, 2006, 8:47 am
Posts: 346
Location: Phoenix, AZ
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 21st, 2011, 8:13 am 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Code:
v_Date = 20110425000000
EnvAdd, v_Date, 1, Days
MsgBox, %v_Date%
FormatTime, v_AdjustedDate, %v_Date%, yyyy/MM/dd
Msgbox, %v_AdjustedDate%
return


:)


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Do'h
PostPosted: April 21st, 2011, 11:11 am 
Offline

Joined: June 18th, 2006, 8:47 am
Posts: 346
Location: Phoenix, AZ
Thanks SKAN!
Right now I am so glad I can laugh at myself.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Error using AHK_L
PostPosted: June 1st, 2011, 8:41 pm 
Offline

Joined: June 18th, 2006, 8:47 am
Posts: 346
Location: Phoenix, AZ
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 1st, 2011, 8:44 pm 
Offline

Joined: December 26th, 2010, 7:40 pm
Posts: 4172
Location: Awesometown, USA
need a space before and after each .

_________________
Autofire, AutoClick, Toggle, SpamWindow Control Tools
Recommended: AutoHotkey_L


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 68 posts ]  Go to page Previous  1, 2, 3, 4, 5  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Exabot [Bot], Google Feedfetcher, notsoobvious, tomoe_uehara and 16 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group