def format_time(timestamp, utc_offset=0, timezone_name=''): minutes = timestamp.time().minute minutes = 15 * int(minutes / 15) local_timezone = SimpleTimeZone(hours=utc_offset) newtime = timestamp.replace(minute=minutes).astimezone(local_timezone) timestring = Timestamp.to_string(newtime, format_string='%Y-%m-%d %H:%M', include_tz=False) return timestring
def my_format_time(timestamp): minutes = timestamp.time().minute minutes = 15 * int(minutes / 15) local_timezone = SimpleTimeZone(hours=utc_offset) newtime = timestamp.replace(minute=minutes).astimezone(local_timezone) timestring = Timestamp.to_string(newtime, format_string='%Y-%m-%d\n%H:%M {}'.format( timezone_label), include_tz=False) return timestring
def write_trajectory(trajectory, outfile): writer = csv.writer(outfile, delimiter=',') writer.writerow([ '# object_id', 'timestamp', 'longitude', 'latitude', 'altitude' ]) for point in trajectory: latest_row = [ point.object_id, Timestamp.to_string(point.timestamp, include_dst=False), str(point.longitude), str(point.latitude), str(int(point.altitude)) ] writer.writerow(latest_row)
def my_format_time(timestamp): minutes = timestamp.time().minute minutes = 15 * int(minutes / 15) local_timezone = SimpleTimeZone(hours=utc_offset) newtime = timestamp.replace(minute=minutes).astimezone(local_timezone) timestring = Timestamp.to_string( newtime, format_string='%Y-%m-%d\n%H:%M {}'.format(timezone_label), include_tz=False) return timestring
def dictionary_from_trajectory(trajectory): """Returns a dictionary constructed from the given trajectory Args: trajectory: the trajectory to convert into a dictonary representation """ dictionary = {} dictionary['domain'] = trajectory.DOMAIN dictionary['object_id'] = trajectory[0].object_id # set trajectory properties dictionary['trajectory_properties'] = {} for (name, value) in trajectory.properties.items(): if isinstance(value, datetime.datetime): dictionary['trajectory_properties'].update({name: {'type': type(value).__name__, 'value': Timestamp.to_string(value, include_tz=False)}}) else: dictionary['trajectory_properties'].update({name: {'type': type(value).__name__, 'value': value}}) # initialize timestamps and coordinates dictionary['timestamps'] = [] dictionary['coordinates'] = [] # initialize point properties dictionary['point_properties'] = {} for (name, value) in trajectory[0].properties.items(): dictionary['point_properties'].update({name: {'type': type(value).__name__, 'values': []}}) # set timestamps, coordinates and point_properties for i in range(len(trajectory)): dictionary['timestamps'].append(Timestamp.to_string(trajectory[i].timestamp, include_tz=False)) dictionary['coordinates'].append(tuple(trajectory[i])) for (name, value) in trajectory[i].properties.items(): if isinstance(value, datetime.datetime): dictionary['point_properties'][name]['values'].append(Timestamp.to_string(value, include_tz=False)) else: dictionary['point_properties'][name]['values'].append(value) return dictionary
def format_time2(timestamp): hours = timestamp.time().hour minutes = timestamp.time().minute if minutes < 30: minutes = 0 else: minutes = 30 new_timestamp = datetime.datetime(year=my_timestamp.date().year, month=my_timestamp.date().month, day=my_timestamp.date().day, hour=hours, minute=minutes, second=0, tzinfo=timestamp.tzinfo) new_timestamp = new_timestamp.astimezone(pytz.timezone('US/Pacific')) print("format_time2: new timestamp is {}".format(str(new_timestamp))) return Timestamp.to_string(new_timestamp, format_string='%H:%M', include_tz=False)
def format_time1(timestamp): return Timestamp.to_string(timestamp)
def dictionary_from_trajectory(trajectory): """Returns a dictionary constructed from the given trajectory Args: trajectory: the trajectory to convert into a dictonary representation """ dictionary = {} dictionary['domain'] = trajectory.DOMAIN dictionary['object_id'] = trajectory[0].object_id # set trajectory properties dictionary['trajectory_properties'] = {} for (name, value) in trajectory.properties.items(): if isinstance(value, datetime.datetime): dictionary['trajectory_properties'].update({ name: { 'type': type(value).__name__, 'value': Timestamp.to_string(value, include_tz=False) } }) else: # Python 2 has both 'int' and 'long' data types. The # first is your system's ordinary integer; the second is # arbitrary-precision. In Python 3, all integers are of # type 'int' and are of arbitrary precision. if sys.version_info[0] == 2 and type(value) is long: type_name = 'int' else: type_name = type(value).__name__ dictionary['trajectory_properties'].update( {name: { 'type': type_name, 'value': value }}) # initialize timestamps and coordinates dictionary['timestamps'] = [] dictionary['coordinates'] = [] # initialize point properties dictionary['point_properties'] = {} for (name, value) in trajectory[0].properties.items(): # As above -- Python 2 has a 'long' data type that we will # call 'int'. if sys.version_info[0] == 2 and type(value) is long: type_name = 'int' else: type_name = type(value).__name__ dictionary['point_properties'].update( {name: { 'type': type_name, 'values': [] }}) # set timestamps, coordinates and point_properties for i in range(len(trajectory)): dictionary['timestamps'].append( Timestamp.to_string(trajectory[i].timestamp, include_tz=False)) dictionary['coordinates'].append(tuple(trajectory[i])) for (name, value) in trajectory[i].properties.items(): if isinstance(value, datetime.datetime): dictionary['point_properties'][name]['values'].append( Timestamp.to_string(value, include_tz=False)) else: dictionary['point_properties'][name]['values'].append(value) return dictionary