Friday, July 30, 2010

#1 2007-10-23 04:26:38 pm

Bruce Phillips
Administrator
Registered: 2004-07-15
Posts: 2647

Date Formatting With Command-Line Tools

PHP:

Applescript:

formatDate("Y-m-d", current date)

on formatDate(theFormat, theDate)
   -- theDate should be an AppleScript date object
   do shell script "/usr/bin/php -r 'echo date($argv[1], strtotime($argv[2]));' " & ¬
       quoted form of theFormat & " " & quoted form of (theDate as Unicode text)
end formatDate

See also: http://php.net/date

Edit: http://php.net/strtotime

The function expects to be given a string containing a US English date format …

Offline

 

#2 2007-10-23 04:41:53 pm

StefanK
Member
From: Sankt Gallen, Switzerland
Registered: 2006-10-21
Posts: 7316
Website

Re: Date Formatting With Command-Line Tools

Hi Bruce,

I like it, but apparently it works only on systems with english date format settings.
With my german date format settings I get the result 1970-01-01


regards

Stefan

Offline

 

#3 2007-10-23 04:45:25 pm

Bruce Phillips
Administrator
Registered: 2004-07-15
Posts: 2647

Re: Date Formatting With Command-Line Tools

Ruby:

Applescript:

formatDate("%Y-%m-%d", current date)

on formatDate(theFormat, theDate)
   -- theDate should be an AppleScript date object
   try
       do shell script "/usr/bin/ruby -e 'require \"parsedate\"; puts Time.local(*ParseDate.parsedate(ARGV[1])).strftime(ARGV[0])' " & ¬
           quoted form of theFormat & " " & quoted form of (theDate as Unicode text)
   on error
       -- The date could not be parsed
       return false
   end try
end formatDate

See also: http://www.ruby-doc.org/core/classes/Time.html#M000297

Edit: This also requires a US English date format.

Offline

 

#4 2007-10-23 04:49:14 pm

StefanK
Member
From: Sankt Gallen, Switzerland
Registered: 2006-10-21
Posts: 7316
Website

Re: Date Formatting With Command-Line Tools

I'm sorry, same problem, I get this error message (with the Ruby version)

-e:1:in `local': no implicit conversion from nil to integer (TypeError)
    from -e:1


regards

Stefan

Offline

 

#5 2007-10-23 04:52:02 pm

Bruce Phillips
Administrator
Registered: 2004-07-15
Posts: 2647

Re: Date Formatting With Command-Line Tools

StefanK wrote:

I like it, but apparently it works only on systems with english date format settings.

Apparently so; I've edited my first post.

How does this work for you?

Applescript:

formatDate("Y-m-d", (current date) as «class isot» as string)

on formatDate(theFormat, theDate)
   -- Example for theDate: "2007-10-23T17:54:23"
   do shell script "/usr/bin/php -r 'echo date($argv[1], strtotime($argv[2]));' " & ¬
       quoted form of theFormat & " " & quoted form of theDate
end formatDate

Offline

 

#6 2007-10-23 05:03:05 pm

StefanK
Member
From: Sankt Gallen, Switzerland
Registered: 2006-10-21
Posts: 7316
Website

Re: Date Formatting With Command-Line Tools

Bruce Phillips wrote:

How does this work for you?

Applescript:

formatDate("Y-m-d", (current date) as «class isot» as string)

on formatDate(theFormat, theDate)
   -- Example for theDate: "2007-10-23T17:54:23"
   do shell script "/usr/bin/php -r 'echo date($argv[1], strtotime($argv[2]));' " & ¬
       quoted form of theFormat & " " & quoted form of theDate
end formatDate

actually it works, but I have an offset of +9 hours in the result (now we have 00:03:00
2007-10-24-09-03-00


regards

Stefan

Offline

 

#7 2007-10-23 05:03:56 pm

Bruce Phillips
Administrator
Registered: 2004-07-15
Posts: 2647

Re: Date Formatting With Command-Line Tools

Ruby with isot:

Applescript:

formatDate("%Y-%m-%d", (current date) as «class isot» as string)

on formatDate(theFormat, theDate)
   -- Example for `theDate`: "2007-10-23T17:54:23"
   try
       do shell script "/usr/bin/ruby -e 'require \"parsedate\"; puts Time.local(*ParseDate.parsedate(ARGV[1])).strftime(ARGV[0])' " & ¬
           quoted form of theFormat & " " & quoted form of theDate
   on error
       -- The date could not be parsed
       return false
   end try
end formatDate

Offline

 

#8 2007-10-23 05:07:38 pm

StefanK
Member
From: Sankt Gallen, Switzerland
Registered: 2006-10-21
Posts: 7316
Website

Re: Date Formatting With Command-Line Tools

Ruby works fine also with H-M-S

Code:

formatDate("%Y-%m-%d-%H-%M-%S", (current date) as «class isot» as string)


regards

Stefan

Offline

 

#9 2007-10-23 05:11:19 pm

Bruce Phillips
Administrator
Registered: 2004-07-15
Posts: 2647

Re: Date Formatting With Command-Line Tools

StefanK wrote:

Ruby works fine also with H-M-S

Good. smile

StefanK wrote:

actually it works, but I have an offset of +9 hours in the result (now we have 00:03:00
2007-10-24-09-03-00

Not good. hmm I actually have a +3 offset here.

Offline

 

#10 2007-10-23 05:45:33 pm

Bruce Phillips
Administrator
Registered: 2004-07-15
Posts: 2647

Re: Date Formatting With Command-Line Tools

Bruce Phillips wrote:

I actually have a +3 offset here.

The offset was gone after I removed the "T" from the isot string.

Offline

 

#11 2007-10-24 08:11:54 am

StefanK
Member
From: Sankt Gallen, Switzerland
Registered: 2006-10-21
Posts: 7316
Website

Re: Date Formatting With Command-Line Tools

This works with the php version

Applescript:


formatDate("Y-m-d_H-i-s", current date)

on formatDate(F, D)
   tell D as «class isot» as string to set theDate to text 1 thru 10 & " " & text 12 thru -1
   do shell script "/usr/bin/php -r 'echo date($argv[1], strtotime($argv[2]));' " & ¬
       quoted form of F & " " & quoted form of theDate
end formatDate


regards

Stefan

Offline

 

#12 2007-10-24 08:35:46 am

Bruce Phillips
Administrator
Registered: 2004-07-15
Posts: 2647

Re: Date Formatting With Command-Line Tools

Bruce Phillips wrote:

Bruce Phillips wrote:

I actually have a +3 offset here.

The offset was gone after I removed the "T" from the isot string.

I don't have this problem with PHP 5.2.4. (Which I mistakenly tested with when I first posted.)

Offline

 

#13 2007-10-24 01:26:53 pm

StefanK
Member
From: Sankt Gallen, Switzerland
Registered: 2006-10-21
Posts: 7316
Website

Re: Date Formatting With Command-Line Tools

PS: After testing both php and ruby verisons, I prefer the php one because the parameters are easier to use (for me)


regards

Stefan

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.249 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)