Converting with the convert_chrome_time() method

The last of our showcased timestamps is the Google Chrome timestamp, which is similar to both of the previously mentioned timestamps. This timestamp is the number of microseconds since the 1601-01-01 00:00:00 epoch. We'll leverage the earlier-defined self.unix_epcoh_offset value to help in conversion. On line 248, we begin to convert the raw timestamp through a series of functions.

First, we convert the timestamp into a float and subtract the 1601 epoch constant. Next, we divide the value by one million to convert the value from microseconds into seconds so that the datetime.datetime.fromtimestamp() method can interpret the value properly. Finally, on line 251, we format converted_time to a string using the strftime() function. On lines 253 through 255, we handle exceptions that may occur from invalid values as seen in previous sections, as follows:

239     def convert_chrome_time(self):
240 """
241 The convert_chrome_time method handles the
242 conversion of timestamps per the Google Chrome
243 timestamp format
244 """
245 # Run Conversion
246 if self.time_type.get() == 'raw':
247 try:
248 dt_val = datetime.datetime.fromtimestamp(
249 (float(self.input_time.get()
250 )-self.epoch_1601)/1000000)
251 self.processed_chrome_time = dt_val.strftime(
252 '%Y-%m-%d %H:%M:%S.%f')
253 except Exception as e:
254 logger.error(str(type(e)) + "," + str(e))
255 self.processed_chrome_time = str(type(e).__name__)

When a formatted value is passed as an input, we must reverse the process. As in our other functions, we convert the input to a datetime object from a string using the duparser.parse() method. Once converted, we calculate the number of seconds by adding the 1601 epoch constant to the total_seconds() method.

This count of seconds is multiplied by one million to convert it into microseconds. Once calculated, we can cast this integer value into a string that will be displayed in our GUI. In the event that any errors arise, we catch them on line 264 through 266 in the same way as with previous methods:

257         elif self.time_type.get() == 'formatted':
258 try:
259 converted_time = duparser.parse(
260 self.input_time.get())
261 chrome_time = (converted_time - self.epoch_1970
262 ).total_seconds()*1000000 + self.epoch_1601
263 self.processed_chrome_time = str(int(chrome_time))
264 except Exception as e:
265 logger.error(str(type(e)) + "," + str(e))
266 self.processed_chrome_time = str(type(e).__name__)