Skip to content

Commit

Permalink
fix: Return string instead of Date for utcTime field
Browse files Browse the repository at this point in the history
  • Loading branch information
microshine committed Jun 13, 2024
1 parent 0a47769 commit c578fc7
Showing 1 changed file with 6 additions and 22 deletions.
28 changes: 6 additions & 22 deletions src/pkcs11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,14 @@ napi_value create_version(napi_env env, CK_VERSION version)
*/
napi_value create_date_utc_property(napi_env env, CK_UTF8CHAR_PTR utcTime)
{
// create Date object from utcTime
napi_value utcTimeConstructor;
napi_get_global(env, &utcTimeConstructor);
napi_get_named_property(env, utcTimeConstructor, "Date", &utcTimeConstructor);
napi_value utcTimeDate;
char timeStr[17];
strncpy(timeStr, (char *)utcTime, 16);
timeStr[16] = '\0'; // Null-terminate the string

if (utcTime == nullptr || utcTime[0] == '\0' || utcTime[0] == ' ' || strlen((char *)utcTime) < 14)
{
napi_new_instance(env, utcTimeConstructor, 0, nullptr, &utcTimeDate);
return utcTimeDate;
}

// create string from utcTime
char isoTime[20];
snprintf(isoTime, sizeof(isoTime), "%c%c%c%c-%c%c-%c%cT%c%c:%c%c:%c%cZ", utcTime[0], utcTime[1], utcTime[2], utcTime[3], utcTime[4], utcTime[5], utcTime[6], utcTime[7], utcTime[8], utcTime[9], utcTime[10], utcTime[11], utcTime[12], utcTime[13]);
napi_value utcTimeValue;
napi_create_string_utf8(env, isoTime, NAPI_AUTO_LENGTH, &utcTimeValue);

// Date(utcTime)
napi_value utcTimeArgs[1];
utcTimeArgs[0] = utcTimeValue;
napi_new_instance(env, utcTimeConstructor, 1, utcTimeArgs, &utcTimeDate);
napi_value jsUtcTime;
napi_create_string_utf8(env, timeStr, NAPI_AUTO_LENGTH, &jsUtcTime);

return utcTimeDate;
return jsUtcTime;
}

/**
Expand Down

0 comments on commit c578fc7

Please sign in to comment.