mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2025-10-06 03:50:02 +02:00
GT-3286 - Updated usage of timestamps in the UI to be consistent
This commit is contained in:
parent
2cafb27574
commit
feaf203365
17 changed files with 130 additions and 165 deletions
|
@ -24,8 +24,20 @@ import java.util.*;
|
|||
import ghidra.util.exception.AssertException;
|
||||
|
||||
public class DateUtils {
|
||||
private static final String DATEFORMAT_STR = "MM/dd/yyyy";
|
||||
private static final String TIMEFORMAT_STR = "h:mm";
|
||||
|
||||
/** Example: Oct 31, 2019 03:24 PM */
|
||||
private static final String DATE_TIME_FORMAT_STRING = "MMM dd, yyyy hh:mm aaa";
|
||||
private static final String DATE_FORMAT_STRING = "MM/dd/yyyy";
|
||||
private static final String TIME_FORMAT_STRING = "h:mm";
|
||||
|
||||
private static final ThreadLocal<SimpleDateFormat> DATE_TIME_FORMAT =
|
||||
ThreadLocal.withInitial(() -> new SimpleDateFormat(DATE_TIME_FORMAT_STRING));
|
||||
|
||||
private static final ThreadLocal<SimpleDateFormat> DATE_FORMAT =
|
||||
ThreadLocal.withInitial(() -> new SimpleDateFormat(DATE_FORMAT_STRING));
|
||||
|
||||
private static final ThreadLocal<SimpleDateFormat> TIME_FORMAT =
|
||||
ThreadLocal.withInitial(() -> new SimpleDateFormat(TIME_FORMAT_STRING));
|
||||
|
||||
public static final long MS_PER_SEC = 1000;
|
||||
public static final long MS_PER_MIN = MS_PER_SEC * 60;
|
||||
|
@ -172,7 +184,7 @@ public class DateUtils {
|
|||
|
||||
public static Date normalizeDate(Date date) {
|
||||
try {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT_STR);
|
||||
SimpleDateFormat sdf = DATE_FORMAT.get();
|
||||
return sdf.parse(sdf.format(date));
|
||||
}
|
||||
catch (ParseException e) {
|
||||
|
@ -201,19 +213,35 @@ public class DateUtils {
|
|||
return dayOfWeek == SATURDAY || dayOfWeek == SUNDAY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the given date into a string. This is in contrast to
|
||||
* {@link #formatDateTimestamp(Date)}, which will also return the time portion of the date.
|
||||
*
|
||||
* @param date the date to format
|
||||
* @return the date string
|
||||
*/
|
||||
public static String formatDate(Date date) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT_STR);
|
||||
return sdf.format(date);
|
||||
return DATE_FORMAT.get().format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current local timezone time-of-day as an HOUR:MIN string.
|
||||
* Formats the given date into a string that contains the date and time. This is in
|
||||
* contrast to {@link #formatDate(Date)}, which only returns a date string.
|
||||
*
|
||||
* @param date the date to format
|
||||
* @return the date and time string
|
||||
*/
|
||||
public static String formatDateTimestamp(Date date) {
|
||||
return DATE_TIME_FORMAT.get().format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current local time zone time-of-day as an HOUR:MIN string.
|
||||
*
|
||||
* @return current time-of-day as "HOUR:MIN"
|
||||
*/
|
||||
public static String getTimeNow() {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(TIMEFORMAT_STR);
|
||||
return sdf.format(new Date());
|
||||
return TIME_FORMAT.get().format(new Date());
|
||||
}
|
||||
|
||||
public static Date getDate(int year, int month, int day) {
|
||||
|
@ -230,7 +258,7 @@ public class DateUtils {
|
|||
int days = 0;
|
||||
while (cal.getTime().compareTo(date2) < 0) {
|
||||
cal.add(Calendar.DAY_OF_MONTH, 1);
|
||||
if (!DateUtils.isWeekend(cal) && !DateUtils.isHoliday(cal)) {
|
||||
if (!isWeekend(cal) && !isHoliday(cal)) {
|
||||
days++;
|
||||
}
|
||||
}
|
||||
|
@ -239,8 +267,8 @@ public class DateUtils {
|
|||
|
||||
/**
|
||||
* Formats a millisecond duration as a English string expressing the number of
|
||||
* hours, minutes and seconds in the duration.
|
||||
* <p>
|
||||
* hours, minutes and seconds in the duration
|
||||
*
|
||||
* @param millis Count of milliseconds of an elapsed duration.
|
||||
* @return String such as "5 hours, 3 mins, 22 secs".
|
||||
*/
|
||||
|
|
|
@ -15,49 +15,41 @@
|
|||
*/
|
||||
package ghidra.util;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import generic.test.AbstractGenericTest;
|
||||
public class DateUtilsTest {
|
||||
|
||||
public class DateUtilsTest extends AbstractGenericTest {
|
||||
|
||||
public DateUtilsTest() {
|
||||
// nada
|
||||
@Test
|
||||
public void testFormatDate() {
|
||||
Date date = new Date(1572896586687L);
|
||||
assertEquals("11/04/2019", DateUtils.formatDate(date));
|
||||
}
|
||||
|
||||
/**
|
||||
* This test was moved here from DateUtils.main()
|
||||
*/
|
||||
//@Test
|
||||
public void testHolidays() {
|
||||
for (int year = 2012; year < 2020; year++) {
|
||||
List<Date> holidays = DateUtils.getHolidays(year);
|
||||
for (Date date : holidays) {
|
||||
System.out.println(DateUtils.formatDate(date));
|
||||
}
|
||||
}
|
||||
@Test
|
||||
public void testFormatDateTime() {
|
||||
Date date = new Date(1572896586687L);
|
||||
assertEquals("Nov 04, 2019 02:43 PM", DateUtils.formatDateTimestamp(date));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatDuration() {
|
||||
Assert.assertEquals("0 secs", DateUtils.formatDuration(100));
|
||||
Assert.assertEquals("0 secs", DateUtils.formatDuration(DateUtils.MS_PER_SEC - 1));
|
||||
Assert.assertEquals("1 secs", DateUtils.formatDuration(DateUtils.MS_PER_SEC));
|
||||
Assert.assertEquals("1 secs", DateUtils.formatDuration(DateUtils.MS_PER_SEC + 1));
|
||||
Assert.assertEquals("59 secs", DateUtils.formatDuration(DateUtils.MS_PER_MIN - 1));
|
||||
Assert.assertEquals("1 mins, 0 secs", DateUtils.formatDuration(DateUtils.MS_PER_MIN));
|
||||
Assert.assertEquals("1 mins, 1 secs",
|
||||
assertEquals("0 secs", DateUtils.formatDuration(100));
|
||||
assertEquals("0 secs", DateUtils.formatDuration(DateUtils.MS_PER_SEC - 1));
|
||||
assertEquals("1 secs", DateUtils.formatDuration(DateUtils.MS_PER_SEC));
|
||||
assertEquals("1 secs", DateUtils.formatDuration(DateUtils.MS_PER_SEC + 1));
|
||||
assertEquals("59 secs", DateUtils.formatDuration(DateUtils.MS_PER_MIN - 1));
|
||||
assertEquals("1 mins, 0 secs", DateUtils.formatDuration(DateUtils.MS_PER_MIN));
|
||||
assertEquals("1 mins, 1 secs",
|
||||
DateUtils.formatDuration(DateUtils.MS_PER_MIN + DateUtils.MS_PER_SEC));
|
||||
Assert.assertEquals("23 hours, 59 mins, 59 secs",
|
||||
assertEquals("23 hours, 59 mins, 59 secs",
|
||||
DateUtils.formatDuration(DateUtils.MS_PER_DAY - 1));
|
||||
Assert.assertEquals("1 days, 0 hours, 0 mins, 0 secs",
|
||||
assertEquals("1 days, 0 hours, 0 mins, 0 secs",
|
||||
DateUtils.formatDuration(DateUtils.MS_PER_DAY));
|
||||
Assert.assertEquals("1 days, 0 hours, 0 mins, 0 secs",
|
||||
assertEquals("1 days, 0 hours, 0 mins, 0 secs",
|
||||
DateUtils.formatDuration(DateUtils.MS_PER_DAY + 1));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue