luxon icon indicating copy to clipboard operation
luxon copied to clipboard

Rehydrate/recreate Luxon object (structural cloning)

Open florianbepunkt opened this issue 2 years ago • 5 comments

Is your feature request related to a problem? Please describe. I'm using luxon in Electron. After sending luxon objects via IPC, all prototype methods are lost, since IPC uses structural cloning.

So the data is still there (see below), but you cannot call methods on it.

{
    "ts": 1632952800000,
    "_zone": {},
    "loc": {
        "locale": "de",
        "numberingSystem": null,
        "outputCalendar": null,
        "intl": "de",
        "weekdaysCache": {
            "format": {},
            "standalone": {}
        },
        "monthsCache": {
            "format": {},
            "standalone": {}
        },
        "meridiemCache": null,
        "eraCache": {},
        "specifiedLocale": null,
        "fastNumbersCached": null
    },
    "invalid": null,
    "weekData": null,
    "c": {
        "year": 2021,
        "month": 9,
        "day": 30,
        "hour": 0,
        "minute": 0,
        "second": 0,
        "millisecond": 0
    },
    "o": 120,
    "isLuxonDateTime": true
}

Describe the solution you'd like A way to create a DateTime from a DateTime obj or another way to rehydrate the DateTime.

Describe alternatives you've considered Do some custom serialization, but I would like to avoid this since we use custom Codecs for validation. Recreating the DateTime from the timestamp (ts)

Additional context Add any other context or screenshots about the feature request here.

florianbepunkt avatar Jan 22 '22 21:01 florianbepunkt

Let me make sure I understand your request: you would like a function that takes in a plain ol javascript object, presumably a serialized-and-then-deserialized DateTime instance, and returns a DateTime?

icambron avatar Feb 28 '22 03:02 icambron

Yes, exactly.

florianbepunkt avatar Feb 28 '22 05:02 florianbepunkt

That would require a bit of mucking with the DateTime constructor, which is a little messy. But it's a reasonable request; I'd take a crisp PR that does this

icambron avatar Mar 05 '22 03:03 icambron

We have a similar need when using Cloudflare Durable Objects and Cloudflare KV store which does structural cloning. Would be very helpful it we didn't have to JSON.parse and JSON.stringify each item we store

tobad357 avatar Jun 29 '22 05:06 tobad357

This feature is also useful for passing Luxon DateTime to web workers.

cphillips avatar Feb 01 '24 14:02 cphillips