add more date/time format methods

This commit is contained in:
2023-02-08 17:01:38 -06:00
parent 0dcc5ef8c5
commit e3c4a3d91d
2 changed files with 86 additions and 20 deletions

View File

@ -25,6 +25,8 @@ package com.kingsrook.qqq.backend.core.actions.values;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.List;
@ -32,7 +34,6 @@ import java.util.Map;
import com.kingsrook.qqq.backend.core.logging.QLogger;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
import com.kingsrook.qqq.backend.core.utils.StringUtils;
import com.kingsrook.qqq.backend.core.utils.ValueUtils;
@ -46,8 +47,10 @@ public class QValueFormatter
{
private static final QLogger LOG = QLogger.getLogger(QValueFormatter.class);
private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd h:mm a");
private static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd h:mm a");
private static DateTimeFormatter dateTimeWithZoneFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd h:mm a z");
private static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private static DateTimeFormatter localTimeFormatter = DateTimeFormatter.ofPattern("h:mm a");
@ -56,23 +59,6 @@ public class QValueFormatter
*******************************************************************************/
public static String formatValue(QFieldMetaData field, Serializable value)
{
if(QFieldType.BOOLEAN.equals(field.getType()))
{
Boolean b = ValueUtils.getValueAsBoolean(value);
if(b == null)
{
return (null);
}
else if(b)
{
return ("Yes");
}
else
{
return ("No");
}
}
return (formatValue(field.getDisplayFormat(), field.getName(), value));
}
@ -102,6 +88,22 @@ public class QValueFormatter
return (null);
}
////////////////////////////////////////////////////////////////////////////////////////////////
// try to apply some type-specific defaults, if we were requested to just format as a string. //
////////////////////////////////////////////////////////////////////////////////////////////////
if("%s".equals(displayFormat))
{
if(value instanceof Boolean b)
{
return formatBoolean(b);
}
if(value instanceof LocalTime lt)
{
return formatLocalTime(lt);
}
}
////////////////////////////////////////////////////////
// if the field has a display format, try to apply it //
////////////////////////////////////////////////////////
@ -168,6 +170,26 @@ public class QValueFormatter
/*******************************************************************************
**
*******************************************************************************/
public static String formatDateTimeWithZone(ZonedDateTime dateTime)
{
return (dateTimeWithZoneFormatter.format(dateTime));
}
/*******************************************************************************
**
*******************************************************************************/
public static String formatLocalTime(LocalTime localTime)
{
return (localTimeFormatter.format(localTime));
}
/*******************************************************************************
** Make a string from a table's recordLabelFormat and fields, for a given record.
*******************************************************************************/
@ -293,4 +315,25 @@ public class QValueFormatter
}
}
/*******************************************************************************
**
*******************************************************************************/
public static String formatBoolean(Boolean b)
{
if(b == null)
{
return (null);
}
else if(b)
{
return ("Yes");
}
else
{
return ("No");
}
}
}

View File

@ -23,6 +23,12 @@ package com.kingsrook.qqq.backend.core.actions.values;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.List;
import com.kingsrook.qqq.backend.core.BaseTest;
@ -76,6 +82,10 @@ class QValueFormatterTest extends BaseTest
assertEquals("Yes", QValueFormatter.formatValue(new QFieldMetaData().withType(QFieldType.BOOLEAN), true));
assertEquals("No", QValueFormatter.formatValue(new QFieldMetaData().withType(QFieldType.BOOLEAN), false));
assertNull(QValueFormatter.formatValue(new QFieldMetaData().withType(QFieldType.TIME), null));
assertEquals("5:00 AM", QValueFormatter.formatValue(new QFieldMetaData().withType(QFieldType.TIME), LocalTime.of(5, 0)));
assertEquals("5:00 PM", QValueFormatter.formatValue(new QFieldMetaData().withType(QFieldType.TIME), LocalTime.of(17, 0)));
//////////////////////////////////////////////////
// this one flows through the exceptional cases //
//////////////////////////////////////////////////
@ -177,4 +187,17 @@ class QValueFormatterTest extends BaseTest
assertEquals("2", records.get(1).getDisplayValue("homeStateId")); // PVS NOT translated by this class.
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testFormatDates()
{
assertEquals("2023-02-01", QValueFormatter.formatDate(LocalDate.of(2023, Month.FEBRUARY, 1)));
assertEquals("2023-02-01 7:15 PM", QValueFormatter.formatDateTime(LocalDateTime.of(2023, Month.FEBRUARY, 1, 19, 15)));
assertEquals("2023-02-01 7:15 PM CST", QValueFormatter.formatDateTimeWithZone(ZonedDateTime.of(LocalDateTime.of(2023, Month.FEBRUARY, 1, 19, 15), ZoneId.of("US/Central"))));
}
}