rat-times/scripts/time_entry.gd
2023-03-04 01:12:54 +04:00

78 lines
1.8 KiB
GDScript

## Describes a row in a timesheet
## Has a beginning and an end
class_name TimeEntry
var name := ""
var closed := false
var start_time := TimeStamp.new()
var end_time := TimeStamp.new()
var previous_total := 0
func start_recording() -> TimeEntry:
start_time = start_time.from_current_time()
end_time = end_time.from_current_time()
return self
func update() -> void:
end_time = end_time.from_current_time()
func get_elapsed_seconds() -> int:
var elapsed := end_time.get_difference(start_time)
return elapsed
func get_total_elapsed_seconds() -> int:
var elapsed := get_elapsed_seconds() + previous_total
return elapsed
func get_period() -> String:
var time_in_secs := get_elapsed_seconds()
return TimeEntry.time_to_period(time_in_secs)
func get_total_period() -> String:
var time_in_secs := get_total_elapsed_seconds()
return TimeEntry.time_to_period(time_in_secs)
static func time_to_period(time_in_secs: int) -> String:
var seconds := time_in_secs%60
@warning_ignore("integer_division")
var minutes := (time_in_secs/60)%60
@warning_ignore("integer_division")
var hours := (time_in_secs/60)/60
return "%02d:%02d:%02d" % [hours, minutes, seconds]
func to_csv_line() -> PackedStringArray:
return PackedStringArray([
name,
start_time,
end_time,
str(get_elapsed_seconds()) if closed else tr(Consts.ONGOING)
])
static func is_csv_line_valid(line: PackedStringArray) -> bool:
return line.size() > 3
func from_csv_line(line: PackedStringArray) -> TimeEntry:
name = line[0]
var start_time_string = line[1]
start_time.from_string(start_time_string)
var elapsed_seconds = int(line[3]) if line[3].is_valid_int() else 0
closed = elapsed_seconds > 0
if closed == true:
var end_time_string = line[2]
end_time.from_string(end_time_string)
else:
end_time.from_current_time()
return self