rat-times/scripts/time_entry.gd

104 lines
2.3 KiB
GDScript3
Raw Normal View History

2023-03-03 21:12:54 +00:00
## Describes a row in a timesheet
## Has a beginning and an end
class_name TimeEntry
2023-03-09 23:25:50 +00:00
2023-03-03 21:12:54 +00:00
var name := ""
2023-03-09 23:25:50 +00:00
var is_closed := false
2023-03-03 21:12:54 +00:00
var start_time := TimeStamp.new()
var end_time := TimeStamp.new()
func start_recording() -> TimeEntry:
2023-04-22 01:08:05 +00:00
# warning-ignore:return_value_discarded
start_time.from_current_time()
# warning-ignore:return_value_discarded
end_time.from_current_time()
2023-03-03 21:12:54 +00:00
return self
func update() -> void:
2023-04-22 01:08:05 +00:00
# warning-ignore:return_value_discarded
end_time.from_current_time()
2023-03-03 21:12:54 +00:00
2023-03-09 23:25:50 +00:00
func close() -> void:
update()
is_closed = true
2023-03-03 21:12:54 +00:00
2023-03-09 23:25:50 +00:00
func get_elapsed_seconds() -> int:
var elapsed := end_time.get_difference(start_time)
2023-03-03 21:12:54 +00:00
return elapsed
func get_period() -> String:
var time_in_secs := get_elapsed_seconds()
2023-03-09 20:26:57 +00:00
return time_to_period(time_in_secs)
2023-03-03 21:12:54 +00:00
static func time_to_period(time_in_secs: int) -> String:
2023-03-09 20:26:57 +00:00
# warning-ignore:integer_division
2023-03-03 21:12:54 +00:00
var seconds := time_in_secs%60
2023-03-09 20:26:57 +00:00
# warning-ignore:integer_division
2023-03-03 21:12:54 +00:00
var minutes := (time_in_secs/60)%60
2023-03-09 20:26:57 +00:00
# warning-ignore:integer_division
2023-03-03 21:12:54 +00:00
var hours := (time_in_secs/60)/60
return "%02d:%02d:%02d" % [hours, minutes, seconds]
2023-03-09 23:25:50 +00:00
2023-04-25 22:19:35 +00:00
static func period_to_time(period_string: String) -> int:
var period := period_string.split(":")
if period.size() < 3:
return -1
var hours := int(period[0])
var minutes := int(period[1])
var seconds := int(period[2])
var time := seconds + (minutes * 60) + (hours * 60 * 60)
return time
2023-03-09 20:26:57 +00:00
func to_csv_line() -> PoolStringArray:
return PoolStringArray([
2023-03-03 21:12:54 +00:00
name,
start_time,
end_time,
2023-03-09 23:25:50 +00:00
str(get_elapsed_seconds()) if is_closed else tr(Consts.ONGOING)
2023-03-03 21:12:54 +00:00
])
2023-03-09 23:25:50 +00:00
2023-03-09 20:26:57 +00:00
static func is_csv_line_valid(line: PoolStringArray) -> bool:
2023-03-03 21:12:54 +00:00
return line.size() > 3
2023-03-09 20:26:57 +00:00
func from_csv_line(line: PoolStringArray) -> TimeEntry:
2023-03-03 21:12:54 +00:00
name = line[0]
var start_time_string = line[1]
2023-03-09 20:26:57 +00:00
# warning-ignore:return_value_discarded
2023-03-03 21:12:54 +00:00
start_time.from_string(start_time_string)
2023-03-09 20:26:57 +00:00
var elapsed_seconds = int(line[3]) if line[3].is_valid_integer() else 0
2023-03-09 23:25:50 +00:00
is_closed = elapsed_seconds > 0
2023-03-03 21:12:54 +00:00
2023-03-09 23:25:50 +00:00
if is_closed == true:
2023-03-03 21:12:54 +00:00
var end_time_string = line[2]
2023-03-09 20:26:57 +00:00
# warning-ignore:return_value_discarded
2023-03-03 21:12:54 +00:00
end_time.from_string(end_time_string)
else:
2023-03-09 20:26:57 +00:00
# warning-ignore:return_value_discarded
2023-03-03 21:12:54 +00:00
end_time.from_current_time()
return self
2023-03-09 23:25:50 +00:00
func to_dict() -> Dictionary:
return {
start_time = start_time,
closed = is_closed,
}
2023-04-22 01:08:05 +00:00
2023-03-04 14:07:03 +00:00
func _to_string() -> String:
2023-03-09 23:25:50 +00:00
return "%s\t%s\t%s"%[name, tr(Consts.ONGOING) if is_closed == false else end_time.to_string(), start_time]