Stupid time format

Hello,

I’m trying to get the same result as this, but using do shell script, how would I do this?


set now to (time string of (current date)) + 0 * minutes

/mkh

That exact script generates a -1700 error for me: Can’t make “22:05:6” into type number.
Also, there is a formating error when the seconds are less than 10 (no leading zero pad).

If you want to replace (time string of (current date)) with a call to the date command-line program, then try one of these variations:

set dateFormat to "%r" -- 12 hour + am/pm format
set dateFormat to "%T" -- 24 hour format
do shell script "date +" & (quoted form of dateFormat)

If you were intent on doing date arithmetic, you might try this combination of date and shell arithmetic expansion:

set dateFormat to "%r" -- 12 hour + am/pm format
set dateFormat to "%T" -- 24 hour format
set secondsDelta to -(5 + 2 / 3) * minutes
do shell script "date -r $(($(date +%s) + " & (round secondsDelta) & " )) +" & (quoted form of dateFormat)

This relies on the fact that a single second is the time base of both the set of AppleScript time pseudo-constants seconds, minutes, hours, etc. as well as the argument to date’s -r option (seconds since the UNIX epoch). If this were not the case, we would have to do another conversion before inserting the rounded value into (or as part of) the shell’s arithmetic expression.

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari Version 3.1.2 (4525.22)
Operating System: Mac OS X (10.4)

Well, on MY machine


set now to (time string of (current date)) + 0 * minutes

Returns “152336” which is the current time in sec

I don’t want the time as " 15:19:18 "

Hi,

this depends on the international date format settings.
In almost all countries the time string returns hh:mm:ss (colon separated).
The danish system uses a decimal point as separator.

I recommend to do the date math first and then coerce to a string


set now to time string of ((current date) + minutes)

using the shell script version

do shell script "/bin/date +%H%M%S"

you get the current time as string, but you can’t easily do any math

Like Stefan mentioned, that result is sure to be locale-specific. I am still not sure what format you want to achieve.
To what time (in HH:MM:SS format) does “152336” correspond? 152336 is more than a full day’s worth of seconds (there are only 86400 seconds in most days). In what way is “152336” a time in seconds? Does it measure seconds since some event? What would the “time in sec” equivalent of “15:19:18” be? How would one convert from the “152336” format to the “15:19:18” format?

If you want the number of seconds since midnight you could use one of these (probably the first or last, depending on the application):

-- Get seconds since midnight...

-- Use builtin property of dates to get the number
set secsSinceMidnight to time of (current date)

-- Numbers and arithmetic done in AppleScript
set {hours:hr, minutes:mn, seconds:sc} to current date
set _secsSinceMidnight to (hr * 60 + mn) * 60 + sc

-- Numbers from shell, arithmetic done in AppleScript
set {hr, mn, sc} to words of (do shell script "date +'%H %M %S'") -- get HH MM and SS via shell
set __secsSinceMidnight to ((hr as number) * 60 + (mn as number)) * 60 + (sc as number)

-- All computation done in shell. Perl, Python, C, etc. would all probably be be less ugly.
set ___secsSinceMidnight to (do shell script "echo $(($(date +'(10#%H * 60 + 10#%M)* 60 + 10#%S')))") as number

{secsSinceMidnight, _secsSinceMidnight, __secsSinceMidnight, ___secsSinceMidnight}

If you want all the digits crammed together (no punctuation or separator characters), Stefan showed that (+%H%M%S).

mkh uses obviously the danish system which returns “hh.mm.ss” as time string
The unorthodox date math removes the periods

Wow, thats alot of info, thanks alot, i’ll look into this and see if i get my problem sovled… Thx

/mkh

This was what i needed, damm, thx thx thx :smiley:


do shell script "/bin/date +%H%M%S"

Nitpick: Choose a subject that is meaningful to other forum members, not to yourself.

Well, what I’m trying to do is create a sleep script for Last.fm, haven’t found any script for this so…


set tmp to (time string of (current date)) + 30 * minutes -- adds 10 minutes to the sleep event, and saved to a plist on application startup
set now to (time string of (current date)) + 0 * minutes -- to get the current time

-- I then pull out the tmp value set in the plist and add it as 
set sleepLastFm to my readDef("sleepIn", defFile)

if now > sleepLastFm then
  tell application "Last.fm" to quit
else
  return my secondsToMinutes((sleepLastFm - now))
end if

