Skip to content

Instantly share code, notes, and snippets.

@Eosis
Last active January 13, 2022 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eosis/35fae49813e21fd46c64de94ad936fc4 to your computer and use it in GitHub Desktop.
Save Eosis/35fae49813e21fd46c64de94ad936fc4 to your computer and use it in GitHub Desktop.
// Backend TimestampCodec
import * as T from 'io-ts'
import { DateTime } from 'luxon'
import * as admin from 'firebase-admin'
export const LuxonDateTimeT = new T.Type<DateTime, admin.firestore.Timestamp, unknown>(
'LuxonDateTimeT',
(u: unknown): u is DateTime => u instanceof DateTime,
(u: unknown, c: T.Context) => {
if (!(u instanceof admin.firestore.Timestamp)) {
return T.failure(u, c)
}
return T.success(DateTime.fromJSDate(u.toDate()))
},
(a: DateTime) => admin.firestore.Timestamp.fromDate(a.toJSDate()),
)
const JustStartTimeT = T.type({
startTime: LuxonDateTimeT,
})
export default JustStartTimeT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment