Sonoma placing odd character within timestamp

I’m running into an issue on Sonoma. Using the code below, the text version of the current date timestamp in the text file contains a ‘?’ character before the AM/PM - this does not happen on previous versions of MacOS. When reading it back in and turning it into a date again, it breaks due to the ? character, and reverts to midnight of the date given, not the correct timestamp.

Example: Thursday, October 5, 2023 at 1:45:57?AM

I’m assuming this is a bug in Sonoma, just not sure if it is related to the current date command or the write command?

on run
set theFile to ((path to desktop folder) as text) & "timestamp.txt"
set theExpiration to ((current date) + 12 * hours) as text

set theContents to theExpiration
my writeFile(theContents, theFile, false) -- text file contains an errant ? file before AM/PM

set theReadFile to (read (theFile as alias))
set theExpiration to date theReadFile -- Does not include the time on Sonoma
end run

on writeFile(this_data, target_file, append_data)
	set the target_file to the target_file as text
	tell application "Finder" to set the open_target_file to open for access file target_file with write permission
	if append_data is false then set eof of the open_target_file to 0
	write this_data to the open_target_file starting at eof
	close access the open_target_file
	return true
on error theerr
		tell application "Finder" to close access file target_file
	end try
	return false
end try

end writeFile

It looks like Apple has decided to use a narrow no-break space before am/pm. This is a Unicode character, and because you’re trying to write it in MacRoman format, it’s getting mangled.

You used to be able to edit the date formats, but that option seems to have been removed from System Settings. Another stupid example of Apple knows best, I’m afraid.

You have several options: replace the character with a search/replace, write as UTF-8 rather than MacRoman, and/or use ASObjC so you have some control over the date format.

1 Like

OK, something like that is what I was afraid of. I am currently using find/replace to work around it, but will look into the other options as a possibility. Thanks for your help!