Created
April 6, 2011 22:21
-
-
Save mhulse/906668 to your computer and use it in GitHub Desktop.
Returns a relative date. Language keywords: CSP (Caché Server Page), COS (Caché Objectscript)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<csp:comment>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</csp:comment> | |
<script language="cache" method="timesince" arguments='ts1:%String="", ts2:%String=""' returntype="%String" procedureblock="1"> | |
/* | |
** | |
** Note: dformat/tformat are not currently used. | |
** Read comments below for more info. | |
** | |
** More info: | |
** https://groups.google.com/d/msg/dti-lightning/88j7s18RTEY/bCb1hb93A7IJ | |
** | |
** TODO: Write "timeuntil" method. | |
** | |
*/ | |
//---------------------------------- | |
// Initialize: | |
//---------------------------------- | |
set (return, diff, unit) = "" | |
//---------------------------------- | |
// Now? | |
//---------------------------------- | |
; DATEDIFF has the flexability I am looking for: | |
set:('$length(ts2)) ts2 = $horolog | |
//---------------------------------- | |
// Difference: | |
//---------------------------------- | |
; The nice thing abut "DATEDIFF" is that it accepts multiple date formats. | |
; Docs: http://docs.intersystems.com/cache20081/csp/docbook/DocBook.UI.Page.cls?KEY=RSQL_datediff | |
; Unfortunately, it is slower than other options. | |
; See here for more info: http://snipurl.com/109ria | |
set diff = $SYSTEM.SQL.DATEDIFF("s", ts1, ts2) // "s" = seconds. | |
//---------------------------------- | |
// Calculate date/time: | |
//---------------------------------- | |
// Many ways to skin a cat, I chose this one: | |
// http://stackoverflow.com/questions/2952361/date-time-convert-to-time-since-php | |
if (diff < 60) { | |
set return = $normalize(diff, 0) | |
set unit = "second" | |
} elseif (diff < (60*60)) { | |
set return = $normalize(diff / 60, 0) | |
set unit = "minute" | |
} elseif (diff < (24*60*60)) { | |
set return = $normalize(diff / (60*60), 0) | |
set unit = "hour" | |
} elseif (diff < (30*24*60*60)) { | |
set return = $normalize(diff / (24*60*60), 0) | |
set unit = "day" | |
} elseif (diff < (365*24*60*60)) { | |
set return = $normalize(diff / (30*24*60*60), 0) | |
set unit = "month" | |
} else { | |
set return = $normalize(diff / (365*24*60*60), 0) | |
set unit = "year" | |
} | |
set return = return _ " " _ unit _ $select(return '= 1 : "s", 1:"") | |
//---------------------------------- | |
// Return: | |
//---------------------------------- | |
quit return | |
</script> | |
<csp:comment> | |
Check out the DATEDIFF docs for info on other possible date formats: | |
http://docs.intersystems.com/cache20081/csp/docbook/DocBook.UI.Page.cls?KEY=RSQL_datediff | |
</csp:comment> | |
<csp:comment> | |
<csp:object name="gStory" classname="dt.cms.schema.CMSStory" objid="25802356"> | |
#(..timesince(gStory.publishedToWebDate))# ago | |
</csp:comment> | |
<csp:comment>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</csp:comment> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment