Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust to server direct get message time formatting #571

Merged
merged 1 commit into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 1 addition & 30 deletions src/jsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1546,36 +1546,7 @@ js_directGetMsgToJSMsg(const char *stream, natsMsg *msg)
val = NULL;
s = natsMsgHeader_Get(msg, JSTimeStamp, &val);
if ((s == NATS_OK) && !nats_IsStringEmpty(val))
{
// The server sends the time in this format (always UTC):
// 2006-01-02 15:04:05.999999999 +0000 UTC
// But for our parsing to work (from JSON) we will convert this
// to something like that:
// 2006-01-02T15:04:05.999999999Z
char tmpTime[40] = {'\0'};
char *ptr = NULL;

if (snprintf(tmpTime, sizeof(tmpTime), "%s", val) >= (int) sizeof(tmpTime)) {
tmpTime[39] = '\0';
}

ptr = strchr(tmpTime, ' ');
if (ptr != NULL)
{
*ptr = 'T';
ptr++;

ptr = strchr(ptr, ' ');
if (ptr != NULL)
{
*ptr = 'Z';
ptr++;
*ptr = '\0';

s = nats_parseTime((char*) tmpTime, &tm);
}
}
}
s = nats_parseTime((char*) val, &tm);
if ((s != NATS_OK) || (tm == 0))
return nats_setError(NATS_ERR, "missing or invalid timestamp '%s'", val);

Expand Down
22 changes: 13 additions & 9 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1284,41 +1284,45 @@ nats_JSONGetObject(nats_JSON *json, const char *fieldName, nats_JSON **value)
}

natsStatus
nats_parseTime(char *str, int64_t *timeUTC)
nats_parseTime(char *orgStr, int64_t *timeUTC)
{
natsStatus s = NATS_OK;
char *dotPos = NULL;
char utcOff[7] = {'\0'};
int64_t nanosecs = 0;
char *p = NULL;
char orgStr[36] = {'\0'};
char timeStr[36] = {'\0'};
char timeStr[42] = {'\0'};
char tmpStr[36] = {'\0'};
char *str = NULL;
char offSign = '+';
int offHours = 0;
int offMin = 0;
int i, l;
struct tm tp;

// Check for "0"
if (strcmp(str, "0001-01-01T00:00:00Z") == 0)
if (strcmp(orgStr, "0001-01-01T00:00:00Z") == 0)
{
*timeUTC = 0;
return NATS_OK;
}

l = (int) strlen(str);
l = (int) strlen(orgStr);
// The smallest date/time should be: "YYYY:MM:DDTHH:MM:SSZ", which is 20
// while the longest should be: "YYYY:MM:DDTHH:MM:SS.123456789-12:34" which is 35
if ((l < 20) || (l > (int) (sizeof(timeStr) - 1)))
if ((l < 20) || (l > (int) (sizeof(tmpStr) - 1)))
{
if (l < 20)
s = nats_setError(NATS_INVALID_ARG, "time '%s' too small", str);
s = nats_setError(NATS_INVALID_ARG, "time '%s' too small", orgStr);
else
s = nats_setError(NATS_INVALID_ARG, "time '%s' too long", str);
s = nats_setError(NATS_INVALID_ARG, "time '%s' too long", orgStr);
return NATS_UPDATE_ERR_STACK(s);
}

snprintf(orgStr, sizeof(orgStr), "%s", str);
// Copy the user provided string in our temporary buffer since we may alter
// the string as we parse.
snprintf(tmpStr, sizeof(tmpStr), "%s", orgStr);
str = (char*) tmpStr;
memset(&tp, 0, sizeof(struct tm));

// If ends with 'Z', the time is already UTC
Expand Down
7 changes: 5 additions & 2 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -27880,7 +27880,7 @@ test_JetStreamConvertDirectMsg(void)
nats_clearLastError();

test("Missing subject: ");
s = natsMsgHeader_Set(msg, JSTimeStamp, "2006-01-02 15:04:05.999999999 +0000 UTC");
s = natsMsgHeader_Set(msg, JSTimeStamp, "2006-01-02T15:04:05Z");
IFOK(s, js_directGetMsgToJSMsg("test", msg));
testCond((s == NATS_ERR) && (strstr(nats_GetLastError(NULL), "missing or invalid subject") != NULL));
nats_clearLastError();
Expand All @@ -27897,7 +27897,7 @@ test_JetStreamConvertDirectMsg(void)
testCond((s == NATS_OK)
&& (strcmp(natsMsg_GetSubject(msg), "foo") == 0)
&& (natsMsg_GetSequence(msg) == 1)
&& (natsMsg_GetTime(msg) == 1136214245999999999L)
&& (natsMsg_GetTime(msg) == 1136214245000000000L)
&& (natsMsgHeader_Get(msg, "some", &val) == NATS_OK)
&& (strcmp(val, "header") == 0));

Expand Down Expand Up @@ -31730,6 +31730,9 @@ test_StanInternalSubsNotPooled(void)
stanSubscription_Destroy(sub);
stanConnection_Destroy(sc);

if (valgrind)
nats_Sleep(900);

_stopServer(pid);
}

Expand Down