-- MovieScript "DateLib" --@ Version 2.0.3 --@ Author Luke Wigley luke at meccamedialight com au --@ Created Tuesday, 19th January 2005, 1:09 pm -------------------------------------------------------------------------------------------------------------- --@ DESCRIPTION -- Script for creating a PHP-like global GetDateStr() function that returns a string formatted according to -- the given format string. See http://au.php.net/manual/en/function.date.php for a description -- of the parameter string (though note that not all parameters are implemented) --@NOTES -- Script *MUST* be called 'DateLib' --@VERSION HISTORY -- v.2.0.3 Updated the examples to reflect the new script name -- v.2.0.2 Fixed bug ("M" was returning numeric month and "n" missing) -- v.2.0.1 Fixed bug "d" was returning month, not day (duh) -- v.2.0.0 Basically merged the old Datelib with the DateLib scripts -- v.1.0.1 fixed a mapping bug ("A" was not being mapped to the _aupper transform) -------------------------------------------------------------------------------------------------------------- --- STATIC SCRIPT PROPERTIES -------------------------------------- property __Transforms property __RefDate property __DayList property __MonthList --- GLOBAL FUNCTION ----------------------------------------------- on GetDateStr(paramStr, dateObject) -- This function uses a PHP-like syntax to format a Director dateObject -- If no date is supplied, the current dateObject is used. -- -- Example use: -- -- d = the systemDate -- put GetDateStr("jS of F Y, \a\\t g:i:s a", d) -- "28th of February 2005, at 11:43:37 am" if paramStr.ilk <> #String then return "" -- parameter error if dateObject.ilk <> #Date then dateObject = the systemDate return script("DateLib")._GetDateStr(paramStr, dateObject) end on GetTimeStr (dateParam, format12hr, incSeconds) -- Extract the time from a date object, and return it as a string. --* dateParam is a Director date object in format date(2000, 9, 6) {date,optional - if not supplied, uses current time} --* format12hr: optional parameter to convert the hour from 24 to 12 hr format {boolean, optional} --* incSeconds if true, the seconds are included {boolean, optional} --*returns: string (eg. "12:23 AM") if voidP(dateParam) then dateParam = (the systemDate) t = dateParam.seconds hr = t/3600 if format12hr then dhr = hr mod 12 if hr < 12 then ampm= "am" else ampm= "pm" else dhr = hr end if mins = (t/60) - (hr*60) if mins < 10 then mins = "0"&mins sec = t mod 60 if sec < 10 then sec = "0"&sec str = dhr & ":" & mins if incSeconds then str = str & ":" & sec if format12hr then str = str && ampm return str end on GetTimeList (dateParam, format12hr) -- Extract the time from a date object, and return it as a property list. --* dateParam is a Director date object in format date(2000, 9, 6) {date,optional - if not supplied, uses current time} --*ĉOutput: returns proplist [#Hr: Int, #Min: Int, #Sec: Int] if voidP(dateParam) then dateParam = (the systemDate) t = dateParam.seconds hr = t/3600 if format12hr then dhr = hr mod 12 else dhr = hr mins = (t/60) - (hr*60) sec = t mod 60 return [#hr: dhr, #Min: mins, #sec: sec] end on GetDayWithSuffix (dateParam) -- returns a string which is the English ordinal suffix -- for the day of the month --* dataparam {date}is a Director date object --* returns {string} if voidP(dateParam) then dateParam = (the systemDate) dStr = string(dateParam.day) if the last char of dStr = "2" then if dStr = "12" then return "12th" else return dStr & "nd" else if the last char of dStr = "3" then if dStr = "13" then return "13th" else return dStr & "rd" else if the last char of dStr = "1" then if dStr = "11" then return "11th" else return dStr & "st" else return dStr & "th" end --- PRIVATE METHODS ----------------------------------------------- on _GetDateStr(this, paramStr, dateObject) if voidP(__Transforms) then this.script.__Initialise() mx = paramStr.char.count out = "" i = 0 repeat while i < mx i = i + 1 k = paramStr.char[i] if k = "\" then -- escape the next char if i < mx then i = i + 1 nk = paramStr.char[i] if nk <> "\" then case(nk) of "r", "n": out = out & return "t": out = out & tab otherwise out = out & nk end case else if i < mx then i = i + 1 nk = paramStr.char[i] out = out & nk end if end if else t = __Transforms.getAProp(k) if voidP(t) then out = out & k else out = out & call(t, this, dateObject) end if end repeat return out end on _a (this, d) -- am or pm t = d.seconds hr = t/3600 if hr < 12 then return "am" else return "pm" end on _aupper (this, d) -- AM or PM t = d.seconds hr = t/3600 if hr < 12 then return "AM" else return "PM" end on _dayofMonthNum (this, d) -- day of month (number, with leading zeros) n = d.day if n < 10 then return "0"&n else return string(n) end on _dayOfWeek (this, d) -- return day of week (three letters) currentDayNum = ((d - __RefDate) mod 7) +1 currentDay = __DayList[currentDayNum] return currentDay.char[1..3] end on _MonthName (this, d) MonthNum = d.month currentMonth = __MonthList[MonthNum] return currentMonth end on _MonthNameShort (this, d) MonthNum = d.month currentMonth = __MonthList[MonthNum] return currentMonth.char[1..3] end on _Minutes (this, d) -- Minutes with leading zeros t = d.seconds hr = t/3600 mins = (t/60) - (hr*60) if mins < 10 then return "0"&mins else return string(mins) end on _dayNoZero (this, d) return string(d.day) end on _YearAbbr (this, d) y = string(d.year) mx = y.char.count return y.char[mx-1..mx] end on _Year (this, d) return string(d.year) end on _hr12Zero (this, d) t = d.seconds hr = (t/3600) mod 12 if hr < 10 then return "0"&hr else return string(hr) end on _hr24Zero (this, d) t = d.seconds hr = (t/3600) if hr < 10 then return "0"&hr else return string(hr) end on _hr12NoZero (this, d) t = d.seconds hr = (t/3600) mod 12 return string(hr) end on _hr24NoZero (this, d) t = d.seconds hr = (t/3600) return string(hr) end on _dayOfWeekLong (this, d) currentDayNum = ((d - __RefDate) mod 7) +1 return __DayList[currentDayNum] end on _MonthNumZero (this, d) MonthNum = d.month if MonthNum < 10 then return "0"&MonthNum else return string(MonthNum) end on _MonthNumNoZero (this, d) MonthNum = d.month return string(MonthNum) end on _SecOfHour (this, d) t = d.seconds hr = t/3600 mins = (t/60) - (hr*60) sec = t mod 60 if sec > 9 then return string(sec) else return "0"&sec end on _OrdinalSuffix (this, d) dStr = string(d.day) if the last char of dStr = "2" then if dStr = "12" then return "th" else return "nd" else if the last char of dStr = "3" then if dStr = "13" then return "th" else return "rd" else if the last char of dStr = "1" then if dStr = "11" then return "th" else return "st" else return "th" end on __Initialise (this) if voidP(this.__DayList) then __Transforms = [:] __Transforms["a"] = #_a __Transforms["A"] = #_aupper __Transforms["d"] = #_dayofMonthNum __Transforms["D"] = #_dayOfWeek __Transforms["F"] = #_MonthName __Transforms["h"] = #_hr12Zero __Transforms["H"] = #_hr24Zero __Transforms["g"] = #_hr12NoZero __Transforms["G"] = #_hr24NoZero __Transforms["i"] = #_Minutes __Transforms["j"] = #_dayNoZero __Transforms["l"] = #_dayOfWeekLong __Transforms["M"] = #_MonthNameShort __Transforms["m"] = #_MonthNumZero __Transforms["n"] = #_MonthNumNoZero __Transforms["s"] = #_SecOfHour __Transforms["S"] = #_OrdinalSuffix __Transforms["y"] = #_YearAbbr __Transforms["Y"] = #_Year __RefDate = date(1905,1,2) __DayList = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] __MonthList = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] end if end