Skip to content

Instantly share code, notes, and snippets.

@roryl
Last active April 5, 2016 17:13
Show Gist options
  • Save roryl/6a9839d7d9cbd52636afc35844e1aa3f to your computer and use it in GitHub Desktop.
Save roryl/6a9839d7d9cbd52636afc35844e1aa3f to your computer and use it in GitHub Desktop.
Lucee Date Examples
component {
this.timezone = "UTC";
}
<cfscript>
myDate = createDate(2016, 4, 5);
writeDump(myDate);
</cfscript>
<cfscript>
myDate = createDateTime(2016, 4, 5, 0, 0, 0);
writeDump(myDate);
</cfscript>
<cfscript>
myDate = createDate(2016, 4, 5);
myDate2 = createDate(2016, 4, 7);
otherDate = createDate(2016, 4, 5);
echo("myDate should be before myDate2: #myDate.compare(myDate2)# <br />");
echo("myDate2 should be after myDate: #myDate2.compare(myDate)# <br />");
echo("myDate should be equal to otherDate: #myDate.compare(otherDate)# <br />");
</cfscript>
<cfscript>
setTimezone("UTC");
params = [
{sqltype:'timestamp', value:now()}
];
writeDump(params);
writeDump(dateTimeFormat(now(), "medium"));
//Clear out any previous entries in the table
query name="truncate" datasource="luceebook" {
echo("truncate dates");
}
// //Insert a record to save our date
query name="date" datasource="luceebook" params="#params#"{
// echo("SET @@session.time_zone='+00:00';");
echo("INSERT INTO dates (date) values (?)");
}
//Insert a record to save our date
// query name="date" datasource="luceebook" {
// // echo("SET @@session.time_zone='+00:00';");
// echo("INSERT INTO dates (date) values ({ts '2016-04-05 15:55:31'})");
// }
// query name="set" datasource="luceebook" {
// echo("SET @@session.time_zone='+00:00';");
// }
//Query the record, it should return in UTC
query name="get" datasource="luceebook" {
// echo("SET @@session.time_zone='+00:00';");
echo("SELECT * FROM dates LIMIT 0,1");
}
writeDump(get);
writeDump(get.date[1]);
writeDump(dateTimeFormat(get.date[1], "medium", "PST")); //But we can output into any other time zone
writeDump(dateTimeFormat(get.timestamp[1], "medium", "PST")); //But we can output into any other time zone
setting showdebugoutput="true";
</cfscript>
USE luceebook;
CREATE TABLE `dates` (
`date` DATETIME NULL DEFAULT NULL,
`timestamp` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP
)
/**
* My xUnit Test
*/
component extends="testbox.system.BaseSpec"{
/*********************************** LIFE CYCLE Methods ***********************************/
// executes before all test cases
function beforeTests(){
}
// executes after all test cases
function afterTests(){
}
// executes before every test case
function setup( currentMethod ){
}
// executes after every test case
function teardown( currentMethod ){
}
/*********************************** TEST CASES BELOW ***********************************/
// Remember that test cases MUST start or end with the keyword 'test'
function checkAllSyntaxTest(){
var files = directoryList("");
for(file IN files){
if(file CONTAINS ".cfm"){
include template="#getFileFromPath(file)#";
}
}
}
function queryTest(){
include template="date_insert.cfm";
}
}
<cfscript>
setTimezone("America/New_York"); //Set a timezone for this request
myDate = now(); //Set the current time to now
writeDump(myDate);
writeDump(dateTimeFormat(myDate, "medium", "PST")); //But we can output into any other time zone
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment