3 releases
Uses new Rust 2024
| 0.1.2 | Nov 20, 2025 |
|---|---|
| 0.1.1 | Nov 17, 2025 |
| 0.1.0 | Nov 12, 2025 |
#1337 in Algorithms
41KB
492 lines
Fone Crate
The fone crate is a lightweight utility library for Rust that provides:
- Arithmetic Operations: Simple functions for performing basic math.
- Logging Utilities: Macros for in-memory and file-based logging with support for different severity levels.
Features
-
Arithmetic Utilities:
- add: Adds two integers.
-
Logging Utilities:
- In-memory logging:
logi!,logd!,logw!,loge! - File-based logging:
logfi!,logfd!,logfw!,logfe!
- In-memory logging:
Installation
Add the following to your Cargo.toml:
[dependencies]
fone = "a.b.c"
Testing
To run the test suite:
cargo test -v
cargo test -- --show-output
cargo test test_t -v -- --show-output
Usage
use fone::add;
fn main() {
let result = add(2, 2);
println!("Result: {}", result);
}
use fone::{logi, logd, logw, loge};
fn main() {
logi!("Application initialized");
logd!("Configuration loaded");
logw!("Deprecated API used");
loge!("Critical system failure");
}
use fone::{logfi, logfd, logfw, logfe};
fn main() {
logfi!("Application initialized");
logfd!("Configuration loaded");
logfw!("Deprecated API used");
logfe!("Critical system failure");
}
start_format_time(None);
start_format_time(Some(1800)); // UTC+0.5
start_format_time(Some(3600)); // UTC+1
start_format_time(Some(3600 * 8)); // UTC+1
fn start_format_time(time_zone_offset: Option<i32>) {
// Get current timestamp with nanoseconds precision
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Time went backwards");
let timestamp = now.as_secs();
let nanos = now.subsec_nanos(); // Get fractional part in nanoseconds
let timestamp_with_millis = timestamp * 1_000 + (nanos / 1_000_000) as u64; // Convert to milliseconds
// Convert timestamp to `DateTime` with optional time zone offset
let date_time = DateTime::from_timestamp(timestamp_with_millis, time_zone_offset);
// Print formatted date and time
let format_date = "%Y-%m-%d"; // YYYY-MM-DD
let format_datetime = "%Y-%m-%d %H:%M"; // YYYY-MM-DD HH:MM
let format_datetime_seconds = "%Y-%m-%d %H:%M:%S"; // YYYY-MM-DD HH:MM:SS
let format_datetime_millis = "%Y-%m-%d %H:%M:%S.%f"; // YYYY-MM-DD HH:MM:SS.sss
println!("==========The Now Time:=========");
// Get current time with no time zone offset (UTC)
let current_time = DateTime::now(None);
// Print individual components
println!("Current Time Components:");
current_time.print_components();
// Format and print the current time
let formatted_time = current_time.format("%Y-%m-%d %H:%M:%S.%f");
println!("Formatted Current Time: {}", formatted_time);
println!("==========The Time:=========");
println!("Timestamp:{}, timestamp_with_millis:{}, time_zone_offset:{:?}", timestamp, timestamp_with_millis, time_zone_offset);
println!("Date: {}", date_time.format(format_date));
println!("DateTime: {}", date_time.format(format_datetime));
println!("DateTime with Seconds: {}", date_time.format(format_datetime_seconds));
println!("DateTime with Milliseconds: {}", date_time.format(format_datetime_millis));
println!("Time Zone: {}", date_time.get_time_zone());
println!("Year: {}", date_time.get_year());
println!("Month: {}", date_time.get_month());
println!("Day: {}", date_time.get_day());
println!("Hour: {}", date_time.get_hour());
println!("Minute: {}", date_time.get_minute());
println!("Second: {}", date_time.get_second());
println!("Millisecond: {}", date_time.get_millisecond());
// Print individual components
// println!("\nIndividual Components:");
// date_time.print_components();
println!("======The Time Zone======");
// Create a DateTime object with a specific time zone offset (e.g., UTC+1)
let timestamp = 1_696_500_000_000; // Example timestamp
let date_time = DateTime::from_timestamp(timestamp, Some(3600)); // UTC+1
// Get and print the time zone
let time_zone = date_time.get_time_zone();
println!("Time Zone: {}", time_zone);
// Example with negative offset (e.g., UTC-5)
let date_time_negative = DateTime::from_timestamp(timestamp, Some(-18000)); // UTC-5
let time_zone_negative = date_time_negative.get_time_zone();
println!("Time Zone: {}", time_zone_negative);
}
Documentation
Generate and view the documentation locally:
cargo doc --open
For online documentation, visit docs.rs/fone.
Contributing
Contributions are welcome! Please ensure that:
- New features include appropriate tests.
- Documentation is updated for all public APIs.
- Code adheres to Rust best practices and formatting standards.
License
This project is licensed under the MIT License. See LICENSE for details.