-- This script was found on this forum in some post, don't know the author, but it does the job ok
on secondsToMinutes(mySec)
	set origNUM to mySec
	set numHours to origNUM div hours
	set temp to origNUM mod hours
	set numMinutes to temp div minutes
	set numSeconds to temp mod minutes
	if numHours < 10 then set numHours to "0" & numHours
	if numMinutes < 10 then set numMinutes to "0" & numMinutes
	if numSeconds < 10 then set numSeconds to "0" & numSeconds
	set finalTime to numHours & ":" & numMinutes & ":" & numSeconds as string
end secondsToMinutes


All this is just a part of the script, and I have no problem running it on my own machine, but as soon I try it anywhere else, I get error
Can’t make “23:45:20” into type number

Why even worry about the date format? I don’t think you have to worry about it if you just do your math with dates. The language stuff should take care of itself… I think.
This will write a date to a plist file as a string:

set plistPath to (path to desktop folder as text) & "test"
set sleepTime to (current date) + 30 * minutes

writeDefault(plistPath, "sleepTime", "string", sleepTime as string)

(*=========== SUBROUTINES ===========*)
on writeDefault(prefsFilePath, KeyName, keyType, keyValue)
	set keyType to "-" & keyType as string
	do shell script "/usr/bin/defaults write " & quoted form of POSIX path of prefsFilePath & space & quoted form of KeyName & space & quoted form of keyType & space & quoted form of keyValue
end writeDefault

This will read the plist file date, put it back into a ‘date’ format, then do your date comparison.

set plistPath to (path to desktop folder as text) & "test"
set now to current date
set sleepTime to date (readDefault(plistPath, "sleepTime"))
if now is greater than sleepTime then
	-- quit your program or whatever
else
	set theSecs to sleepTime - now
	set remainingMins to text -2 thru -1 of ("00" & (theSecs div 60) as string)
	set remainingSecs to text -2 thru -1 of ("00" & (theSecs mod 60) as string)
	return remainingMins & ":" & remainingSecs
end if

(*============= SUBROUTINES ==============*)
on readDefault(prefsFilePath, KeyName)
	try
		set theVal to do shell script "/usr/bin/defaults read " & quoted form of POSIX path of prefsFilePath & space & quoted form of KeyName
	on error
		return false
	end try
	return theVal
end readDefault

Hey regulus6633, this looks great, but one question, if I set the value to 120 min and not 30, I think there will be a problem :frowning:

But yes, this is just what I needed…

/mkh

as mentioned above, this date math cannot work, you’re adding the minutes to a string, not to a date object


set tmp to (current date) + 30 * minutes -- adds 10 minutes to the sleep event, and saved to a plist on application startup
set now to (current date) + 0 * minutes -- to get the current time

That’s easily fixed. You can get the hours, minutes, and seconds like so… which you probably already figured out…

set theSecs to sleepTime - now
set remainingHrs to text -2 thru -1 of ("00" & (theSecs div 3600) as string)
set theSecs to theSecs mod 3600
set remainingMins to text -2 thru -1 of ("00" & (theSecs div 60) as string)
set remainingSecs to text -2 thru -1 of ("00" & (theSecs mod 60) as string)
return remainingHrs & ":" & remainingMins & ":" & remainingSecs

Hi Regulus,

just for sports. :wink:

secsToHMS(sleepTime - now)

on secsToHMS(secs)
	tell secs to return my addZero(it div hours) & ":" & my addZero(it mod hours div minutes) & ":" & my addZero(it mod hours mod minutes div 1)
end secsToHMS

on addZero(v)
	return text -2 thru -1 of ("0" & v)
end addZero

Cool Stefan. That’s one thing I never got used to doing i.e. telling a variable and using ‘it’ to refer to it. I have to remember to try that more.

Sorry. I can’t resist plugging my own favourite. :wink:

secsToHMS(sleepTime - now)

on secsToHMS(secs)
	tell (1000000 + secs div hours * 10000 + secs mod hours div minutes * 100 + secs mod minutes) as string to return text 2 thru 3 & ":" & text 4 thru 5 & ":" & text 6 thru 7
end secsToHMS

Oh my, thanks alot for all the help, but I’m gonna give up on this, I hope it will be usefull for other people…

Er… I would try


set now to do shell script "osascript -e \"(time string of (current date)) + 0 * minutes\"" 

This will do whatever it is you want. It of course returns an error for me. But if it works for you… then you should be worry free.

Folks, please let me repeat, this syntax is nonsense

(time string of (current date)) + 0 * minutes

First of all: 0 * minutes = 0
Second of all: You cannot add an integer (0) to a string value, even if the danish system accept this because of its special date format