Dates - return value from a Pashua dialog

Pashua is a tool for creating native Aqua dialogs simple or complex -----available (Free/Donations) from http://www.bluem.net/en/mac/pashua/

The string “2010-06-07 00:00:00 +0100” is the return value from a Pashua dialog. (Element type date)

I have used the script below (an adaptation of Adam Bell’s script at http://macscripter.net/viewtopic.php?id=29997) to get my desired result.

set PashuaDate to "2010-06-07 00:00:00 +0100"
set MO to {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"}
set yr to text 1 thru 4 of PashuaDate
set mn to item (text 6 thru 7 of PashuaDate as integer) of MO
set dy to text 9 thru 10 of PashuaDate
set theDate to date string of date (dy & "/" & mn & "/" & yr)  ----- "Monday, 7 June, 2010"

So far so good.

My Queries.

  1. Will this work in every timezone ?

  2. Is there a better way to convert the date to the desired format (“Monday, 7 June, 2010”)

For those familiar with Pashua

  1. Will a string of the type “2010-06-07 00:00:00 +0100” always be the return value irrespective of timezone

  2. So far Pashua (2007) has worked flawlessly in scripts on 10.6.3 (Intel iMac). Any downside ?

Would be thrilled to get response.

Val

Model: iMac
AppleScript: Version 2.3 (118)
Browser: Safari 533.16
Operating System: Mac OS X (10.6)

Hi, Val.

  1. The time zone per se shouldn’t make any difference. Your script ignores that information.

  2. ‘date (dy & “/” & mn & “/” & yr)’ only works where the user’s Date and Time preferences are set to expect short dates in day-month-year order. For users in the US, for example, the date produced would be 6th July, not 7th June. Similarly, the ‘date string’ extracted from it will match the user’s preferences, which may or may not include the weekday and/or the commas. (I don’t know how exact you want to be about that.) It’s usually best to start with a compiled date, or the date returned by ‘current date’, and set its properties individually:


set PashuaDate to "2010-06-07 00:00:00 +0100"

tell (current date)
	set its day to 1 -- Overflow precaution.
	set its year to text 1 thru 4 of PashuaDate -- Automatic .
	set its month to text 6 thru 7 of PashuaDate -- . text to integer .
	set its day to text 9 thru 10 of PashuaDate --  . coercions.
	set theDate to its date string -- A date string in the user's preferred format.
end tell

I don’t have Pashua, so I can’t comment on your other two questions.

Here’s a quick and dirty foundation CLI which converts iso dates to a string: isodate2string

the usage is:
isodate2string ‘2010-06-07 00:00:00 +0100’
→ displays the common long date string like Montag, 7. Juni 2010 0:00 Uhr GMT+0100
isodate2string ‘2010-06-07 00:00:00 +0100’ ‘%A, %d. %B %Y’
→ displays a custom string representation according to the rules of strftime()

the result string follows always the current international date format settings.
Timezone is considered.
Don’t forget to escape the arguments!
The code is small and has no real error handling. It checks only the number of arguments.

[code]#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
if (argc > 3){
printf(“usage: isodate2string isodate [output format]\n”);
return 1;
}
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSString *isoDate = [[[NSProcessInfo processInfo] arguments] objectAtIndex:1];
NSString *isoTimeZone = [[isoDate componentsSeparatedByString:@" "] lastObject];
BOOL minus = [[isoTimeZone substringToIndex:1] isEqual:@"-"];
NSInteger GMToffset = [[isoTimeZone substringWithRange:NSMakeRange(1, 2)] integerValue] * 3600 + [[isoTimeZone substringWithRange:NSMakeRange(3, 2)] integerValue];
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate *formatterDate = [inputFormatter dateFromString:isoDate];
NSTimeZone *zone = [NSTimeZone timeZoneForSecondsFromGMT:(minus) ? -GMToffset : GMToffset];
if (argc == 2)
	printf("%s\n", [[formatterDate descriptionWithCalendarFormat:@"%c" timeZone:zone locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]] UTF8String]);
else
	printf("%s\n", [[formatterDate descriptionWithCalendarFormat:[[[NSProcessInfo processInfo] arguments] objectAtIndex:2] timeZone:zone locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]] UTF8String]);
[inputFormatter release];
[pool drain];
return 0;

}[/code]

Nigel, StefanK,

Many thanks for your responses.
Basically, a date string in the user’s preferred format is what I was looking for. Thanks!
As I am relatively new to Applescript StefanK’s code is a little bit over my head at the moment.
Someday…
Anyway, Thanks.

Val

load the CLI (link above) and save it somewhere.
Then use this code, you have to adjust the path and probably the output date format setting


set isoString to "2010-06-07 00:00:00 +0100"
set dateString to do shell script "/path/to/isodate2string " & quoted form of isoString & space & quoted form of "%A, %d. %B %Y"

Nice! Stefan

I have some related questions about offset to gmt and gestalt constants.

I state that I can find the time to gmt easily with some system property.

