time-format
A lightweight, zero-dependency Rust crate for formatting Unix timestamps in both UTC and local time, with millisecond precision.
Features
- 🚀 Zero dependencies - No external crates needed
- 🌐 Multiple timezones - Support for both UTC and local time zones
- ⏱️ Millisecond precision - High-resolution timestamp formatting
- 🧩 Component access - Split timestamps into their individual components
- 📅 Standard formats - Built-in support for ISO 8601, RFC 3339, RFC 2822, and more
- 🔄 Custom formatting - Flexible formatting using C's
strftimepatterns - ⚡ Performance - Direct FFI bindings to system time functions
Why time-format?
Simple things should be simple.
When you just need to format a timestamp in a standardized way like ISO 8601 or get time components, you shouldn't need to pull in complex dependencies, understand type conversion hierarchies, or deal with feature flags.
time-format excels at:
- Minimalism: Zero dependencies means faster builds and smaller binaries
- Simplicity: Clear and intuitive API with straightforward error handling
- Performance: Thin wrapper over system time functions for minimal overhead
- Common formats: Built-in support for the most widely used date formats
It's the ideal choice when you just need timestamp formatting without the complexity of full-featured time libraries.
Installation
Add to your Cargo.toml:
[]
= "1.2.1"
Basic Usage
Getting the Current Time
// Get current time in seconds
let ts = now.unwrap;
// Get current time with millisecond precision
let ts_ms = now_ms.unwrap;
Converting from SystemTime
use SystemTime;
// Convert a SystemTime to a TimeStamp (seconds)
let system_time = now;
let ts = from_system_time.unwrap;
// Convert a SystemTime to a TimeStampMs (with millisecond precision)
let ts_ms = from_system_time_ms.unwrap;
Splitting a Timestamp into Components
// Get components in UTC
let ts = now.unwrap;
let components = components_utc.unwrap;
println!;
// Available components:
// - sec (0-59)
// - min (0-59)
// - hour (0-23)
// - month_day (1-31)
// - month (1-12) - January is 1, December is 12
// - year (e.g., 2025)
// - week_day (0-6) - Sunday is 0, Saturday is 6
// - year_day (0-365)
// Get components in local time
let local_components = components_local.unwrap;
Formatting a Timestamp
UTC Time
let ts = now.unwrap;
// Basic date formatting
let date = strftime_utc.unwrap;
// Example: "2025-05-20"
// Time formatting
let time = strftime_utc.unwrap;
// Example: "14:30:45"
// Combined date and time
let datetime = strftime_utc.unwrap;
// Example: "2025-05-20 14:30:45"
// Custom format
let custom = strftime_utc.unwrap;
// Example: "Tue, 20 May 2025 14:30:45 UTC"
Local Time
let ts = now.unwrap;
// Local time with timezone name
let local_time = strftime_local.unwrap;
// Example: "2025-05-20 09:30:45 PDT"
Millisecond Precision
let ts_ms = now_ms.unwrap;
// Format with milliseconds in UTC
let precise_time = strftime_ms_utc.unwrap;
// Example: "2025-05-20 14:30:45.123"
// Format with milliseconds in local time
let precise_local = strftime_ms_local.unwrap;
// Example: "2025-05-20 09:30:45.123 PDT"
ISO 8601 Formatting
Format timestamps according to ISO 8601 standard:
let ts = now.unwrap;
let ts_ms = now_ms.unwrap;
// ISO 8601 in UTC
let iso8601 = format_iso8601_utc.unwrap;
// Example: "2025-05-20T14:30:45Z"
// ISO 8601 with milliseconds in UTC
let iso8601_ms = format_iso8601_ms_utc.unwrap;
// Example: "2025-05-20T14:30:45.123Z"
// ISO 8601 in local timezone
let iso8601_local = format_iso8601_local.unwrap;
// Example: "2025-05-20T09:30:45-05:00"
// ISO 8601 with milliseconds in local timezone
let iso8601_ms_local = format_iso8601_ms_local.unwrap;
// Example: "2025-05-20T09:30:45.123-05:00"
Common Date Formats
The crate provides convenient formatting for common date formats:
let ts = now.unwrap;
let ts_ms = now_ms.unwrap;
// RFC 3339 (similar to ISO 8601)
let rfc3339 = format_common_utc.unwrap;
// Example: "2025-05-20T14:30:45Z"
// RFC 2822 format (email format)
let rfc2822 = format_common_utc.unwrap;
// Example: "Tue, 20 May 2025 14:30:45 +0000"
// HTTP date format (RFC 7231)
let http_date = format_common_utc.unwrap;
// Example: "Tue, 20 May 2025 14:30:45 GMT"
// SQL date format
let sql_format = format_common_utc.unwrap;
// Example: "2025-05-20 14:30:45"
// US date format
let us_format = format_common_local.unwrap;
// Example: "05/20/2025 02:30:45 PM"
// European date format
let eu_format = format_common_local.unwrap;
// Example: "20/05/2025 14:30:45"
// With millisecond precision
let rfc3339_ms = format_common_ms_utc.unwrap;
// Example: "2025-05-20T14:30:45.123Z"
// Custom format
let custom = format_common_utc.unwrap;
// Example: "2025/05/20"
Available format types:
RFC3339: ISO 8601-like formatRFC2822: Email date formatHTTP: Web standard date formatSQL: SQL database formatUS: US style date format (MM/DD/YYYY)European: European style date format (DD/MM/YYYY)ShortDate: Short date (MM/DD/YY)LongDate: Long date with full month and day namesShortTime: Hours and minutesLongTime: Hours, minutes, and secondsDateTime: ISO-like date and timeCustom: Custom format string
Common Format Directives
| Directive | Description | Example |
|---|---|---|
%Y |
Year (4 digits) | 2025 |
%m |
Month (01-12) | 05 |
%d |
Day of month (01-31) | 20 |
%H |
Hour (00-23) | 14 |
%M |
Minute (00-59) | 30 |
%S |
Second (00-59) | 45 |
%a |
Abbreviated weekday | Tue |
%A |
Full weekday | Tuesday |
%b |
Abbreviated month | May |
%B |
Full month | May |
%c |
Locale date and time | Tue May 20 14:30:45 2025 |
%Z |
Timezone name | UTC, PDT, etc. |
%z |
Timezone offset | +0000, -0500 |
{ms} |
Milliseconds (custom extension) | 123 |
Comparison with Other Time Libraries
| Feature | time-format | chrono | time |
|---|---|---|---|
| Dependencies | None | Multiple | Multiple |
| ISO 8601 | ✅ | ✅ | ✅ |
| RFC 2822/3339 | ✅ | ✅ | ✅ |
| Milliseconds | ✅ | ✅ | ✅ |
| Local timezone | ✅ | ✅ | ✅ |
| Custom formats | ✅ | ✅ | ✅ |
| Binary size impact | Very Small | Larger | Medium |
| Compile time | Fast | Slower | Medium |
time-format is designed to be a lightweight alternative when you only need basic timestamp formatting capabilities without pulling in additional dependencies.
Related Crates
- coarsetime - A minimal crate to get timestamps and perform basic operations on them
- chrono - Complete date and time library with calendar feature support
- time - A comprehensive time library with timezone database support
Examples
Working with Web APIs
use ;
Converting from SystemTime in Network Applications
use ;
use ;
// Example of tracking network response times
Logging with Timestamps
use ;
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License.
Note: This crate uses FFI bindings to C's time functions. It's designed to be lightweight and efficient, but it does not include a timezone database. For applications requiring extensive timezone handling, consider chrono or time.