cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Convert Data Item Timestamp to Different Timezone

Highlighted
Marble

Convert Data Item Timestamp to Different Timezone

The following script takes a parameter of a model name, a device serial number and a data item name, finds the asset location and uses that longitude to determine the current TimeZone.  It then converts the Timezone of the data item timestamp to an Eastern Standard Timezone timestamp.

import groovy.xml.MarkupBuilder

import com.axeda.drm.sdk.Context

import java.util.TimeZone

import com.axeda.drm.sdk.data.*

import com.axeda.drm.sdk.device.*

import com.axeda.common.sdk.jdbc.*;

import net.sf.json.JSONObject

import net.sf.json.JSONArray

import com.axeda.drm.sdk.mobilelocation.MobileLocationFinder

import com.axeda.drm.sdk.mobilelocation.MobileLocation

import com.axeda.drm.sdk.mobilelocation.CurrentMobileLocationFinder

def response

try {

    Context ctx = Context.getUserContext()

    ModelFinder mfinder = new ModelFinder(ctx)

    mfinder.setName(parameters.model_name)

    Model m = mfinder.find()

    DeviceFinder dfinder = new DeviceFinder(ctx)

    dfinder.setModel(m);

    dfinder.setSerialNumber(parameters.device)

    Device d = dfinder.find()

    CurrentMobileLocationFinder cmlFinder = new CurrentMobileLocationFinder(ctx);

    cmlFinder.setDeviceId(d.id.getValue());

    MobileLocation ml = cmlFinder.find();

    def lng = -72.158203125

    if (ml?.lng){

        lng = ml?.lng

    }

    // set boundaries for timezones - longitudes

    def est = setUSTimeZone(-157.95415000000003)

    def tz = setUSTimeZone(lng)

    CurrentDataFinder cdfinder = new CurrentDataFinder(ctx, d)

    DataValue dvalue = cdfinder.find(parameters.data_item_name)

    def adjtime = convertToNewTimeZone(dvalue.getTimestamp(),tz,est)

    def results = JSONObject.fromObject(lat: ml?.lat, lng: ml?.lng, current: [name: dvalue.dataItem.name, time: adjtime.format("MM/dd/yyyy HH:mm"), value: dvalue.asString()]).toString(2)

    response = results

}

catch (Exception e) {

    response = [

                message: "Error: " + e.message

            ]

    response =  JSONObject.fromObject(response).toString(2)

}

return ['Content-Type': 'application/json', 'Cache-Control':'no-cache', 'Content': response]

def setUSTimeZone(lng){

    TimeZone tz

    // set boundaries for US timezones by longitude

    if (lng <= -67.1484375 && lng > -85.517578125){

        tz = TimeZone.getTimeZone("EST");

    }

    else if (lng <= -85.517578125 && lng > -96.591796875){

        tz = TimeZone.getTimeZone("CST");

    }

    else if (lng <= -96.591796875 && lng > -113.90625){

        tz = TimeZone.getTimeZone("MST");

    }

    else if (lng <= -113.90625){

        tz = TimeZone.getTimeZone("PST");

    }

    logger.info(tz)

    return tz

}

public Date convertToNewTimeZone(Date date, TimeZone oldTimeZone, TimeZone newTimeZone){

    long oldDateinMilliSeconds=date.time - oldTimeZone.rawOffset

    // oldtimeZone.rawOffset returns the difference(in milliSeconds) of time in that timezone with the time in GMT

    // date.time returns the milliseconds of the date

    Date dateInGMT=new Date(oldDateinMilliSeconds)

    long convertedDateInMilliSeconds = dateInGMT.time + newTimeZone.rawOffset

    Date convertedDate = new Date(convertedDateInMilliSeconds)

    return convertedDate

}

Tags (2)