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, months, years, decades, centuries, and millennia from the current time.
Features
- 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, months, years, and beyond.
- Precise Calculations: Handles leap years, month lengths, and timezone transitions.
- Comprehensive Formatting: Customize date-time output with flexible formatting options.
- 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
:
[]
= "0.1.7"
Usage
Create and Manipulate Time
extern crate easytime;
use EasyTime;
use Local;
Advanced Operations
Working with Months and Years
use EasyTime;
use Utc;
// Create an EasyTime instance for UTC with a value of 5
let easy_time = new_with_utc;
// Add months and years
let future_date = easy_time.months_from_now;
let past_year = easy_time.years_ago;
println!;
println!;
Using Decades and Centuries
use EasyTime;
use Local;
let easy_time = new; // Value represents decades or centuries
let decade_future = easy_time.decades_from_now;
let century_past = easy_time.centuries_ago;
println!;
println!;
Formatting and Conversion
use EasyTime;
use Local;
let easy_time = new;
// Format current time
let formatted_time = easy_time.to_string_with_timezone;
// Convert to UNIX timestamp
let timestamp = easy_time.to_timestamp;
println!;
println!;
More Examples
Custom Time Offsets
use EasyTime;
use ;
let easy_time = new;
let custom_future = easy_time.offset;
let custom_past = easy_time.offset_neg;
println!;
println!;
Handling Leap Years
use EasyTime;
use ;
let easy_time = new;
let leap_year = is_leap_year;
let non_leap_year = is_leap_year;
println!;
println!;
Date Adjustments Across Timezones
use EasyTime;
use ;
let local_time = new;
let utc_time = new_with_utc;
println!;
println!;
API Highlights
Constructors
new(value: i64)
: Create an instance with the current local time.new_with_utc(value: i64)
: Create an instance with the current UTC time.
Offset Calculations
seconds_from_now()
,seconds_ago()
minutes_from_now()
,minutes_ago()
hours_from_now()
,hours_ago()
days_from_now()
,days_ago()
months_from_now()
,months_ago()
years_from_now()
,years_ago()
Formatting Methods
to_string()
: Returns a string in the formatYYYY-MM-DD HH:MM:SS
.to_string_with_timezone()
: Includes timezone offset in the output.to_string_with_format(format_str: &str)
: Allows custom formatting.
Conversion Methods
to_timestamp()
: Converts the time to a UNIX timestamp.to_date()
: Returns the date inYYYY-MM-DD
format.to_time()
: Returns the time inHH:MM:SS
format.
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 library into rust that seamlessly make working in 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.