How can I parse timestamps in ISO 8601 format?

I use an api that returns timestamps in this format: 

2025-04-10T18:21:47.779Z
Is there a way to parse this into a Moment or unix timestamp?
The best idea I could come up with is to read the parts with substring, then convert to Number, then create a dictionary and then 
var options = {
    :year   => 2025,
    :month  => 4,
    :day    => 10,
    :hour   => 18,
    :minute => 21,
:second => 47
}; var timestamp = Gregorian.moment(options);


But all this would almost double the datafield.... so the best I could come up with is to store the timestamps as a string,
and compare them as a string.

  • I meant use a different server, like a home server or some cloud one you could spin up, that would basically proxy your API call but fix the return value. How is the time coming back? Is it in json or some other format?

  • Your version of the code (which is the same as mine except for the casts and the fact that yours doesn't use a local variable for the options dictionary) takes 183 bytes of code and 9 bytes of data in my testing.

    Here's a version that takes 165 bytes of code, 9 bytes of data, and has the same (non-existent) level of error checking. That's an 18 byte or ~10% savings.

    function parseISO8601(timestamp as String) as Number {
        var options = {};
        var keys = [
            :year,
            :month,
            :day,
            :hour,
            :minute,
            :second
        ];
        var index = 0;
        var offset = 2;
        for (var i = 0; i < 6; i++) {
            var endIndex = index + 2 + offset;
            offset = 0;
            options[keys[i]] = (timestamp.substring(index, endIndex) as String).toNumber();
            index = endIndex + 1;
        }
        return Gregorian.moment(options).value();
    }

  • I did think about a proxy for a moment, because the API I found returns lots of data I don't need, and on some older devices it's too big and it crashes. But for now I try to avoid adding a proxy, because it would add more points of failure. It could also have an advantage though: I could change the provider without the user (of the CIQ DF) needing to know, but it would also mean I would have to pay for the API, which is free up-to 500 requests per day per IP...

  • Like it! 

    Monkey C forces us to be creative.

  • Just for the fun: even 8 bytes less:

        var length = 4;
        for (var i = 0; i < 6; i++) {
            var endIndex = index + length;
            length = 2;
    

  • Just for the fun: even 8 bytes less:

    Nice! But is it really just for "fun"? I was under the impression that you were super worried about the impact on code size this function would have. Maybe it turned out to be a *lot* smaller than you thought it would be?

    Otherwise I would think that an additional savings of 8 bytes would be significant (given that a ~200 byte function is an issue in the first place, and that initially saving 18 bytes makes a difference.)