Skip to content
This repository was archived by the owner on Jul 1, 2024. It is now read-only.
This repository was archived by the owner on Jul 1, 2024. It is now read-only.

Support for milliseconds #8

Description

@JamoCA

Some ISO 8601 strings I was parsing (for a timed race) contained decimal precision and the CastToUTC() and castFromUTC() functions were truncating the time to the nearest second. Here's the changes that I made to support milliseconds. (Is this enough?)

<!--- returns UTC from given date in given TZ, takes DST into account --->
<cffunction name="castToUTC" output="No" access="public" returntype="date">  
    <cfargument name="thisDate" required="yes" type="date">
    <cfargument name="thisTZ" required="no" default="#tzObj.getDefault().ID#">
    <cfscript>
        var timezone=tzObj.getTimeZone(arguments.thisTZ);
        var tYear=javacast("int",Year(arguments.thisDate));
        var tMonth=javacast("int",month(arguments.thisDate)-1); //java months are 0 based
        var tDay=javacast("int",Day(thisDate));
        var tDOW=javacast("int",DayOfWeek(thisDate));   //day of week
        var thisOffset=(timezone.getOffset(1,tYear,tMonth,tDay,tDOW,0)/1000)*-1.00;
        var tMilliseconds = 0;
        if (listlen(listLast(arguments.thisDate,":"),".") IS 2){
            tMilliseconds = val(ListLast(listLast(arguments.thisDate,":"),"."));
        }
        return dateAdd("l", tMilliseconds, dateAdd("s",thisOffset, arguments.thisDate));
    </cfscript>
</cffunction>

<!--- returns date in given TZ from given UTC date, takes DST into account --->
<cffunction name="castFromUTC" output="No" access="public" returntype="date">  
    <cfargument name="thisDate" required="yes" type="date">
    <cfargument name="thisTZ" required="no" default="#tzObj.getDefault().ID#">
    <cfscript>
        var timezone=tzObj.getTimeZone(arguments.thisTZ);
        var tYear=javacast("int",Year(arguments.thisDate));
        var tMonth=javacast("int",month(arguments.thisDate)-1); //java months are 0 based
        var tDay=javacast("int",Day(thisDate));
        var tDOW=javacast("int",DayOfWeek(thisDate));   //day of week
        var thisOffset=timezone.getOffset(1,tYear,tMonth,tDay,tDOW,0)/1000;
        var tMilliseconds = 0;
        if (listlen(listLast(arguments.thisDate,":"),".") IS 2){
            tMilliseconds = val(ListLast(listLast(arguments.thisDate,":"),"."));
        }
        return dateAdd("l", tMilliseconds, dateAdd("s",thisOffset, arguments.thisDate));
    </cfscript>
</cffunction>

OBSERVATION: While testing w/CF2016, I noticed that RFC8601 strings could be passed and ColdFusion now evaluates the string as a valid "date". I passed the same RF8601 string to this function using CF10 and it threw an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions