EasyTime Library
The EasyTime library is a powerful Rust crate designed to simplify date and time calculations. It builds upon the chrono library to provide robust support for time zone-aware operations, leap year handling, and extensive formatting options. With EasyTime, you can easily add or subtract seconds, minutes, hours, days, weeks, months, years, decades, centuries, and millennia from the current time using simple static methods.
Features
- Simple Static API: No need to create instances - just call
EasyTime::<Local>::days_from_now(5). - Time Zone Support: Work with local time (
Local) or UTC time (Utc). - Flexible Date-Time Offsets: Add or subtract time intervals with methods for seconds, minutes, hours, days, weeks, months, years, and beyond.
- Precise Calculations: Handles leap years, month lengths, and timezone transitions.
- Comprehensive Formatting: Customize date-time output with flexible formatting functions.
- Conversion Utilities: Convert between timestamps, dates, and times.
Installation
To use the EasyTime library in your Rust project, add it as a dependency in your Cargo.toml:
[]
= "*"
Usage
Basic Time Calculations
use EasyTime;
use Local;
Working with UTC
use EasyTime;
use Utc;
Using Custom Base Times
use EasyTime;
use ;
Decades, Centuries, and Millennia
use EasyTime;
use Local;
Formatting Functions
use ;
use Local;
Handling Leap Years
use EasyTime;
use Local;
API Reference
Time Offset Methods (Local Timezone)
All methods are static and take the offset value as a parameter:
| Method | Description |
|---|---|
EasyTime::<Local>::seconds_from_now(n) |
n seconds in the future |
EasyTime::<Local>::seconds_ago(n) |
n seconds in the past |
EasyTime::<Local>::minutes_from_now(n) |
n minutes in the future |
EasyTime::<Local>::minutes_ago(n) |
n minutes in the past |
EasyTime::<Local>::hours_from_now(n) |
n hours in the future |
EasyTime::<Local>::hours_ago(n) |
n hours in the past |
EasyTime::<Local>::days_from_now(n) |
n days in the future |
EasyTime::<Local>::days_ago(n) |
n days in the past |
EasyTime::<Local>::weeks_from_now(n) |
n weeks in the future |
EasyTime::<Local>::weeks_ago(n) |
n weeks in the past |
EasyTime::<Local>::months_from_now(n) |
n months in the future |
EasyTime::<Local>::months_ago(n) |
n months in the past |
EasyTime::<Local>::years_from_now(n) |
n years in the future |
EasyTime::<Local>::years_ago(n) |
n years in the past |
EasyTime::<Local>::decades_from_now(n) |
n decades in the future |
EasyTime::<Local>::decades_ago(n) |
n decades in the past |
EasyTime::<Local>::centuries_from_now(n) |
n centuries in the future |
EasyTime::<Local>::centuries_ago(n) |
n centuries in the past |
EasyTime::<Local>::millenniums_from_now(n) |
n millennia in the future |
EasyTime::<Local>::millenniums_ago(n) |
n millennia in the past |
Methods with Custom Base Time
For calculations from a specific date instead of "now":
| Method | Description |
|---|---|
EasyTime::<Local>::seconds_from(base, n) |
n seconds after base |
EasyTime::<Local>::seconds_before(base, n) |
n seconds before base |
EasyTime::<Local>::days_from(base, n) |
n days after base |
EasyTime::<Local>::days_before(base, n) |
n days before base |
EasyTime::<Local>::months_from(base, n) |
n months after base |
EasyTime::<Local>::months_before(base, n) |
n months before base |
| ... | (all time units supported) |
UTC Methods
Same methods are available for EasyTime::<Utc>:
days_from_now
hours_ago
Convenience UTC Methods
These can be called from any timezone type and return UTC times:
utc_days_from_now
utc_hours_ago
Formatting Functions
| Function | Description |
|---|---|
format_datetime(&dt) |
Format as YYYY-MM-DD HH:MM:SS |
format_datetime_with(&dt, fmt) |
Format with custom format string |
format_datetime_with_timezone(&dt) |
Format with timezone offset |
format_datetime_with_timezone_format(&dt, fmt) |
Custom format with timezone |
to_date(&dt) |
Returns YYYY-MM-DD |
to_time(&dt) |
Returns HH:MM:SS |
to_timestamp(&dt) |
Returns Unix timestamp |
Utility Methods
| Method | Description |
|---|---|
EasyTime::<Local>::is_leap_year(year) |
Check if year is a leap year |
EasyTime::<Local>::days_in_month(year, month) |
Get number of days in a month |
Rusty Rails Project
Rusty Rails is a larger project aiming to bridge the gap between Rust and Ruby/Ruby on Rails. We are actively working on recreating Ruby libraries in Rust to make working with Rust more easy and fun for new developers.
Contributing
Contributions to the EasyTime library are welcome! Feel free to open issues, submit pull requests, or provide feedback to help improve this library.