rat-times/scripts/time_entry.gd
2023-03-10 03:25:50 +04:00

93 lines
2.1 KiB
GDScript

## Describes a row in a timesheet
## Has a beginning and an end
class_name TimeEntry
signal end_time_updated
signal closed
var name := ""
var is_closed := false
var start_time := TimeStamp.new()
var end_time := TimeStamp.new()
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()
emit_signal("end_time_updated")
func close() -> void:
update()
is_closed = true
emit_signal("closed")
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]