DEV Community

tetsu
tetsu

Posted on

5 3

epo: Handy Unix epoch time converter in Rust

epo

epo is a handy Unix time <-> date string converter that also taking multiple time zones into account. Written in Rust.

GitHub repository

$ epo 1648771200 1648771200+86400 greenwich
|      Epoch |                Greenwich |
| ---------- | ------------------------ |
| 1648771200 | 2022-04-01T00:00:00+0000 |
| 1648857600 | 2022-04-02T00:00:00+0000 |
Enter fullscreen mode Exit fullscreen mode

Install

brew tap wtetsu/epo
brew install epo
Enter fullscreen mode Exit fullscreen mode

Or download binaries

Usage

UNIX time → date string

Basically just specify Unix time and timezone.

$ epo 1648771200 greenwich
|      Epoch |                Greenwich |
| ---------- | ------------------------ |
| 1648771200 | 2022-04-01T00:00:00+0000 |
Enter fullscreen mode Exit fullscreen mode

Multiple UNIX times x Multiple time zones.

$ epo 1648771200 1648771200+86400 "1648771200+86400*2" greenwich +0900 new_y
|      Epoch |                Greenwich |                    +0900 |         America/New_York |
| ---------- | ------------------------ | ------------------------ | ------------------------ |
| 1648771200 | 2022-04-01T00:00:00+0000 | 2022-04-01T09:00:00+0900 | 2022-03-31T20:00:00-0400 |
| 1648857600 | 2022-04-02T00:00:00+0000 | 2022-04-02T09:00:00+0900 | 2022-04-01T20:00:00-0400 |
| 1648944000 | 2022-04-03T00:00:00+0000 | 2022-04-03T09:00:00+0900 | 2022-04-02T20:00:00-0400 |
Enter fullscreen mode Exit fullscreen mode

Options can be in any order. You can also write a formula: add 86400 to get one day later.

When specifying tz database names, it is OK to use forward matching (here "new_y" is assumed to be America/New_York).


You can also write JavaScript code (boa is embedded)

epo "range(10).map(i=>1647165300+i*60)" los_angeles phoenix
Enter fullscreen mode Exit fullscreen mode

It prints a Markdown formatted table, so it can be pasted here as is.

Epoch America/Los_Angeles America/Phoenix
1647165300 2022-03-13T01:55:00-0800 2022-03-13T02:55:00-0700
1647165360 2022-03-13T01:56:00-0800 2022-03-13T02:56:00-0700
1647165420 2022-03-13T01:57:00-0800 2022-03-13T02:57:00-0700
1647165480 2022-03-13T01:58:00-0800 2022-03-13T02:58:00-0700
1647165540 2022-03-13T01:59:00-0800 2022-03-13T02:59:00-0700
1647165600 2022-03-13T03:00:00-0700 2022-03-13T03:00:00-0700
1647165660 2022-03-13T03:01:00-0700 2022-03-13T03:01:00-0700
1647165720 2022-03-13T03:02:00-0700 2022-03-13T03:02:00-0700
1647165780 2022-03-13T03:03:00-0700 2022-03-13T03:03:00-0700
1647165840 2022-03-13T03:04:00-0700 2022-03-13T03:04:00-0700

In the above table, you can see the moment when Los Angeles enters daylight saving time: -0800 becomes -0700, and suddenly it is 03:00. Incidentally, Phoenix is known as an area in the U.S. where daylight saving time is not adopted, and it remains at -0700 all the time.

Date string → UNIX time

It interprets ISO 8601 formats appropriately.

$ epo 2022-04-01 2022-05-01T12:30 2022-06-15T12:30:45 tokyo hawaii gmt
|                Date | Asia/Tokyo |  US/Hawaii |        GMT |
| ------------------- | ---------- | ---------- | ---------- |
| 2022-04-01T00:00:00 | 1648738800 | 1648807200 | 1648771200 |
| 2022-05-01T12:30:00 | 1651375800 | 1651444200 | 1651408200 |
| 2022-06-15T12:30:45 | 1655263845 | 1655332245 | 1655296245 |
Enter fullscreen mode Exit fullscreen mode

Image of Bright Data

Access Niche Markets with Ease – Unlock restricted market data with precision.

Get access to hard-to-reach data with our specialized proxy services designed for niche markets.

Access Markets

Top comments (0)

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay