Search code examples
julia

Parse "2023-12-12T00:00:00+05:30" in julia


Please help in parsing the following: "2023-12-12T00:00:00+05:30"

Currently I am using the following

function parsetime(dt::String)
    a,b=split(dt, '+')
    c,d=split(b,':')
    DateTime(a)+Hour(c)+Minute(d)
end

But, I think it is not very efficient. Please give some good methods.


Solution

  • using TimeZones
    function parseZonedDateTime(dt::String)
        return ZonedDateTime(dt,"yyyy-mm-ddTHH:MM:SSzzzz")
    end
    
    using Dates
    function parseDateTime(dt::String)
        return DateTime(dt[1:(end-6)],"yyyy-mm-ddTHH:MM:SS")
    end
    
    function parseToUTC(dt::ZonedDateTime)
        return astimezone(parseZonedDateTime(dt), tz"UTC")
    end
    
    # The best
    function manualParseToUTC(dt::String)
        res = DateTime(dt[1:(end-6)],"yyyy-mm-ddTHH:MM:SS")
        return res + Hour(parse(Int, x[(end-5):(end-3)]))
    end