Timestamp synchronization with PowerShell and Python
- julienmesser
- 5 mars 2023
- 2 min de lecture
To train myself with script languages I have been working on some simple scripts to synchronize timestamps in files. The idea is to correct a set of timestamps contained in a source file with the help of a translation table.
Concept
In the following example we see on the left side the original (incorrect) timestamps and on the right side the correction:

The translation table has two entries - it is enough to correct any timestamp in-between with a linear interpolation. For instance the timestamp "8:15" would be corrected to "9:30".
The calculation is explained in the following illustration:

To cover a bigger timespan, the translation table can be extended with additional rows.
Implementation
The implementation in a script consist in iterating and storing the elements of the translation tables in an array. Then the source file is parsed and corrected according to the translation table (according to the timestamp value, we iterate this table with an index).
Since it is straightforward I began the implementation with "powershell" which provides facilities to read files (here my work in progress: https://github.com/axis68/productive-scripts/blob/main/resync-withtimetable.ps1).
Somehow I found the calculation with DateTime laborious and decided to move to python. Chat-GPT managed to convert the script without any problem! Finally I completed the realization in python, this is the result: https://github.com/axis68/productive-scripts/blob/main/resync.py
Some learnings with python
Handling arguments calls with python
Similar to PowerShell python offer means to define and parse the argument calls:
parser = argparse.ArgumentParser(description='What does my program do')
parser.add_argument('-v', '--verbose', action="store_true", help='Example of argument')
args = parser.parse_args()
print(args.verbose)
A convenient help is available from the command line:
PS C:\Users\i02464501\github\productive-scripts> py ./resync.py -?
usage: resync.py [-h] [-v] timetable_file toprocess_file
resync.py: error: the following arguments are required: timetable_file, toprocess_file
fdsdsf
Arrays and hash tables
The use of arrays and hash tables is similar to powershell:
myArray = array[]
myArray.append(value)
print(myArray[index])
myHashtable = { 'key1': value1, 'key2', value2 }
print(myHashtable['key1']
Regular expressions with "re" library
Regex are handled by the integrated library "re":
import re
if (match := re.match(r'REGULAR-EXPRESSION-PATTERN', string_value))
matchedValue = match.group(1).strip()
DateTime operations
The DateTime format is supported natively.
Parsing from / to a string:
datetime.datetime.strptime(date_str, '%H:%M:%S,%f')
newEndTimespan.strftime('%H:%M:%S,%f')[:-3]
The 3 last digits (microseconds) are removed with the instruction [:-3]
As timespans cannot be multiplicated/divided, we convert the value in seconds:
from datetime import timedelta
# Get the value in seconds
valueSeconds = myTimeSpan.total_seconds()
# Convert back to DateTime
convertedTime = timedelta(seconds = valueSeconds)
Conclusion
I hope you had some fun reading this post...
Comments