Hi! I am using a C function from an existing shared library to convert a Julian day number into a date and time information in the ISO 8601 format (YYYY-MM-DDThh:mm:ssZ). This C function generates and returns a vector of characters, which must be converted into a string with the String()
function. The whole operation works fine except that this final conversion from a vector of characters to a string appears to empty that vector:
(base) michel@MicMac2:~$ JMtk15
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.8.2 (2022-09-29)
_/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
julia> using JMtk15
[ Info: Precompiling JMtk15 [600a0e46-2e83-4a57-a945-993fe116ce31]
julia> jd = 2.4523965833333335e6
2.4523965833333335e6
julia> datetime = zeros(Cuchar, 28)
28-element Vector{UInt8}:
0x00
0x00
âŽ
0x00
0x00
julia> status = MtkJulianToDateTime(jd, datetime)
MTK_SUCCESS::MTKt_status = 0x00000000
julia> dt = rstrip(String(datetime), '\0')
"2002-05-02T02:00:00Z"
julia> datetime
UInt8[]
julia> x = rstrip(String(datetime), '\0')
""
where the Julia call to the C function (by the same name) is written as follows:
function MtkJulianToDateTime(jd, datetime)
ccall((:MtkJulianToDateTime, mtklib),
MTKt_status,
(Cdouble, Ptr{Cchar}),
jd, datetime)
end
Two questions:
-
Why does the variable
datetime
appear to be empty once it has been used to createdt
? -
Is there any way that the Julia function
MtkJulianToDateTime
could setdatetime
to the desired string rather than a vector of characters? In other words, could this conversion be made part of the Julia function to avoid having to remember to do it explicitly after the function has returned control?
Thanks for any clarification on these matters.