• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: date syntax not working on 10.10.5?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: date syntax not working on 10.10.5?


  • Subject: Re: date syntax not working on 10.10.5?
  • From: Christopher Stone <email@hidden>
  • Date: Wed, 19 Aug 2015 02:41:19 -0500

On Aug 18, 2015, at 14:15, Michael Grant <email@hidden> wrote:
Does this mean I'm going to have to futz around with TIDs just to get reliable date and time handling?
______________________________________________________________________

Hey Michael,

I do sympathize that your long-used (and seemingly simple) method of creating a date has become problematic.

The workaround method you're using will work fine for you on your system with its settings (for now).

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.

If you don't like TIDs then this works:

----------------------------------------------------------------------------
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

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: date syntax not working on 10.10.5?
      • From: Yvan KOENIG <email@hidden>
    • Re: date syntax not working on 10.10.5?
      • From: Michael Grant <email@hidden>
    • Re: date syntax not working on 10.10.5?
      • From: Shane Stanley <email@hidden>
References: 
 >date syntax not working on 10.10.5? (From: Michael Grant <email@hidden>)
 >Re: date syntax not working on 10.10.5? (From: Christopher Stone <email@hidden>)
 >Re: date syntax not working on 10.10.5? (From: Michael Grant <email@hidden>)
 >Re: date syntax not working on 10.10.5? (From: Shane Stanley <email@hidden>)
 >Re: date syntax not working on 10.10.5? (From: Michael Grant <email@hidden>)

  • Prev by Date: Re: How do we Convert PICT to JPEG
  • Next by Date: Re: date syntax not working on 10.10.5?
  • Previous by thread: Re: date syntax not working on 10.10.5?
  • Next by thread: Re: date syntax not working on 10.10.5?
  • Index(es):
    • Date
    • Thread