AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Date parser - convert any date format to YYYYMMDDHH24MISS
Goto page Previous  1, 2, 3, 4
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
alpha
Guest





PostPosted: Wed May 13, 2009 4:15 pm    Post subject: Reply with quote

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

PostPosted: Wed May 13, 2009 8:21 pm    Post subject: Reply with quote

thank you alpha. that last one works great.
_________________
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
View user's profile Send private message
EdScriptNewbie



Joined: 20 Jan 2007
Posts: 48

PostPosted: Mon Jul 20, 2009 1:08 am    Post subject: Trouble calling & using Dateparser Reply with quote

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
View user's profile Send private message
alpha
Guest





PostPosted: Mon Jul 20, 2009 12:12 pm    Post subject: Re: Trouble calling & using Dateparser Reply with quote

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





PostPosted: Mon Jul 20, 2009 12:29 pm    Post subject: Re: Trouble calling & using Dateparser Reply with quote

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

PostPosted: Sun Aug 16, 2009 7:56 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Icarus



Joined: 24 Nov 2005
Posts: 824

PostPosted: Mon Aug 17, 2009 4:00 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
alpha
Guest





PostPosted: Mon Aug 17, 2009 4:41 pm    Post subject: Reply with quote

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

PostPosted: Mon Aug 17, 2009 4:58 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3, 4
Page 4 of 4

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group