Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DateFromUnixTime #4192

Open
ryanleecode opened this issue Dec 26, 2024 · 0 comments
Open

Add DateFromUnixTime #4192

ryanleecode opened this issue Dec 26, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@ryanleecode
Copy link

What is the problem this feature would solve?

No easy constructor to create date from unix time.

What is the feature you are proposing to solve the problem?

import { Schema } from 'effect'

export const UnixTime = Schema.Int.pipe(
  Schema.nonNegative(),
  Schema.brand('UnixTime'),
)

const isUnixTime = Schema.is(UnixTime)

export const DateFromUnixTime = Schema.transform(
  UnixTime,
  Schema.DateFromSelf.pipe(
    Schema.filter((date) => isUnixTime(date.getTime() / 1000)),
  ),
  {
    strict: false,
    decode: (time) => new Date(time * 1000),
    encode: (date) => Math.floor(date.getTime() / 1000),
  },
)

What alternatives have you considered?

No response

@ryanleecode ryanleecode added the enhancement New feature or request label Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant