I do sympathize that your long-used (and seemingly simple) method of creating a date has become problematic.
Shane is simply saying you cannot count on string-to-date coercion to work the way you want OR be consistent over time, and if you want to manage dates reliably over time you need to work with the date object directly and unambiguously.
You're not futzing with TIDs per se, you're futzing with a string (your input data), therefore you have to use methods that work with a string.
dateFromTimeStr("10:15")
on dateFromTimeStr(timeStr)
tell (current date)
set time to 0
set time to time + ((word 1 of timeStr) * hours) + ((word 2 of timeStr) * minutes)
return it
end tell
end dateFromTimeStr
----------------------------------------------------------------------------
If you don't like using words then let's get really out there and use ASObjC and a regular _expression_.
-------------------------------------------------------------------------------------------
use framework "Foundation"
use scripting additions
set timeStr to "10:15"
set myDate
to dateFromHoursMinutes
(my findPattern
:"\\d+" inString
:timeStr
)
-------------------------------------------------------------------------------------------
on dateFromHoursMinutes(hoursMinutesRecord) # Format "HH:MM" ':' may be any non-number character.
set {_hours, _minutes} to hoursMinutesRecord
tell (current date)
set time to 0
set time to time + (_hours * hours) + (_minutes * minutes)
return it
end tell
end dateFromHoursMinutes
-------------------------------------------------------------------------------------------
on findPattern:thePattern inString:theString
set theResult to {} -- we will add to this
set theNSString to current application's NSString's stringWithString:theString
set theOptions to ((current application's NSRegularExpressionDotMatchesLineSeparators) as integer) + ((current application's NSRegularExpressionAnchorsMatchLines) as integer)
set theRegEx to current application's NSRegularExpression's regularExpressionWithPattern:thePattern options:theOptions |error|:(missing value)
set theFinds to theRegEx's matchesInString:theNSString options:0 range:{location:0, |length|:theNSString's |length|()}
set theRanges to (theFinds's valueForKey:"range") as list -- so we can loop through
repeat with i from 1 to count of items of theRanges
set end of theResult to (theNSString's substringWithRange:(item i of theRanges)) as string
end repeat
return theResult
end findPattern:inString:
-------------------------------------------------------------------------------------------
Of course if you're just wanting a date-time-string and not concerned about fooling with date objects you can use ASObjC to get a formatted date-string and add the time to it.
-------------------------------------------------------------------------------------------
use framework "Foundation"
set theDateTimeStr to my makeDateStr:"MM/dd/YYYY" withFixedTimeStr:"10:15"
on makeDateStr:myDateFormat withFixedTimeStr:myTimeStr
set theNSDateFormatter to current application's NSDateFormatter's alloc()'s init()
theNSDateFormatter's setDateFormat:myDateFormat
theNSDateFormatter's setTimeZone:(current application's NSTimeZone's timeZoneForSecondsFromGMT:0)
return ((theNSDateFormatter's stringFromDate:(current application's NSDate's |date|())) as text) & (space & myTimeStr)
end makeDateStr:withFixedTimeStr:
-------------------------------------------------------------------------------------------
Or you can simply shell-out and do something like this:
-------------------------------------------------------------------------------------------
myDateStr("%Y/%m/%d", "10:15")
on myDateStr(dateStr, timeStr)
return (do shell script "date '+" & dateStr & "'") & space & timeStr
end myDateStr
-------------------------------------------------------------------------------------------
On my system I use the
Satimage.osax AppleScript Extension for date-time-strings:
-------------------------------------------------------------------------------------------
set myTimeStr to "10:15"
set myDateTimeStr to strftime (current date) into "%m/%d/%Y" & space & myTimeStr
-------------------------------------------------------------------------------------------
I do have to admit that long ago when I first started fooling with dates in AppleScript I thought they were nuts.
--
Best Regards,
Chris