Bevy In-Game Clock
A plugin for the Bevy game engine that provides an in-game clock system with date/time tracking, configurable speed, sending events on time based intervals, and flexible formatting.
Features
- 📅 Date & Time Tracking - Full date and time support with configurable start date/time
- ⏰ Flexible Speed Control - Set speed by multiplier or by real-time duration per in-game day
- ⚡ Adjustable Clock Speed - Slow motion, fast forward, or any custom speed
- ⏸️ Pause and Resume - Full control over clock state
- 🎨 Flexible Formatting - Default or custom datetime formats using chrono
- 📆 Date Calculations - Automatic handling of months, years, and leap years via chrono
- ⚙️ Event System - Receive Bevy events at configurable intervals (hourly, daily, custom)
- 🎮 Simple Integration - Easy to use with Bevy's ECS
Compatibility
| Bevy Version | Plugin Version |
|---|---|
| 0.17 | 0.1 |
Installation
Add this to your Cargo.toml:
[]
= "0.17"
= "0.1"
Quick Start
use *;
use ;
Usage Examples
Setting Start Date/Time
use InGameClock;
Adjusting Clock Speed
Builder Pattern Configuration
Pausing the Clock
Reading Date and Time
Custom Formatting
Format Specifiers (via chrono):
%Y- Year (4 digits)%m- Month (01-12)%d- Day (01-31)%H- Hour 24h (00-23)%I- Hour 12h (01-12)%M- Minute (00-59)%S- Second (00-59)%p- AM/PM%B- Full month name%A- Full weekday name- See chrono format docs for more
Interval Events
The event system allows you to receive Bevy messages at specific in-game time intervals.
use ;
How It Works:
- Register intervals during setup or at any time during gameplay
- Events are triggered when the in-game time crosses interval boundaries
- Each event includes a
countfield tracking total occurrences since the clock started - Duplicate prevention: Registering the same interval multiple times is safe - only one tracker is created
- No unregistration: Once registered, intervals cannot be removed. Filter events in handlers if needed.
Available Intervals:
ClockInterval::Second- Every in-game secondClockInterval::Minute- Every 60 in-game secondsClockInterval::Hour- Every 3600 in-game secondsClockInterval::Day- Every 86400 in-game secondsClockInterval::Week- Every 7 in-game daysClockInterval::Custom(seconds)- Custom interval in seconds
API Reference
InGameClockPlugin
The main plugin. Add it to your Bevy app to enable clock functionality.
InGameClock Resource
| Field | Type | Description |
|---|---|---|
elapsed_seconds |
f64 |
Total in-game time elapsed in seconds since start |
speed |
f32 |
Speed multiplier (1.0 = real-time, 2.0 = double speed) |
paused |
bool |
Whether the clock is paused |
start_datetime |
NaiveDateTime |
The starting date/time for the clock |
Methods
Construction & Configuration
new()- Create a new clock with current UTC date/timewith_start_datetime(year, month, day, hour, minute, second)- Set specific start date/timewith_start(datetime)- Set start from aNaiveDateTimewith_speed(speed)- Set initial speed multiplierwith_day_duration(real_seconds_per_day)- Set speed by defining real seconds per in-game day
Control
pause()- Pause the clockresume()- Resume the clocktoggle_pause()- Toggle pause stateset_speed(speed)- Change the clock speed multiplierset_day_duration(real_seconds_per_day)- Change speed by day durationday_duration()- Get current day duration in real seconds
Reading Time
current_datetime()- Get currentNaiveDateTime(use chrono traits for advanced operations)current_date()- Get current date as(year, month, day)current_time()- Get current time as(hour, minute, second)as_hms()- Get time as(hours, minutes, seconds)tuple
Formatting
format_datetime(format)- Format date and time (default: "YYYY-MM-DD HH:MM:SS")format_date(format)- Format date only (default: "YYYY-MM-DD")format_time(format)- Format time only (default: "HH:MM:SS")
All formatting methods accept Option<&str> where None uses the default format, or Some("format_string") for custom chrono format strings.
Events
ClockIntervalEvent
Message sent when a registered time interval has passed.
Fields:
interval: ClockInterval- The interval that triggered the eventcount: u64- Total number of times this interval has passed
ClockInterval Enum
Defines time intervals for events:
Second,Minute,Hour,Day,Week- Built-in intervalsCustom(u32)- Custom interval in seconds
ClockCommands Trait
Extension trait for Commands to register intervals:
register_clock_interval(interval)- Register an interval to receive events
Examples
Run the basic example with interactive controls:
Basic Example
Interactive demo showing clock controls and display.
Controls:
Space- Pause/Resume+/-- Double/Halve speed1-6- Set day duration (30s, 60s, 5min, 10min, 20min, real-time)R- Reset clock
Events Example
Demonstrates the interval event system with multiple registered intervals.
Controls:
Space- Pause/Resume+/-- Speed Up/Down1-6- Toggle different interval events ON/OFFR- Reset clock
Digital Clock Example
Visual digital clock display with vintage styling, showing time in digital format with a date calendar display.
Controls:
Space- Pause/Resume+/-- Speed Up/DownR- Reset clock
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.