rat-times/scripts/time_entry.gd
2023-04-22 03:08:05 +02:00

92 lines
2.0 KiB
GDScript

## Describes a row in a timesheet
## Has a beginning and an end
class_name TimeEntry
var name := ""
var is_closed := false
var start_time := TimeStamp.new()
var end_time := TimeStamp.new()
func start_recording() -> TimeEntry:
# warning-ignore:return_value_discarded
start_time.from_current_time()
# warning-ignore:return_value_discarded
end_time.from_current_time()
return self
func update() -> void:
# warning-ignore:return_value_discarded
end_time.from_current_time()
func close() -> void:
update()
is_closed = true
func get_elapsed_seconds() -> int:
var elapsed := end_time.get_difference(start_time)
return elapsed
func get_period() -> String:
var time_in_secs := get_elapsed_seconds()
return time_to_period(time_in_secs)
static func time_to_period(time_in_secs: int) -> String:
# warning-ignore:integer_division
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() -> PoolStringArray:
return PoolStringArray([
name,
start_time,
end_time,
str(get_elapsed_seconds()) if is_closed else tr(Consts.ONGOING)
])
static func is_csv_line_valid(line: PoolStringArray) -> bool:
return line.size() > 3
func from_csv_line(line: PoolStringArray) -> TimeEntry:
name = line[0]
var start_time_string = line[1]
# warning-ignore:return_value_discarded
start_time.from_string(start_time_string)
var elapsed_seconds = int(line[3]) if line[3].is_valid_integer() else 0
is_closed = elapsed_seconds > 0
if is_closed == true:
var end_time_string = line[2]
# warning-ignore:return_value_discarded
end_time.from_string(end_time_string)
else:
# warning-ignore:return_value_discarded
end_time.from_current_time()
return self
func to_dict() -> Dictionary:
return {
start_time = start_time,
closed = is_closed,
}
func _to_string() -> String:
return "%s\t%s\t%s"%[name, tr(Consts.ONGOING) if is_closed == false else end_time.to_string(), start_time]