-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimestamp.py
More file actions
74 lines (63 loc) · 2.2 KB
/
timestamp.py
File metadata and controls
74 lines (63 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
Functions for getting and formatting timestamps, for use for creating and naming the .csv data files.
For use with the Python web application for CR1000 data logging.
Author: Liam Eime
Date: 12/12/2023
"""
# Python imports
import os
def format_timestamp(entry: list[str]):
"""
#### Format timestamp string for desired appearance in file
##### Parameters:
- entry: list[str]
- entry from csv file containing timestamp information
##### Returns:
- formatted_timestamp: str
- timestamp from entry formatted for file name
"""
formatted_timestamp = entry[0].replace(':', '.').replace("-", ".").replace(" ", "_")
return formatted_timestamp
def get_initial_timestamp(file_path: str):
"""
#### Get the initial timestamp from file
##### Parameters:
- file_path: str
- string containing the path to the file from which to get the initial timestamp from
##### Returns:
- initial_timestamp: str
- formatted initial timestamp
"""
with open(file_path, 'r') as f:
f.readline() # skip over the header
first_entry = f.readline().strip("\r\n").split(",")
initial_timestamp = format_timestamp(first_entry)
return initial_timestamp
def get_final_timestamp(
file_path: str,
row_pos: int
):
"""
#### Get the final timestamp from file
##### Parameters:
- file_path: str
- string containing the path to the file from which to get the final timestamp from
- row_pos: int
- integer value indicating which row from last in file to get timestamp from, 1 means last row and 2 means second last row.
##### Returns:
- final_timestamp: str
- formatted final timestamp
"""
num_newlines = 0
with open(file_path, 'rb') as f:
try: # catch OSError in case of a one line file
f.seek(-2, os.SEEK_END)
while num_newlines < row_pos:
f.seek(-2, os.SEEK_CUR)
if f.read(1) == b'\n':
num_newlines += 1
except OSError:
f.seek(0)
final_entry = f.readline().decode().strip("\r\n").split(",")
final_timestamp = format_timestamp(final_entry)
return final_timestamp