Timestamp
Last modification: Tuesday, February 02, 2010 11:29 pmReturns a string for a date. The first parameter is the date object, the second parameter is an integer value representing the format in which to return the string.
Required parameters
Parameter 1 (date)Date object to be converted into a timestamp.
Parameter 2 (integer)
Specify 1 to return the timestamp for use in log files (with the year being mentioned).
Specify 2 to return the timestamp for use in file names.
Specify 3 to return the timestamp for use in log files (without mention of the year).
Implementation
on timeStamp(_date, _format)-- Get the month and day as integer
set _month to month of _date as integer
set _day to day of _date
-- Get the year
set _year to year of _date as string
-- Get the seconds since midnight
set _time to (time of _date)
-- Get hours, minutes, and seconds
set _hours to _time div (60 * 60)
set _minutes to _time mod (60 * 60) div 60
set _seconds to _time mod 60
if _format is not 3 then
-- Zeropad month value
if _month is less than 10 then
set _month to "0" & (_month as string)
else
set _month to _month as string
end if
end if
-- Zeropad day value
if _day is less than 10 then
if _format is 3 then
set _day to " " & (_day as string)
else
set _day to "0" & (_day as string)
end if
else
set _day to _day as string
end if
-- Zeropad hours value
if _hours is less than 10 then
set _hours to "0" & (_hours as string)
else
set _hours to _hours as string
end if
-- Zeropad minutes value
if _minutes is less than 10 then
set _minutes to "0" & (_minutes as string)
else
set _minutes to _minutes as string
end if
-- Zeropad seconds value
if _seconds is less than 10 then
set _seconds to "0" & (_seconds as string)
else
set _seconds to _seconds as string
end if
if _format is 1 then
-- Return in a format suitable for log files (e.g. 2000-01-28 23:15:59)
return _year & "-" & _month & "-" & _day & " " & _hours & ":" & _minutes & ":" & _seconds
else if _format is 2 then
-- Return in a format suitable for file names (e.g. 2000-01-28_23-15-59)
return _year & "-" & _month & "-" & _day & "_" & _hours & "-" & _minutes & "-" & _seconds
else if _format is 3 then
-- Return in an alternative log file format (e.g. Jan 28 23:15:59)
set _shortMonths to {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
return (item _month of _shortMonths) & " " & _day & " " & _hours & ":" & _minutes & ":" & _seconds
else
return ""
end if
end timeStamp