Formatting leading zero of time

Hi,

I am trying to get rid of the leading zero on any time that starts similar to 01pm, 02am, 05pm etc.,

My script formats like this currently below:

mm-dd-yy_hourampm
05_06_11_02am



(*
mm-dd-yy_hourampm)
05_06_11_02am
*)
set dateformat to do shell script "date '+%m_%d_%y'"

set thetime to do shell script "date '+%I%p'"

set fulltime to dateformat & "_" & thetime
set fulltime to my changecaseof(fulltime, "lower")
set the clipboard to fulltime



on changecaseof(thistext, thiscase)
	if thiscase is "lower" then
		set the comparisonstring to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		set the sourcestring to "abcdefghijklmnopqrstuvwxyz"
	else
		set the comparisonstring to "abcdefghijklmnopqrstuvwxyz"
		set the sourcestring to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	end if
	set the newtext to ""
	repeat with thisChar in thistext
		set x to the offset of thisChar in the comparisonstring
		if x is not 0 then
			set the newtext to (the newtext & character x of the sourcestring) as string
		else
			set the newtext to (the newtext & thisChar) as string
		end if
	end repeat
	return the newtext
end changecaseof

I would appreciate any pointer in the right direction to get rid of those hours leading zeros.

Thanks for having a look,

Jeff

Model: Macbook Pro
Browser: Firefox 5.0.1
Operating System: Mac OS X (10.6)

You could use PHP to do this for you. PHP’s Date command has much more features than darwin’s date command. REad the date manual @ php.net for all possible values.

set phpCommand to "date_default_timezone_set('Europe/Amsterdam');echo date (\"m_d_y_ga\");"
do shell script "php -E " & quoted form of phpCommand

The subroutine is a case-changer, and I do not want to make it anything else.
I added a bit of code higher up, and did a little optimisation (might be as much faster as neutrinos are faster than light :lol:).

set dateformat to do shell script "date '+%m_%d_%y'"

set thetime to changecaseof(do shell script "date '+%I%p'", "lower")

-- this string is always 4 characters; the 1st one is sometimes zero
-- let's drop it when a zero
if 1st text item of thetime is "0" then
	set thetime to text items 2 thru end of thetime as string -- drop the zero
end if

set fulltime to dateformat & "_" & thetime
set the clipboard to fulltime


on changecaseof(thistext, thiscase)
	if thiscase is "lower" then
		set the comparisonstring to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		set the sourcestring to "abcdefghijklmnopqrstuvwxyz"
	else
		set the comparisonstring to "abcdefghijklmnopqrstuvwxyz"
		set the sourcestring to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	end if
	set the newtext to ""
	repeat with thisChar in thistext
		set x to the offset of thisChar in the comparisonstring
		if x is not 0 then
			set the newtext to (the newtext & character x of the sourcestring) as string
		else
			set the newtext to (the newtext & thisChar) as string
		end if
	end repeat
	return the newtext
end changecaseof

In my example I used a fixed time zone but for a local/dynamic script you can get the current time zone with systemsetup command. The PHP command already change AM/PM to lower case so I removed it. This is a more dynamic version who looks what’s your timezone is.

set currentTimeZone to do shell script "systemsetup getTimeZone | awk '{print $3}'"
set phpCommand to "date_default_timezone_set('" & currentTimeZone & "');echo date (\"m_d_y_ga\");"
do shell script "php -E " & quoted form of phpCommand

Hello Alastor933

You may use this code :


set thetime to do shell script "date '+%I%p'"
if thetime starts with "0" then set thetime to text 2 thru -1 of thetime

if thetime ends with "AM" then
	set thetime to text 1 thru -3 of thetime & "am"
else if thetime ends with "PM" then
	set thetime to text 1 thru -3 of thetime & "pm"
end if

It remove the leading zero and it apply the conversion to lowercase without the call to a generic handler.

Yvan KOENIG (VALLAURIS, France) lundi 26 septembre 2011 14:48:04

Use ‘character’ rather than ‘text item’ here. (‘Text items’ are defined by ‘text item delimiters’.) Better still:

if (thetime begins with "0") then set thetime to text 2 thru -1 of thetime

But unless you need to consider time zones as in DJ Bazzie Wazzie’s script, vanilla date handling is faster, briefer, and easier than multiple shell scripts and corrective handlers:

set {year:y, month:m, day:d, hours:h} to (current date)
tell (y * 10000 + m * 100 + d) as text to set dateformat to text 5 thru 6 & "_" & text 7 thru 8 & "_" & text 3 thru 4
set fulltime to dateformat & ("_" & ((h + 11) mod 12 + 1) & item (((h > 11) as integer) + 1) of {"am", "pm"})

:cool:

Thanks guys.
My question was answered way beyond what I was expecting, but I learned alot.

(PHP was excellent and Nigel’s line


set fulltime to dateformat & ("_" & ((h + 11) mod 12 + 1) & item (((h > 11) as integer) + 1) of {"am", "pm"})

was killer. Still trying to figure how it works exactly.

Thank you.

Jeff

Yes. Sorry. When I said the method was “easier”, I wasn’t thinking it might not be so easy to understand! :lol:

h is the value of the date’s ‘hours’ property: an integer between 0 and 23.
(h + 11) mod 12 + 1 calculates the equivalent number for 12-hour time. The result’s automatically coerced to text (without a leading zero) when it’s concatenated to the preceding text.
item (((h > 11) as integer) + 1) of {“am”, “pm”} chooses the appropriate 12-hour suffix: “am” for values of ‘h’ from 0 to 11, “pm” for values from 12 to 23. (h > 1) tests whether or not ‘h’ is greater than 11 and returns the appropriate boolean result (‘false’ or ‘true’), as integer coerces the boolean to an equivalent number (0 or 1), + 1 increases that number by 1 (1 or 2), and the result’s used to index the relevant item in the suffix list.

I was thinking too: “How’s that even easier?” LOL but The rest of your comments I totally agree, it’s much better than the PHP solution I posted here. In this case the do shell script has too much overhead in it.