Skip to content

Commit

Permalink
Updates for Times on Dates
Browse files Browse the repository at this point in the history
  • Loading branch information
dwot committed Jan 4, 2025
1 parent 2f5cc32 commit 94f3af5
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 47 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.12
0.1.13
7 changes: 6 additions & 1 deletion handlers/plant.go
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,12 @@ ORDER BY p.name;`
}

// Parse the date string into time.Time
plant.HarvestDate, err = time.Parse("2006-01-02", harvestDateStr)
//If harvestDateStr contains T it has a time component, otherwise it's just a date, parse it accordingly
if strings.Contains(harvestDateStr, "T") {
plant.HarvestDate, err = time.Parse("2006-01-02T15:04", harvestDateStr)
} else {
plant.HarvestDate, err = time.Parse("2006-01-02", harvestDateStr)
}
if err != nil {
fieldLogger.WithError(err).Error("Failed to parse harvest date")
return nil, err
Expand Down
20 changes: 20 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,26 @@ func main() {
}
return string(a)
},
"formatStringDateTimeLocal": func(t string) string {
parsedTime, err := time.Parse(time.RFC3339, t)
if err != nil {
return "" // Return empty if parsing fails
}
return parsedTime.Format("2006-01-02T15:04")
},
"formatDateTimeLocal": func(t time.Time) string {
return t.Format("2006-01-02T15:04")
},
"toLocalTimeString": func(t time.Time) string {
if err != nil {
return "" // Fallback to the original string if parsing fails
}
return t.In(time.Local).Format("01/02/2006 03:04 PM")
},
"formatDate": func(t time.Time) string {
return t.Format("01/02/2006")
},
"formatDateTime": func(t time.Time) string { return t.Format("01/02/2006 03:04 PM") },
"formatDateISO": func(t time.Time) string {
return t.Format("2006-01-02")
},
Expand Down Expand Up @@ -126,6 +143,9 @@ func main() {
}
return t
},
"now": func() time.Time {
return time.Now()
},
}

// Attach FuncMap and ParseFS
Expand Down
12 changes: 6 additions & 6 deletions web/templates/components/header2.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ <h5 class="modal-title" id="addMultiPlantActivityModalLabel">{{ .lcl.multiple_ac
<!-- Date Picker -->
<div class="mb-3">
<label for="activityMultiDate" class="form-label">{{ .lcl.title_date }}</label>
<input type="date" class="form-control" id="activityMultiDate" required>
<input type="datetime-local" class="form-control" id="activityMultiDate" required value="{{ now | formatDateTimeLocal }}">
</div>

<!-- Plant Selection -->
Expand Down Expand Up @@ -155,13 +155,13 @@ <h5 class="modal-title" id="addMultiPlantActivityModalLabel">{{ .lcl.multiple_ac
const activityMultiDateInput = document.getElementById("activityMultiDate");

// Set default date to today
const setMultiDefaultDate = () => {
const today = new Date().toISOString().split("T")[0];
activityMultiDateInput.value = today;
};
//const setMultiDefaultDate = () => {
// const today = new Date().toISOString().split("T")[0];
// activityMultiDateInput.value = today;
//};

// Set default date when the modal is shown
addMultiPlantActivityModal.addEventListener("show.bs.modal", setMultiDefaultDate);
//addMultiPlantActivityModal.addEventListener("show.bs.modal", setMultiDefaultDate);

form.addEventListener("submit", (e) => {
e.preventDefault();
Expand Down
68 changes: 34 additions & 34 deletions web/templates/pages/plant.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ <h5 class="card-title text-primary">{{ .lcl.plant_details }}</h5>
{{ end }}

<li><strong>{{ .lcl.title_zone }}:</strong> {{ .plant.ZoneName }}</li>
<li><strong>{{ .lcl.start_date }}:</strong> {{ formatDate .plant.StartDT }}</li>
<li><strong>{{ .lcl.start_date }}:</strong> {{ formatDateTime .plant.StartDT }}</li>

<!-- Conditional: If Plant is an Autoflower and not Harvested and not Dead -->
{{ if and (eq .plant.Autoflower true) (ne .plant.Status "Success") (ne .plant.Status "Drying") (ne .plant.Status "Curing") (ne .plant.Status "Dead") }}
Expand All @@ -125,7 +125,7 @@ <h5 class="card-title text-primary">{{ .lcl.plant_details }}</h5>

<!-- Conditional: If Plant is Harvested or Dead -->
{{ if or (eq .plant.Status "Success") (eq .plant.Status "Drying") (eq .plant.Status "Curing") (eq .plant.Status "Dead") }}
<li><strong>{{ .lcl.harvest_date }}:</strong> {{ formatDate .plant.HarvestDate }}</li>
<li><strong>{{ .lcl.harvest_date }}:</strong> {{ formatDateTime .plant.HarvestDate }}</li>
<li><strong>{{ .lcl.harvest_weight }}:</strong> {{ .plant.HarvestWeight }} {{ .lcl.title_grams }}</li>
{{ else }}
<li><strong>{{ .lcl.title_day }}:</strong> {{ .plant.CurrentDay }}</li>
Expand All @@ -138,12 +138,12 @@ <h5 class="card-title text-primary">{{ .lcl.plant_details }}</h5>
{{ end }}

<!-- Optional: Last Watered or Fed -->
{{ if or (ne (formatDate .plant.LastWaterDate) "01/01/1970") (ne (formatDate .plant.LastFeedDate) "01/01/1970") }}
{{ if or (ne (formatDateTime .plant.LastWaterDate) "01/01/1970") (ne (formatDateTime .plant.LastFeedDate) "01/01/1970") }}
<li><strong>{{ .lcl.last_watered_or_fed }}:</strong>
{{ if gt (formatDate .plant.LastWaterDate) (formatDate .plant.LastFeedDate) }}
{{ formatDate .plant.LastWaterDate }} ({{ .lcl.title_watered }})
{{ if gt (formatDateTime .plant.LastWaterDate) (formatDateTime .plant.LastFeedDate) }}
{{ formatDateTime .plant.LastWaterDate }} ({{ .lcl.title_watered }})
{{ else }}
{{ formatDate .plant.LastFeedDate }} ({{ .lcl.title_fed }})
{{ formatDateTime .plant.LastFeedDate }} ({{ .lcl.title_fed }})
{{ end }}
</li>
{{ end }}
Expand Down Expand Up @@ -219,7 +219,7 @@ <h3 class="text-secondary">{{ .lcl.title_sensors }}</h3>
<div class="card-body">
<h5 class="card-title">{{ .Name }}</h5>
<p class="display-6 fw-bold">{{ .Value }} {{ .Unit }}</p>
<small class="text-muted">{{ formatDate .Date }}</small>
<small class="text-muted">{{ toLocalTimeString .Date }}</small>
</div>
</div>
</a>
Expand All @@ -243,7 +243,7 @@ <h3 class="text-secondary">{{ .lcl.status_history }}</h3>
<tbody>
{{ range .plant.StatusHistory }}
<tr class="clickable-row status-row" data-status='{{ json . }}'>
<td>{{ formatDate .Date }}</td>
<td>{{ formatDateTime .Date }}</td>
<td>{{ .Status }}</td>
</tr>
{{ end }}
Expand All @@ -267,7 +267,7 @@ <h3 class="text-secondary">{{ .lcl.title_measurements }}</h3>
<tbody>
{{ range .plant.Measurements }}
<tr class="clickable-row measurement-row" data-measurement='{{ json . }}'>
<td>{{ formatDate .Date }}</td>
<td>{{ formatDateTime .Date }}</td>
<td>{{ .Name }}</td>
<td>{{ .Value }}</td>
</tr>
Expand All @@ -292,7 +292,7 @@ <h3 class="text-secondary">{{ .lcl.title_activities }}</h3>
<tbody>
{{ range .plant.Activities }}
<tr class="clickable-row activity-row" data-activity='{{ json . }}'>
<td>{{ formatDate .Date }}</td>
<td>{{ formatDateTime .Date }}</td>
<td>{{ .Name }}</td>
<td>{{ preview .Note }}</td>
</tr>
Expand Down Expand Up @@ -528,7 +528,7 @@ <h5 class="modal-title" id="changeStatusModalLabel">{{ .lcl.update_plant }}</h5>
<!-- Date Picker -->
<div class="mb-3">
<label for="statusDate" class="form-label">{{ .lcl.effective_date }}</label>
<input type="date" class="form-control" id="statusDate" required>
<input type="datetime-local" class="form-control" id="statusDate" required value="{{ now | formatDateTimeLocal }}">
</div>

<!-- Plant Name -->
Expand All @@ -540,7 +540,7 @@ <h5 class="modal-title" id="changeStatusModalLabel">{{ .lcl.update_plant }}</h5>
<!-- Start Date -->
<div class="mb-3">
<label for="startDate" class="form-label">{{ .lcl.start_date }}</label>
<input type="date" class="form-control" id="startDate" value="{{ .plant.StartDT | formatDateISO }}">
<input type="datetime-local" class="form-control" id="startDate" value="{{ .plant.StartDT | formatDateTimeLocal }}">
</div>

<!-- Plant Description -->
Expand Down Expand Up @@ -658,7 +658,7 @@ <h5 class="modal-title" id="addMeasurementModalLabel">{{ .lcl.add_measurement }}
<!-- Date Picker -->
<div class="mb-3">
<label for="measureDate" class="form-label">{{ .lcl.title_date }}</label>
<input type="date" class="form-control" id="measureDate" required>
<input type="datetime-local" class="form-control" id="measureDate" required value="{{ now | formatDateTimeLocal }}">
</div>

<!-- Submit Button -->
Expand Down Expand Up @@ -697,7 +697,7 @@ <h5 class="modal-title" id="addActivityModalLabel">{{ .lcl.add_activity }}</h5>
<!-- Date Picker -->
<div class="mb-3">
<label for="activityDate" class="form-label">{{ .lcl.title_date }}</label>
<input type="date" class="form-control" id="activityDate" required>
<input type="datetime-local" class="form-control" id="activityDate" required value="{{ now | formatDateTimeLocal }}">
</div>

<!-- Submit Button -->
Expand All @@ -721,7 +721,7 @@ <h5 class="modal-title" id="editStatusModalLabel">{{ .lcl.edit_status }}</h5>
<input type="hidden" id="statusId">
<div class="mb-3">
<label for="editStatusDate" class="form-label">{{ .lcl.title_date }}</label>
<input type="date" class="form-control" id="editStatusDate" required>
<input type="datetime-local" class="form-control" id="editStatusDate" required value="{{ now | formatDateTimeLocal }}">
</div>
{{ if .loggedIn }}
<button type="submit" class="btn btn-primary"><i class="fa-solid fa-floppy-disk"></i> {{ .lcl.save_changes }}</button>
Expand All @@ -746,7 +746,7 @@ <h5 class="modal-title" id="editMeasurementModalLabel">{{ .lcl.edit_measurement
<input type="hidden" id="measurementId">
<div class="mb-3">
<label for="editMeasurementDate" class="form-label">{{ .lcl.title_date }}</label>
<input type="date" class="form-control" id="editMeasurementDate" required>
<input type="datetime-local" class="form-control" id="editMeasurementDate" required value="{{ now | formatDateTimeLocal }}">
</div>
<div class="mb-3">
<label for="editMeasurementValue" class="form-label">{{ .lcl.title_value }}</label>
Expand Down Expand Up @@ -775,7 +775,7 @@ <h5 class="modal-title" id="editActivityModalLabel">{{ .lcl.edit_activity }}</h5
<input type="hidden" id="activityId">
<div class="mb-3">
<label for="editActivityDate" class="form-label">{{ .lcl.title_date }}</label>
<input type="date" class="form-control" id="editActivityDate" required>
<input type="datetime-local" class="form-control" id="editActivityDate" required value="{{ now | formatDateTimeLocal }}">
</div>
<div class="mb-3">
<label for="editActivityType" class="form-label">{{ .lcl.title_type }}</label>
Expand Down Expand Up @@ -806,13 +806,13 @@ <h5 class="modal-title" id="editActivityModalLabel">{{ .lcl.edit_activity }}</h5
const measurementDateInput = document.getElementById("measureDate");

// Set default date to today
const setDefaultDate = () => {
const today = new Date().toISOString().split("T")[0];
measurementDateInput.value = today;
};
//const setDefaultDate = () => {
// const today = new Date().toISOString().split("T")[0];
// measurementDateInput.value = today;
//};

// Set default date when the modal is shown
addMeasurementModal.addEventListener("show.bs.modal", setDefaultDate);
//addMeasurementModal.addEventListener("show.bs.modal", setDefaultDate);

form.addEventListener("submit", (e) => {
e.preventDefault();
Expand Down Expand Up @@ -867,13 +867,13 @@ <h5 class="modal-title" id="editActivityModalLabel">{{ .lcl.edit_activity }}</h5
const activityDateInput = document.getElementById("activityDate");

// Set default date to today
const setDefaultDate = () => {
const today = new Date().toISOString().split("T")[0];
activityDateInput.value = today;
};
//const setDefaultDate = () => {
// const today = new Date().toISOString().split("T")[0];
// activityDateInput.value = today;
//};

// Set default date when the modal is shown
addActivityModal.addEventListener("show.bs.modal", setDefaultDate);
//addActivityModal.addEventListener("show.bs.modal", setDefaultDate);

form.addEventListener("submit", (e) => {
e.preventDefault();
Expand Down Expand Up @@ -936,10 +936,10 @@ <h5 class="modal-title" id="editActivityModalLabel">{{ .lcl.edit_activity }}</h5
const harvestWeight = document.getElementById("harvestWeight");

// Set default date to today
const setDefaultDate = () => {
const today = new Date().toISOString().split("T")[0];
statusDateInput.value = today;
};
//const setDefaultDate = () => {
// const today = new Date().toISOString().split("T")[0];
// statusDateInput.value = today;
//};

// Reset Zone Selection
const resetZoneSelection = () => {
Expand All @@ -963,9 +963,9 @@ <h5 class="modal-title" id="editActivityModalLabel">{{ .lcl.edit_activity }}</h5


// Set default date when the modal is shown
changeStatusModal.addEventListener("show.bs.modal", () => {
setDefaultDate();
});
//changeStatusModal.addEventListener("show.bs.modal", () => {
// setDefaultDate();
//});

// Show/Hide New Zone Input
zoneSelect.addEventListener("change", () => {
Expand Down
10 changes: 5 additions & 5 deletions web/templates/pages/plants.html
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ <h5 class="modal-title" id="addPlantModalLabel">{{ .lcl.add_new_plant }}</h5>
<!-- Date Picker -->
<div class="mb-3">
<label for="startDate" class="form-label">{{ .lcl.title_date }}</label>
<input type="date" class="form-control" id="startDate" required>
<input type="datetime-local" class="form-control" id="startDate" required value="{{ now | formatDateTimeLocal }}">
</div>

<!-- Clone Checkbox -->
Expand Down Expand Up @@ -349,10 +349,10 @@ <h5 class="modal-title" id="addPlantModalLabel">{{ .lcl.add_new_plant }}</h5>
const decrementSeedCount = document.getElementById("decrementSeedCount");

// Set default date to today
const setDefaultDate = () => {
const today = new Date().toISOString().split("T")[0];
startDt.value = today;
};
//const setDefaultDate = () => {
// const today = new Date().toISOString().split("T")[0];
// startDt.value = today;
//};

// Reset Zone Selection
const resetZoneSelection = () => {
Expand Down

0 comments on commit 94f3af5

Please sign in to comment.