Skip to content

Commit

Permalink
Add omrstr_ftime_ex()
Browse files Browse the repository at this point in the history
* like omrstr_ftime(), with a flag for requesting UTC formatted time
* support '%a' format specifier
* simplify implementation for Windows
* add new tests
* clean up and remove what is now dead code

Signed-off-by: Keith W. Campbell <keithc@ca.ibm.com>
  • Loading branch information
keithc-ca committed Apr 21, 2021
1 parent 840473a commit 42b04a6
Show file tree
Hide file tree
Showing 5 changed files with 336 additions and 658 deletions.
32 changes: 19 additions & 13 deletions fvtest/porttest/omrstrTest.cpp
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 1991, 2019 IBM Corp. and others
* Copyright (c) 1991, 2021 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -62,7 +62,6 @@
/* 1139952606740 -> 2006/02/14 16:30:06 */
#endif


/**
* @internal
* @typdef
Expand Down Expand Up @@ -258,16 +257,19 @@ test_omrstr_vprintfNulChar(struct OMRPortLibrary *portLibrary, const char *testN
* @param[in] bufLen The length of buf
* @param[in] format Specifies the format of the time that will be written to buf
* @param[in] timeMillis The time to format in ms
* @param[in] expectedBuf The value that is to be expected following the call to omrstr_ftime
* @param[in] expectedBuf The value that is to be expected following the call to omrstr_ftime_ex
* @param[in] flags argument for omrstr_ftime_ex
*
* @return TEST_PASS if buf and expectedBuf are identical, TEST_FAIL if not
*/
static void
test_omrstr_ftime(struct OMRPortLibrary *portLibrary, const char *testName, char *buf, uintptr_t bufLen, const char *format, I_64 timeMillis, const char *expectedBuf)
test_omrstr_ftime(struct OMRPortLibrary *portLibrary, const char *testName, char *buf, uintptr_t bufLen,
const char *format, int64_t timeMillis, uint32_t flags, const char *expectedBuf)
{
OMRPORT_ACCESS_FROM_OMRPORT(portLibrary);

omrstr_ftime(buf, bufLen, format, timeMillis);
omrstr_ftime_ex(buf, bufLen, format, timeMillis, flags);

if (0 != strncmp(buf, expectedBuf, bufLen)) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "Expected \"%s\", Got \"%s\"\n", expectedBuf, buf);
}
Expand Down Expand Up @@ -501,23 +503,27 @@ TEST(PortStrTest, str_test3)
const char *testName = "omrstr_test3";
char buf[J9STR_BUFFER_SIZE];
char expected[J9STR_BUFFER_SIZE];
uint64_t timeMillis;
uintptr_t ret;
uint64_t timeMillis = 0;
uintptr_t ret = 0;

reportTestEntry(OMRPORTLIB, testName);

/* First test: The epoch */
portTestEnv->log("\t This test could fail if you abut the international dateline (westside of dateline)\n");
timeMillis = 0;
strncpy(expected, "1970 01 Jan 01 XX:YY:00", J9STR_BUFFER_SIZE);
test_omrstr_ftime(OMRPORTLIB, testName, buf, J9STR_BUFFER_SIZE, "%Y %m %b %d XX:YY:%S", timeMillis, expected);

test_omrstr_ftime(OMRPORTLIB, testName, buf, J9STR_BUFFER_SIZE, "%Y %m %b %d XX:YY:%S", timeMillis, OMRSTR_FTIME_FLAG_LOCAL, expected);

strncpy(expected, "1970 01 Jan 01 00:00:00", J9STR_BUFFER_SIZE);
test_omrstr_ftime(OMRPORTLIB, testName, buf, J9STR_BUFFER_SIZE, "%Y %m %b %d %H:%M:%S", timeMillis, OMRSTR_FTIME_FLAG_UTC, expected);