The real question after having read AppleScriptLangauge Guide is in this context:

The problem is, I can’t seem to get what is a Gestalt value in the Gestalt Manager Reference.

Could you please give me an example, so that I can translate the Gestalt Manager Reference for my self?
Edit
A quick search gave the desired results: This post explained it

Thanks and Best Regards

McUsr

StefanK,

Have done that also. Works a dream.

So as I understand it now, irrespective of time zone the correct date can be converted in the format that I specify via the output date format setting such as %A,%e %B %Y etc.

Many many Thanks

Val

right, look here for all abbrevations

For example


system attribute "sysv"

displays the system version number in this format: 4195 (10.6.3) → 1 * (16 ^ 3) + 0 * (16 ^ 2) + 6 * (16 ^ 1) + 3 * (16 ^ 0)

or to get the processor architecture

set ProcArchitecure to system attribute "sysa" -- 1=68k, 2=PPC, 10=Intel

Thank you very much Stefan!
I got it, and the Gestalt Manager Reference :slight_smile:

Best Regards

McUsr

‘time to GMT’ is a StandardAdditions command. It gives the current offset from GMT of the time zone in use the computer.

I don’t see what Gestalt constants have to do with this thread.

What do you mean by “irrespective of time zone the correct date can be converted”? Do you want the given time zone to be translated into the machine’s, or do you simply want it to be ignored?

The AppleScript scripts above ignore the time zone part of the string. Stephan’s CLI errors (on my machine) if the time zone isn’t included or isn’t in the +/-hhmm format, but produces the same date as the AppleScript if it is, regardless of the time zone and times actually used.

The CLI returns a scripter-preferred format, the (my) AppleScript the user-preferred one.

The CLI result is (apparently) exclusively in English, the AppleScripts’ is in the user’s preferred language for Date and Time.

The CLI takes nearly 20 times as long as the AppleScripts to execute.

:slight_smile: Somewhat bashfully again Nigel

At the time being I didn’t know where to find the time to GMT, the only thing i was sure of was that I would be able to find out that for my self. (Somebody else just did it. :wink:

So I guessed or associated the gestalt values from the system attributes command of the System Events with the "Time to GMT property of the Standard Additions.

It was never any intention by me to spawn a thread in the thread.

I’ll be more careful

Best Regards

McUsr

Nigel asked:

The Pashua date dialog’s return value is say “2010-06-07 00:00:00 +0100”.

My location is Ireland so it’s as in the UK.

What I would like is that I convert the returned value from Pashua to the correct date in the format “Monday, 7 June, 2010” or “Monday, 07 June, 2010” or “Monday, 7th June, 2010” irrespective of the location of the machine. As it is a date for a letter the last one is the more correct but.

(I don’t need the time, but “2010-06-07 00:00:00 +0100” is the format Pashua returns.)

So I suppose Its really a scripter-preferred format that I want returned…

However

Earlier I did say “So I suppose Its really a scripter-preferred format that I want returned.” But then who am I to say what date format a user puts on top of a letter ?

So I have flip-flopped back to Nigel’s date string in the user’s preferred format.

Again Many Thanks to you both for your time,knowledge and patience.

Val

Hi Val,

Re your questions 3 & 4: I’m almost done rewriting a rather lengthy Pashua script which ran fine in Tiger, but less so in Snow Leopard.
The script had 2 problems when it moved to Snow Leopard.

Pashua is summer/wintertime [DST] aware, which I did not understand at the time. This becomes a problem when you save dates from Pashua into a file, and then reopen the file at the other side of a DST “boundary”.

In winter, I get “2010-01-01 00:00:00 +0100” (Netherlands)
In sommer, I get "“2010-06-14 11:46:21 +0200”
Get Pashua dates like so:

do shell script "date '+%F %X %z'"-- the %z adds the time zone as time difference from UTC

My solution: convert Pashua dates to date objects immediately; convert them back before passing them to Pashua. Also store them as date objects.

A date which was changed in a Pashua window now comes back in a different format:
“donderdag 15 juli 2010 07:00:00 Nederland-tijd”,
which is not something I have in System Preferences.
Luckily, this smoothly converts to a date object:
date “donderdag 15 juli 2010 07:00:00 Nederland-tijd” → “donderdag 15 juli 2010 7:00”.
So I added a test to my date conversion routine to deal with this.

Here are my routines:

-----------------------------------------------------------------------
-- Convert a date object to a Pashua date string
-- allows for summer/winter time
-- should work with any system date format
-- parameter:	date object
-- returns:	Pashua date string
-----------------------------------------------------------------------
on convertDateObjectToPashuaDate(dateObject)
	tell dateObject to set {d, m, y} to {it's day, it's month as integer, it's year}
	set timeStr to time string of dateObject
	-- create Pashua style date string
	set text item delimiters to "-"
	set dateStr to {y, m, d} as string
	set text item delimiters to ""
	-- create the Pashua date; shell command adds time zone incl DST
	set dateStr to dateStr & " " & timeStr & " " & (do shell script "date '+%z'") -- see manpage for strftime
	return dateStr
end convertDateObjectToPashuaDate
-----------------------------------------------------------------------------------
-- Convert a Pashua date string to a date object
-- assumes dd-mm-yyyy order for the system date
-- Pashua returns a changed date in the system's full format
-- so there's no error
-- an unchanged date comes back in UTC format
-- which triggers an error, and it's dealt with as text
-- parameter:		Pashua date string
-- returns:		AppleScript date object
-----------------------------------------------------------------------------------
on convertPashuaDateToDateObject(pashuaDate)
	try
		return date pashuaDate -- when it was changed
	on error
		set text item delimiters to " "
		set theDate to text item 1 of pashuaDate
		set timeStr to text item 2 of pashuaDate
		set text item delimiters to "-"
		set yyyy to text item 1 of theDate
		set MM to text item 2 of theDate
		set DD to text item 3 of theDate
		set dateStr to {DD, MM, yyyy} as string
		set text item delimiters to ""
		return date (dateStr & " " & timeStr)
	end try
end convertPashuaDateToDateObject

(I would like to thank all of you scriptmasters that helped me -unknowingly- to solve my dating problems : )

There’s room for improvement, I’m sure…

Thijs

Hi Thijs,

Many thanks for your response.

At the moment I am mainly interested in using the Pashua date dialog as a means of entering a date on the top of a letter in the users preferred format.I have no need for the time string.

The Pashua date dialog -if the default is set to to-day -returns in the format “2010-06-11 00:00:00 +0100” if today is chosen or if the dialog is not touched.

The Pashua date dialog, - if a date other than to-day is chosen (say 25/06/10)- returns “Friday, 25 June, 2010 00:00:00 Ireland Time” on my machine.

At the moment I am using Nigel’s script above adapted to cater for a non-default choice. This seems to do the trick, though as I am relatively new to scripting, I may find out in due course that there are some unintended results-hopefully not!.

-----set pashuaDate to "Friday, 25 June, 2010 00:00:00 Ireland Time" --example of non-default user choice
set pashuaDate to "2010-06-11 00:00:00 +0100"
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "-"
set x to text item 1 of pashuaDate
set AppleScript's text item delimiters to tid
set AppleScript's text item delimiters to " 00:"
set y to text item 1 of pashuaDate
set AppleScript's text item delimiters to tid
if x = year of (current date) as text then
	tell (current date)
		set its day to 1 -- Overflow precaution.
		set its year to text 1 thru 4 of pashuaDate -- Automatic .
		set its month to text 6 thru 7 of pashuaDate -- . text to integer .
		set its day to text 9 thru 10 of pashuaDate --  . coercions.
		set UsersPreferedDate to its date string -- A date string in the user's preferred format.
	end tell
else
	set UsersPreferedDate to y
end if

-------- returns “Friday, 11 June, 2010”
---------returns “Friday, 25 June, 2010” if 25/06/10 is chosen in the date dialog

Val

Hi Val,

In that case the DST stuff shouldn’t cause you any problem.

AppleScript can give you a date-only string directly:

set pashuaDate to "donderdag 15 juli 2010 07:00:00 Nederland-tijd"
set preferredDate to date string of date pashuaDate
-->"donderdag 15 juli 2010"

which of course doesn’t work with Pashua’s UTC format.

So, skipping manipulation of pashuaDate to figure out its format:

try -- is it a changed date?
	set preferredDate to date string of date pashuaDate
on error -- no, it wasn't changed
	tell (current date) -- by Nigel Garvey, http://macscripter.net/viewtopic.php?pid=129037
		set its day to 1 -- Overflow precaution.
		set its year to text 1 thru 4 of pashuaDate -- Automatic .
		set its month to text 6 thru 7 of pashuaDate -- . text to integer .
		set its day to text 9 thru 10 of pashuaDate -- . coercions.
		set preferredDate to its date string -- A date string in the user's preferred format.
	end tell
end try

Like I said, always room for improvement : )
(and Nigel’s code usually improves mine a lot - thanks again, Nigel)

Thijs

Yes. Yes!

And it is.

I got it …Eventually!

Many thanks to all.

Val

And, to be complete, the same routine with time included:

try -- is it a changed date?
	set preferredDate to date pashuaDate
on error -- no, it wasn't changed
	set text item delimiters to " "
	set timeStr to text item 2 of pashuaDate
	set text item delimiters to ""
	tell (current date) -- by Nigel Garvey, http://macscripter.net/viewtopic.php?pid=129037
		set its day to 1 -- Overflow precaution.
		set its year to text 1 thru 4 of pashuaDate -- Automatic .
		set its month to text 6 thru 7 of pashuaDate -- . text to integer .
		set its day to text 9 thru 10 of pashuaDate -- . coercions.
		set preferredDate to date (its date string & " " & timeStr) -- date as date object [TAAZ]
	end tell
end try

Happy scripting!