Go's time.Unix in TypeScript
How to use
Install via yarn add locutus and import:
import { Unix } from 'locutus/golang/time/Unix'.
Or with CommonJS: const { Unix } = require('locutus/golang/time/Unix')
Use a bundler that supports tree-shaking so you only ship the functions you actually use. Vite, webpack, Rollup, and Parcel all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
| # | code | expected result |
|---|---|---|
| 1 | Unix(0, 0).toISOString() | '1970-01-01T00:00:00.000Z' |
| 2 | Unix(1700000000, 0).toISOString() | '2023-11-14T22:13:20.000Z' |
| 3 | Unix(1700000000, 500000000).toISOString() | '2023-11-14T22:13:20.500Z' |
Notes
Returns a JavaScript Date representing sec + nsec nanoseconds since Unix epoch.
Here's what our current TypeScript equivalent to Go's time.Unix looks like.
export function Unix(sec: number, nsec = 0): Date { |
Improve this function
Locutus is a community effort following The McDonald's Theory: we ship first iterations, hoping others will improve them. If you see something that could be better, we'd love your contribution.
View on GitHub · Edit on GitHub · View Raw
We have 47 Go functions so far - help us add more
Got a rainy Sunday afternoon and a taste for a porting puzzle?
- Get inspiration from Go strings documentation, Go strings source, Go strings examples source, GopherJS.
- Click "New file" in the appropriate folder on GitHub. This will fork the project to your account, directly add the file to it, and send a Pull Request to us.
We will then review it. If it's useful to the project and in line with our contributing guidelines your work will become part of Locutus and you'll be automatically credited in the authors section accordingly.
Star