/* Second Test: February 29th, 2004, 12:00:00, UTC. */
timeMillis = J9CONST64(1078056000000);
strncpy(expected, "2004 02 Feb 29 00", J9STR_BUFFER_SIZE);
test_omrstr_ftime(OMRPORTLIB, testName, buf, J9STR_BUFFER_SIZE, "%Y %m %b %d %S", timeMillis, expected);
test_omrstr_ftime(OMRPORTLIB, testName, buf, J9STR_BUFFER_SIZE, "%Y %m %b %d %S", timeMillis, OMRSTR_FTIME_FLAG_LOCAL, expected);

strncpy(expected, "2004 02 Feb 29 12:00:00", J9STR_BUFFER_SIZE);
test_omrstr_ftime(OMRPORTLIB, testName, buf, J9STR_BUFFER_SIZE, "%Y %m %b %d %H:%M:%S", timeMillis, OMRSTR_FTIME_FLAG_UTC, expected);

/* Third test: Too short a buffer */
ret = omrstr_ftime(buf, 3, "%Y", timeMillis);
Expand All @@ -529,19 +535,19 @@ TEST(PortStrTest, str_test3)
* Use the time February 29th, 2004, 12:00:00, UTC. */
timeMillis = J9CONST64(1078056000000);
strncpy(expected, "2004 02 Feb 29 00 %pid %uid %job %home %last %seq", J9STR_BUFFER_SIZE);
test_omrstr_ftime(OMRPORTLIB, testName, buf, J9STR_BUFFER_SIZE, "%Y %m %b %d %S %pid %uid %job %home %last %seq", timeMillis, expected);
test_omrstr_ftime(OMRPORTLIB, testName, buf, J9STR_BUFFER_SIZE, "%Y %m %b %d %S %pid %uid %job %home %last %seq", timeMillis, OMRSTR_FTIME_FLAG_LOCAL, expected);

/* Fifth Test: Pass in Tokens that are not valid by default anywhere.
* Use the time February 29th, 2004, 12:00:00, UTC. */
timeMillis = J9CONST64(1078056000000);
strncpy(expected, "2004 02 Feb 29 00 %zzz = %zzz", J9STR_BUFFER_SIZE);
test_omrstr_ftime(OMRPORTLIB, testName, buf, J9STR_BUFFER_SIZE, "%Y %m %b %d %S %zzz = %%zzz", timeMillis, expected);
test_omrstr_ftime(OMRPORTLIB, testName, buf, J9STR_BUFFER_SIZE, "%Y %m %b %d %S %zzz = %%zzz", timeMillis, OMRSTR_FTIME_FLAG_LOCAL, expected);

/* Sixth Test: Pass in all time tokens.
* Use the time February 29th, 2004, 12:00:00, UTC. */
timeMillis = J9CONST64(1078056000000);
strncpy(expected, "04,(2004) 02,(Feb) 29 XX:00:00 %", J9STR_BUFFER_SIZE);
test_omrstr_ftime(OMRPORTLIB, testName, buf, J9STR_BUFFER_SIZE, "%y,(%Y) %m,(%b) %d XX:%M:%S %", timeMillis, expected);
test_omrstr_ftime(OMRPORTLIB, testName, buf, J9STR_BUFFER_SIZE, "%y,(%Y) %m,(%b) %d XX:%M:%S %", timeMillis, OMRSTR_FTIME_FLAG_LOCAL, expected);

reportTestExit(OMRPORTLIB, testName);
}
Expand Down
9 changes: 8 additions & 1 deletion include_core/omrport.h
Expand Up @@ -1159,6 +1159,10 @@ typedef struct J9ProcessorInfos {
/* Windows current thread ANSI code page */
#define J9STR_CODE_WINTHREADACP 8

/* flags for omrstr_ftime_ex */
#define OMRSTR_FTIME_FLAG_LOCAL 0 /* result should be produced for local timezone */
#define OMRSTR_FTIME_FLAG_UTC 1 /* result should be produced for UTC timezone */

#if defined(J9ZOS390)
/*
* OMR on z/OS translates the output of certain system calls such as getenv to ASCII using functions in atoe.c; see stdlib.h for a list.
Expand Down Expand Up @@ -2203,6 +2207,8 @@ typedef struct OMRPortLibrary {
int32_t (*file_blockingasync_lock_bytes)(struct OMRPortLibrary *portLibrary, intptr_t fd, int32_t lockFlags, uint64_t offset, uint64_t length) ;
/** see @ref omrstr.c::omrstr_ftime "omrstr_ftime"*/
uintptr_t (*str_ftime)(struct OMRPortLibrary *portLibrary, char *buf, uintptr_t bufLen, const char *format, int64_t timeMillis) ;
/** see @ref omrstr.c::omrstr_ftime_ex "omrstr_ftime_ex"*/
uintptr_t (*str_ftime_ex)(struct OMRPortLibrary *portLibrary, char *buf, uintptr_t bufLen, const char *format, int64_t timeMillis, uint32_t flags);
/** see @ref omrmmap.c::omrmmap_startup "omrmmap_startup"*/
int32_t (*mmap_startup)(struct OMRPortLibrary *portLibrary) ;
/** see @ref omrmmap.c::omrmmap_shutdown "omrmmap_shutdown"*/
Expand Down Expand Up @@ -2920,7 +2926,8 @@ extern J9_CFUNC int32_t omrport_getVersion(struct OMRPortLibrary *portLibrary);
#define omrfile_lock_bytes(param1,param2,param3,param4) privateOmrPortLibrary->file_lock_bytes(privateOmrPortLibrary, (param1), (param2), (param3), (param4))
#define omrfile_convert_native_fd_to_omrfile_fd(param1) privateOmrPortLibrary->file_convert_native_fd_to_omrfile_fd(privateOmrPortLibrary, (param1))
#define omrfile_convert_omrfile_fd_to_native_fd(param1) privateOmrPortLibrary->file_convert_omrfile_fd_to_native_fd(privateOmrPortLibrary,param1)
#define omrstr_ftime(param1,param2,param3,param4) privateOmrPortLibrary->str_ftime(privateOmrPortLibrary, (param1), (param2), (param3), (param4))
#define omrstr_ftime(param1,param2,param3,param4) privateOmrPortLibrary->str_ftime_ex(privateOmrPortLibrary, (param1), (param2), (param3), (param4), OMRSTR_FTIME_FLAG_LOCAL)
#define omrstr_ftime_ex(param1,param2,param3,param4,param5) privateOmrPortLibrary->str_ftime_ex(privateOmrPortLibrary, (param1), (param2), (param3), (param4), (param5))
#define omrmmap_startup() privateOmrPortLibrary->mmap_startup(privateOmrPortLibrary)
#define omrmmap_shutdown() privateOmrPortLibrary->mmap_shutdown(privateOmrPortLibrary)
#define omrmmap_capabilities() privateOmrPortLibrary->mmap_capabilities(privateOmrPortLibrary)
Expand Down
3 changes: 2 additions & 1 deletion port/common/omrport.c
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2015, 2020 IBM Corp. and others
* Copyright (c) 2015, 2021 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -253,6 +253,7 @@ static OMRPortLibrary MainPortLibraryTable = {
omrfile_blockingasync_unlock_bytes, /* file_blockingasync_unlock_bytes */
omrfile_blockingasync_lock_bytes, /* file_blockingasync_lock_bytes */
omrstr_ftime, /* str_ftime */
omrstr_ftime_ex, /* str_ftime_ex */
omrmmap_startup, /* mmap_startup */
omrmmap_shutdown, /* mmap_shutdown */
omrmmap_capabilities, /* mmap_capabilities */
Expand Down

0 comments on commit 42b04a6

Please sign in to comment.