org-mode : how to calculate dynamic deadlines - emacs

I have a scheduled start date of a task and the effort to complete the task in amount of days,
e.g in the case below N=5, start date is <2016-01-27 Wed>
How can the deadline be automatically calculated based upon scheduled_date+5days ?
such that in the end I have the following:
DEADLINE: <2016-02-03 Wed> SCHEDULED: <2016-01-27 Wed>

The following example calls org-schedule; the user inputs the date/time desired; then the timeshift of 5 days are added to the last inserted timestamp and that value is used programmatically by org-deadline. If the user wishes hours/minutes to appear in the timestamp for the deadline, then set with-hm to t.
[Some additional work would be needed if the users wishes the deadline to have the exact same time of day as the scheduled date. At the present time, the default is midnight for the deadline.]
(defun schedule-me ()
(interactive)
(org-schedule nil)
(let* (
(timeshift 5)
(time
(org-time-from-absolute
(+
timeshift
(org-time-string-to-absolute org-last-inserted-timestamp))))
(with-hm nil)
(fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats)) )
(org-deadline nil (format-time-string fmt time))))

Related

What do the correct arguments for Date dayMonthYearDo: look like in Smalltalk (Pharo / Squeak)

Date dayMonthYearDo: aBlock
"Supply integers for day, month and year to aBlock and return the result"
^ start dayMonthYearDo: aBlock
What should a typical valid block look like for this message?
In this case the comment "Supply integers, etc." means that the argument aBlock will receive three integers as "actual" arguments: day number, month index and year. This means that you will have to create a block with three "formal" arguments, say, day, monthIndex and year as in:
aDate dayMonthYearDo: [:day :monthIndex :year | <your code here>]
The code you write inside <your code here> can refer to the "formal" parameters day, monthIndex and year, much as if it was a method with these three arguments.
This is how blocks generally work in Smalltalk.
Example
aDate
dayMonthYearDo: [:day :monthIndex :year |
monthIndex + day = 2
ifTrue: [Transcript show: 'Happy ' , year asString, '!']]
UPDATE
The example above checks for January 1st by "ingeniously" comparing monthIndex + day with 2. In fact, since both variables are >= 1 the only way to get 2 is when day and monthIndex are both 1, i.e., when the receiver aDate is January 1. A more "serious" approach would look like
(monthIndex = 1 and: [day = 1]) ifTrue: [ <etc> ]
Something like this:
Date today dayMonthYearDo: [:d :m :y| Transcript cr;
show: 'today is the ';
show: d;
show: 'th'
]
today is the 28th
But of course you may do different things that just showing things on transcript

Dates between today and another date

im burning my brains trying to make a function that gives me the ammount of days between todays date and a given date.
possible today function:
today = fmap (formatTime defaultTimeLocale "%Y-%m-%d") getCurrentTime
and thought using diffDays, but wont be able to make it work with a ::Day date
any ideas?
Your formatTime version returns a string, but you want a Day (which looks like your string when you inspect it, but is a different type entirely). Here's one way to write a today function, using utctDay to get a Day out of a UTCTime:
import Data.Time.Calendar
import Data.Time.Clock
today :: IO Day
today = fmap utctDay getCurrentTime
And here's a days-from-today function (which I gave the shorter name daysAway) that uses it:
daysAway :: Day -> IO Integer
daysAway day = fmap (diffDays day) today
If you're always specifying the target as a calendar date, you can do that easily enough:
daysToDate :: Integer -> Int -> Int -> IO Integer
daysToDate year month day = daysAway $ fromGregorian year month day
Given a shorthand function for a commonly-needed relative day:
tomorrow :: IO Day
tomorrow = fmap (addDays 1) today
We can demonstrate the correctness of Annie's Thesis:
ghci> tomorrow >>= daysAway
1

Storing dates in Common Lisp

What's the proper way to store dates in Common Lisp? The closest thing I found to an answer is this, which doesn't really seem to cut it for me.
How about ENCODE-UNIVERSAL-TIME?
(defparameter *my-birth-date* (encode-universal-time 0 0 0 14 2 1984))
If you want to store a date converted to string, you can use the following:
(multiple-value-bind
(s m h d mm y dw dst-p tz) (get-decoded-time)
(format nil "~D\/~D\/~D" date month year))

Lisp: decode-universal-time is void

I'm editing orgmode, a time-management mode for emacs. I'm adding some new time functions.
In my addition i need to determine the day of the week for a given date. I use the following code:
(defun org-day-of-week (day month year)
(nth-value 6 (decode-universal-time (encode-universal-time 0 0 0 day month year 0) 0)))
Executing this code gives me the error:
nth-value: Symbol's function definition is void: decode-universal-time
I'm new to lisp, and can't find any info related to this error. I presume some libraries aren't loaded or available.
Can anyone please shed a light on this?
Regards,
Erwin Vrolijk
Snow.nl
Essentially decode-universal-time is a Common Lisp function. These are not necessarily available in emacs-lisp. Some quick checking in a local emacs indicates that this is the fact.
Something close to your original implementation would be:
(defun org-day-of-week (year month day)
(nth 6
(decode-time
(date-to-time
(format "%d-%02d-%02dT00:00:00" year month day)))))
You should be able to use decode-time instead (maybe in conjuction with date-to-time):
decode-time is a built-in function in `C source code'.
(decode-time &optional specified-time)
Decode a time value as (SEC MINUTE HOUR DAY MONTH YEAR DOW DST ZONE).
The optional specified-time should be a list of (HIGH LOW . IGNORED),
as from `current-time' and `file-attributes', or nil to use the
current time. The obsolete form (HIGH . LOW) is also still accepted.
The list has the following nine members: SEC is an integer between 0
and 60; SEC is 60 for a leap second, which only some operating systems
support. MINUTE is an integer between 0 and 59. HOUR is an integer
between 0 and 23. DAY is an integer between 1 and 31. MONTH is an
integer between 1 and 12. YEAR is an integer indicating the
four-digit year. DOW is the day of week, an integer between 0 and 6,
where 0 is Sunday. DST is t if daylight saving time is in effect,
otherwise nil. ZONE is an integer indicating the number of seconds
east of Greenwich. (Note that Common Lisp has different meanings for
DOW and ZONE.)
A cleaner version of Vatine's answer is:
(defun day-of-week (year month day)
"Return the day of the week given a YEAR MONTH DAY"
(nth 6 (decode-time (encode-time 0 0 0 day month year))))
decode-time and encode-time are documented here.
I haven't found the source of this problem, but found a workaround:
(defun day-of-week (day month year)
"Returns the day of the week as an integer.
Sunday is 0. Works for years after 1752."
(let ((offset '(0 3 2 5 0 3 5 1 4 6 2 4)))
(when (< month 3)
(decf year 1))
(mod
(truncate (+ year
(/ year 4)
(/ (- year)
100)
(/ year 400)
(nth (1- month) offset)
day
-1))
7)))

Unix gettimeofday() - compatible algorithm for determining week within month?

If I've got a time_t value from gettimeofday() or compatible in a Unix environment (e.g., Linux, BSD), is there a compact algorithm available that would be able to tell me the corresponding week number within the month?
Ideally the return value would work in similar to the way %W behaves in strftime() , except giving the week within the month rather than the week within the year.
I think Java has a W formatting token that does something more or less like what I'm asking.
[Everything below written after answers were posted by David Nehme, Branan, and Sparr.]
I realized that to return this result in a similar way to %W, we want to count the number of Mondays that have occurred in the month so far. If that number is zero, then 0 should be returned.
Thanks to David Nehme and Branan in particular for their solutions which started things on the right track. The bit of code returning [using Branan's variable names] ((ts->mday - 1) / 7) tells the number of complete weeks that have occurred before the current day.
However, if we're counting the number of Mondays that have occurred so far, then we want to count the number of integral weeks, including today, then consider if the fractional week left over also contains any Mondays.
To figure out whether the fractional week left after taking out the whole weeks contains a Monday, we need to consider ts->mday % 7 and compare it to the day of the week, ts->wday. This is easy to see if you write out the combinations, but if we insure the day is not Sunday (wday > 0), then anytime ts->wday <= (ts->mday % 7) we need to increment the count of Mondays by 1. This comes from considering the number of days since the start of the month, and whether, based on the current day of the week within the the first fractional week, the fractional week contains a Monday.
So I would rewrite Branan's return statement as follows:
return (ts->tm_mday / 7) + ((ts->tm_wday > 0) && (ts->tm_wday <= (ts->tm_mday % 7)));
If you define the first week to be days 1-7 of the month, the second week days 8-14, ... then the following code will work.
int week_of_month( const time_t *my_time)
{
struct tm *timeinfo;
timeinfo =localtime(my_time);
return 1 + (timeinfo->tm_mday-1) / 7;
}
Assuming your first week is week 1:
int getWeekOfMonth()
{
time_t my_time;
struct tm *ts;
my_time = time(NULL);
ts = localtime(&my_time);
return ((ts->tm_mday -1) / 7) + 1;
}
For 0-index, drop the +1 in the return statement.
Consider this pseudo-code, since I am writing it in mostly C syntax but pretending I can borrow functionality from other languages (string->int assignment, string->time conversion). Adapt or expand for your language of choice.
int week_num_in_month(time_t timestamp) {
int first_weekday_of_month, day_of_month;
day_of_month = strftime(timestamp,"%d");
first_weekday_of_month = strftime(timefstr(strftime(timestamp,"%d/%m/01")),"%w");
return (day_of_month + first_weekday_of_month - 1 ) / 7 + 1;
}
Obviously I am assuming that you want to handle weeks of the month the way the standard time functions handle weeks of the year, as opposed to just days 1-7, 8-13, etc.