RFC822デコーダー v0

AppleScript名:RFC822デコーダー v0
— Created 2016-02-07 by Takaaki Naganoya
— 2016 Piyomaru Software
–http://stackoverflow.com/questions/1850824/parsing-a-rfc-822-date-with-nsdateformatter
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set dateString to "Sun, 19 May 2002 15:21:36 GMT"
–set dateString to "Tue, 01 Dec 2009 08:48:25 +0000"

set aRes to rfc822StrDecode(dateString) of me
–> date "2002年5月20日月曜日 0:21:36"

–http://stackoverflow.com/questions/1850824/parsing-a-rfc-822-date-with-nsdateformatter
on rfc822StrDecode(dateString)
  set aTZ to "GMT"
  
set en_US_POSIX to current application’s NSLocale’s alloc()’s initWithLocaleIdentifier:"en_US_POSIX"
  
set dateFormatter to current application’s NSDateFormatter’s alloc()’s init()
  
dateFormatter’s setLocale:en_US_POSIX
  
dateFormatter’s setTimeZone:(current application’s NSTimeZone’s timeZoneWithAbbreviation:(current application’s NSString’s stringWithString:aTZ))
  
  
set aDate to missing value
  
set RFC822String to (current application’s NSString’s stringWithString:dateString)’s uppercaseString()
  
if (RFC822String’s rangeOfString:",")’s location() is not equal to (current application’s NSNotFound) then
    if aDate is equal to missing value then
      — Sun, 19 May 2002 15:21:36 GMT
      
dateFormatter’s setDateFormat:"EEE, d MMM yyyy HH:mm:ss zzz"
      
set aDate to dateFormatter’s dateFromString:RFC822String
    end if
    
    
if aDate is equal to missing value then
      — Sun, 19 May 2002 15:21 GMT
      
dateFormatter’s setDateFormat:"EEE, d MMM yyyy HH:mm zzz"
      
set aDate to dateFormatter’s dateFromString:RFC822String
    end if
    
    
if aDate is equal to missing value then
      — Sun, 19 May 2002 15:21:36
      
dateFormatter’s setDateFormat:"EEE, d MMM yyyy HH:mm:ss"
      
set aDate to dateFormatter’s dateFromString:RFC822String
    end if
    
    
if aDate is equal to missing value then
      — Sun, 19 May 2002 15:21:36
      
dateFormatter’s setDateFormat:"EEE, d MMM yyyy HH:mm"
      
set aDate to dateFormatter’s dateFromString:RFC822String
    end if
  else
    if aDate is equal to missing value then
      — 19 May 2002 15:21:36 GMT
      
dateFormatter’s setDateFormat:"d MMM yyyy HH:mm:ss zzz"
      
set aDate to dateFormatter’s dateFromString:RFC822String
    end if
    
    
if aDate is equal to missing value then
      — 19 May 2002 15:21 GMT
      
dateFormatter’s setDateFormat:"d MMM yyyy HH:mm zzz"
      
set aDate to dateFormatter’s dateFromString:RFC822String
    end if
    
    
if aDate is equal to missing value then
      — 19 May 2002 15:21:36
      
dateFormatter’s setDateFormat:"d MMM yyyy HH:mm:ss"
      
set aDate to dateFormatter’s dateFromString:RFC822String
    end if
    
    
if aDate is equal to missing value then
      — 19 May 2002 15:21
      
dateFormatter’s setDateFormat:"d MMM yyyy HH:mm"
      
set aDate to dateFormatter’s dateFromString:RFC822String
    end if
    
  end if
  
  
if aDate is equal to missing value then return false
  
return aDate as date
end rfc822StrDecode

★Click Here to Open This Script 

Please follow and like us:

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です