add time from other days

This commit is contained in:
2019-09-20 10:44:06 +02:00
parent 57b23a9bdb
commit 02b8280cc8
4 changed files with 58 additions and 1 deletions

View File

@ -22,7 +22,7 @@ apply plugin: 'eclipse'
apply plugin: 'nu.studer.jooq' apply plugin: 'nu.studer.jooq'
group = 'jottyfan' group = 'jottyfan'
version = '1.0.5' version = '1.0.6'
description = """timetrack""" description = """timetrack"""

View File

@ -4,7 +4,11 @@ import java.io.Serializable;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.Duration; import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Map; import java.util.Map;
import de.jottyfan.timetrack.db.done.tables.records.TDoneRecord; import de.jottyfan.timetrack.db.done.tables.records.TDoneRecord;
@ -41,6 +45,27 @@ public class DoneBean implements Bean, Serializable, Comparable<DoneBean> {
this.activity = jobMap.get(r.getFkJob()); this.activity = jobMap.get(r.getFkJob());
} }
/**
* set the day of timeFrom and timeUntil, keeping the times
*
* @param day
* the day
*/
public void setDay(Date day) {
if (timeFrom != null) {
LocalDateTime ldt = timeFrom.toLocalDateTime();
LocalDate date = day.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
ldt = ldt.withYear(date.getYear()).withMonth(date.getMonthValue()).withDayOfMonth(date.getDayOfMonth());
timeFrom = Timestamp.from(ldt.toInstant(OffsetDateTime.now().getOffset()));
}
if (timeUntil != null) {
LocalDateTime ldt = timeUntil.toLocalDateTime();
LocalDate date = day.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
ldt = ldt.withYear(date.getYear()).withMonth(date.getMonthValue()).withDayOfMonth(date.getDayOfMonth());
timeUntil = Timestamp.from(ldt.toInstant(OffsetDateTime.now().getOffset()));
}
}
public String getTimeSummary() { public String getTimeSummary() {
StringBuilder buf = new StringBuilder(); StringBuilder buf = new StringBuilder();
if (timeFrom != null) { if (timeFrom != null) {

View File

@ -137,6 +137,7 @@ public class DoneModel implements Model, Serializable {
public boolean insert(JooqFacesContext facesContext) { public boolean insert(JooqFacesContext facesContext) {
try { try {
bean.setDay(day);
new DoneGateway(facesContext).insert(bean); new DoneGateway(facesContext).insert(bean);
return true; return true;
} catch (DataAccessException | ClassNotFoundException | SQLException e) { } catch (DataAccessException | ClassNotFoundException | SQLException e) {

View File

@ -1,8 +1,13 @@
package de.jottyfan.timetrack.moduls.done; package de.jottyfan.timetrack.moduls.done;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Date;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -33,4 +38,30 @@ public class TestDoneBean {
assertEquals(ldt.withHour(1).withMinute(0), new DoneBean().getLocalDateTimeFromHHmm("1")); assertEquals(ldt.withHour(1).withMinute(0), new DoneBean().getLocalDateTimeFromHHmm("1"));
assertEquals(ldt.withHour(5).withMinute(0), new DoneBean().getLocalDateTimeFromHHmm("5")); assertEquals(ldt.withHour(5).withMinute(0), new DoneBean().getLocalDateTimeFromHHmm("5"));
} }
@Test
public void testSetDay() throws ParseException {
String today = new SimpleDateFormat("dd.MM.yyyy").format(new Date());
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
SimpleDateFormat sd = new SimpleDateFormat("dd.MM.yyyy");
DoneBean bean = new DoneBean();
bean.setDay(new Date());
assertNull(bean.getTimeFrom());
assertNull(bean.getTimeUntil());
bean.setTimeFromString("08:00");
Timestamp ts1 = bean.getTimeFrom();
assertEquals(today.concat(" 08:00"), sdf.format(ts1));
bean.setDay(sd.parse("01.01.2001"));
assertEquals("01.01.2001", sd.format(bean.getTimeFrom()));
assertNull(bean.getTimeUntil());
bean.setTimeUntilString("10:00");
Timestamp ts2 = bean.getTimeUntil();
assertEquals(today.concat(" 10:00"), sdf.format(ts2));
bean.setDay(sd.parse("01.01.2001"));
assertEquals("01.01.2001", sd.format(bean.getTimeFrom()));
assertEquals("01.01.2001", sd.format(bean.getTimeUntil()));
}
} }