How can I work with dates and times in combination with MATLAB Production Server R2013b?

1 view (last 30 days)
In my MATLAB code I make use of MATLAB Serial Date Numbers. I now want to deploy this code to a MATLAB Production Server (MPS). The documentation for the Java and .NET client libraries do not show any support for java.util.Date or System.DateTime however. So how do I pass these datatypes from my clients to my functions on the MPS server?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 8 Apr 2014
Generally speaking there are 2 options for passing Dates and Times from MPS client applications to the functions on the MPS server:
1. You can pass in the dates as strings and modify your MATLAB code to first use the DATENUM function to convert these strings to MATLAB Serial Date Numbers.
2. You can convert the Date(Time) to a MATLAB Serial Date Number in the form of a double on the client side without having to make any changes in the MATLAB code.
Java and .NET allow you to easily convert a Date(Time) to the number of milliseconds since January 1 1970 or 100-nanosecond intervals since January 1 0001 respectively. Given that a MATLAB Serial Date Number is the number of days since January 0 0000 (and the time of day is represented in a fraction of a whole day), this can then easily be converted into a MATLAB Serial Date Number. The following example functions illustrate this:
Java
    /**
     * Converts a java.util.Date into a MATLAB Serial Date Number not taking into account timezones and DST. MATLAB
     * Serial Date Numbers are doubles counting the number of days since January 0 0000. The time of day is represented
     * in fractions of a whole day.
     *
     * @param date the date to convert
     * @return the date as MATLAB Serial Date Number
     */
    public static double ToMATLABDateUTC(Date date) {
        // getTime() returns the number of milliseconds since January 1 1970 so to convert to MATLAB Serial Date
        // Number divide by the number of milliseconds in a day and add the datenum of January 1 1970 (=719529).
        return date.getTime() / 1000.0 / 3600.0 / 24.0 + 719529.0;
    }
 
    /**
     * Converts a java.util.Date into a MATLAB Serial Date Number in the local timezone. MATLAB Serial Date Number are
     * doubles counting the number of days since January 0 0000. The time of day is represented in fractions of a whole
     * day.
     *
     * @param date the date to convert
     * @return the date as MATLAB Serial Date Number
     */
    public static double ToMATLABDate(Date date) {
        // Use the same procedure as in ToMATLABDateUTC only first add the timezone and DST offset to the date.
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        return (date.getTime() + cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / 1000.0 / 3600.0 / 24.0 + 719529.0;
    }
.NET
/// <summary>
/// Converts a DateTime into a MATLAB Serial Date Number. The DateTime is first converted to UTC. MATLAB
/// Serial Date Numbers are doubles counting the number of days since January 0 0000. The time of day is
/// represented in fractions of a whole day.
/// </summary>
/// <param name="date">the date to convert</param>
/// <returns>the date as MATLAB Serial Date Number</returns>
static double ToMATLABDateUTC(DateTime date)
{
// The Ticks property of a DateTime represents the number of 100-nanosecond intervals since January 1 0001.
// So to conver to MATLAB Serial Date Number divide by the number of 100-nanosecond intervals in a day and
// add the datenum of January 1 0001 (=367)
return date.ToUniversalTime().Ticks / 10000000.0 / 3600.0 / 24.0 + 367.0;
}
/// <summary>
/// Converts a DateTime into a MATLAB Serial Date Number. The DateTime is kept in local time. MATLAB
/// Serial Date Numbers are doubles counting the number of days since January 0 0000. The time of day is
/// represented in fractions of a whole day.
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
static double ToMATLABDate(DateTime date)
{
// Use the same procedure as in ToMATLABDateUTC but without conversion to UTC.
return date.Ticks / 10000000.0 / 3600.0 / 24.0 + 367.0;
}
 
Note that instead of doing to full conversion from intervals since January 1 1970/0001 to number of days since January 0 0000 on the client side you can also choose to do part on the client side; for example only use getTime() or Ticks in the client and then divide by number of intervals in a day and correct for the offset in your MATLAB code. This may be easier when working with large vectors or matrices of dates as MATLAB is very good at matrix processing.

More Answers (0)

Tags

No tags entered yet.

Products


Release

R2013b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!