Re: Date computation
Re: Date computation
- Subject: Re: Date computation
- From: Joseph Weaks <email@hidden>
- Date: Thu, 13 Jan 2005 02:10:03 -0600
Michelle Steiner wrote on Fri, 7 Jan 2005 01:18:28 -0700:
I've run into a brick wall here. I'm trying to compute the date of the
second Wednesday of the current month, and if the current date is later
than or equal to that date, then compute the date of the second
Wednesday of the following month.
Well, I don't get a Social Security check, but this inspired me to try my hand at a script that calculated today's current proximity to a date more personal to me:
-- Dialog messages for those trying to get pregnant
property tryingMessages : {"Not much point right now. Better save up for at least ", "There is a chance to get pregnant here. Those little guys live awhile, and ovulation should come in ", "Today is probably ovulation day! Get after it.", "It is quite possible to get pregnant today, and for the next ", "No chance today. Next cycle begins in "}
-- Dialog message for those avoiding getting pregnant
property avoidingMessages : {"Sex is perfectly safe today, and for the next ", "There is a chance to get pregnant today. Those little guys live a few days, and ovulation should come in ", "Don't even THINK about it today.", "Danger. Danger. Enter at your own risk. Possibility of getting pregnant lasts another ", "Everything is perfectly safe here. Next cycle begins in "}
property cycleLength : 28 -- Average cycle is 28 days, but normal range is from 24 to 35 days
property cycleDidStart : missing value
property cycleWillStart : missing value
property fertileStart : missing value
property fertileEnd : missing value
property ovulationDay : missing value
property trying : false
property theMessages : {}
-- Assumptions
property spermLife : 4 -- shelf life for sperm
property eggLife : 3 -- shelf life for egg
property ovulationBegins : 14 -- counted as days before next cycle begins
--Ovulation normally begins 14 days before next cycle begins, but can vary ±2
mainScript()
on mainScript()
set today to current date
--check to see if first time run
if cycleDidStart is missing value then
getCycleStart()
setParameters()
else if today > cycleWillStart then --check to see if next cycle start date has passed
set cycleDidStart to cycleWillStart
set theDiff to (today - cycleDidStart) div days
set day of cycleWillStart to (day of cycleWillStart as number) + ((theDiff div cycleLength) * cycleLength)
getCycleStart()
end if
setDates()
--determine what day currently in the cycle
set cycleDay to ((today - cycleDidStart) div days)
if cycleDay < fertileStart then
set theMessage to item 1 of theMessages ¬
& (fertileStart - cycleDay) & " day(s)."
else if cycleDay < ovulationDay then
set theMessage to item 2 of theMessages ¬
& (ovulationDay - cycleDay) & " day(s)."
else if cycleDay = ovulationDay then
set theMessage to item 3 of theMessages
else if cycleDay ≤ fertileEnd then
set theMessage to item 4 of theMessages ¬
& (fertileEnd - cycleDay) & " day(s)."
else
set theMessage to item 5 of theMessages ¬
& (cycleLength - cycleDay) & " day(s)."
end if
set theDialog to display dialog theMessage buttons {"Set Parameters", "OK"} default button 2
if button returned of theDialog is "Set Parameters" then
setParameters()
mainScript()
end if
end mainScript
on setDates()
set ovulationDay to cycleLength - ovulationBegins
set fertileStart to cycleLength - ovulationDay - 2 - spermLife
set fertileEnd to cycleLength - ovulationDay + 2 + eggLife
if trying then
set theMessages to tryingMessages
else
set theMessages to avoidingMessages
end if
copy cycleDidStart to cycleWillStart
set day of cycleWillStart to (day of cycleWillStart as number) + cycleLength
end setDates
on getCycleStart()
if cycleDidStart is not missing value then
set dateString to (month of cycleDidStart as number) & "/" & (day of cycleDidStart) & "/" & (year of cycleDidStart) as string
else
set dateString to "1/1/05"
end if
set theDisplay to display dialog "Enter the date your last period began. Use the format via your system settings, such as 1/21/05" default answer dateString buttons {"OK"} default button 1
try
set cycleDidStart to (date (text returned of the theDisplay))
on error
display dialog "That was not a correct date format." --buttons {"OK"} default button 1
getCycleStart()
end try
end getCycleStart
on setParameters()
display dialog "How many days in your cycle?" default answer "28" buttons {"Enter"} default button 1
try
set cycleLength to (text returned of the result) as integer
on error
display dialog "No. Please enter a number for number of days."
setParameters()
end try
display dialog "Are you trying to get pregnant or avoiding getting pregnant?" buttons {"Trying", "Avoiding"}
if button returned of the result is "Trying" then
set trying to true
else
set trying to false
end if
setDates()
end setParameters
With my Applescript skills, I'm sure I have computational errors... so now if I have a second child, I can blame my poor computer programming skills. :)
Joe
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden