Time Functions Application
A comprehensive embedded C++ application demonstrating system time management, time zone handling, and various time-related operations on NetBurner embedded systems.
Overview
This application provides a complete example of how to work with system time functions in an embedded environment. It includes capabilities for setting system time via NTP or manual entry, managing time zones, and displaying time information in various formats.
Features
Core Time Operations
- System Time Display: Shows current time in multiple formats (GMT, UTC, Local)
- NTP Time Synchronization: Automatically sets system time using Network Time Protocol
- Manual Time Setting: Interactive prompts for setting system time manually
- Time Zone Management: Comprehensive time zone selection for US and international locations
- Boot Time Tracking: Displays elapsed time since system boot with millisecond precision
Time Formats Supported
- time_t: Calendar time since epoch
- struct tm: Calendar time structure with individual components
- GMT/UTC: Greenwich Mean Time and Coordinated Universal Time
- Local Time: Time adjusted for current time zone and daylight savings
Interactive Menu System
The application provides a user-friendly serial interface with the following options:
- Display system time information
- Set system time using NTP
- Set local time zone
- Set system time manually
- Test time zone functionality
- Display time since boot with millisecond precision 0. Reboot system
File Structure
main.cpp
The primary application file containing:
- displaySystemTime(): Comprehensive time display function showing various time formats
- setSystemTimeNTP(): NTP-based time synchronization
- setSystemTimeManual(): Manual time setting with parameter input
- setSystemTimeManualPrompt(): Interactive time setting via user prompts
- setSystemTimeZone(): Time zone selection interface
- displayTimeSinceBootInMsec(): Boot time calculation and display
- UserMain(): Main application loop with menu-driven interface
TimezoneTest.cpp
Specialized testing module for time zone functionality:
- testTimeZone(): Validates time zone settings and displays daylight savings transitions
- showHumanTimeGMT(): GMT time formatting and display
- showHumanTimeLocal(): Local time formatting with timezone offset
- getOffsetAt(): Calculates timezone offset for specific timestamps
Key Time Functions Demonstrated
Standard C Time Functions
time()
: Current calendar time retrieval
gmtime_r()
: Convert time_t to UTC struct tm (thread-safe)
localtime_r()
: Convert time_t to local struct tm (thread-safe)
mktime()
: Convert struct tm to time_t
timegm()
: Convert GMT struct tm to time_t
strftime()
: Format time strings for display
difftime()
: Calculate time differences
NetBurner-Specific Functions
SetTimeNTPFromPool()
: NTP synchronization
set_time()
: System time setting
tzsetchar()
: Time zone configuration
Get_msec()
: Millisecond-precision system uptime
Time Zone Support
The application includes comprehensive time zone support:
- US/Canada Time Zones: Major North American time zones
- International Time Zones: Global time zone coverage
- Daylight Savings Time: Automatic DST transitions
- POSIX Time Zone Strings: Standard timezone specification format
Time Zone Testing
The timezone test functionality provides:
- Current timezone display with offset
- Start and end of year boundary analysis
- Automatic daylight savings time transition detection
- Precise timing of DST changes
Usage Requirements
Prerequisites
Before using the time functions:
- Network Connectivity: Required for NTP synchronization
- System Initialization: Proper embedded system startup
- Time Zone Configuration: Must be set for accurate local time display
Compilation Dependencies
Time Precision and Accuracy
Precision Levels
- Second Precision: Standard time_t and struct tm operations
- Millisecond Precision: Boot time tracking via Get_msec()
- NTP Accuracy: Network-synchronized time with sub-second accuracy
Boot Time Calculations
The application calculates and displays:
- Days, hours, minutes, seconds since boot
- Remaining milliseconds for complete precision
- Multiple format options for different use cases
Error Handling
The application includes robust error handling for:
- NTP Failures: Graceful fallback when network time is unavailable
- Invalid Input: Input validation for manual time entry
- Time Zone Errors: Validation of timezone selection
- System Time Issues: Error reporting for time setting operations
Example Output
System Time Information ***
Seconds obtained from
time(): time_t = 1609459200
Seconds since program start = 1234
Time obtainted from gmtime_r():
UTC Time = Fri, 01 Jan 2021 00:00:00
Time obtainted from localtime_r():
Local Time = Thu, 31 Dec 2020 16:00:00 PST [-0800 offset UTC]
Current Time Zone: PST (-0800)
Daylight Savings Time Changes:
At Time: Sun, 08 Mar 2020 01:59:59 (-0800)
Offset change is: -28800 to -25200
time_t time(time_t *pt)
Gets the current system GMT time.
Development Notes
Thread Safety
- Uses thread-safe time functions (
gmtime_r
, localtime_r
)
- Suitable for multi-threaded embedded environments
- Proper buffer management for string operations
Memory Management
- Static buffer allocation for time string formatting
- Minimal dynamic memory usage
- Embedded-friendly memory patterns
Performance Considerations
- Efficient boot time calculations using integer arithmetic
- Minimal system calls for time operations
- Optimized timezone transition detection algorithms
Integration
This application serves as a reference implementation for:
- Real-time clock (RTC) integration
- Network time synchronization systems
- Time-aware embedded applications
- Logging systems requiring accurate timestamps
- Scheduling and timing-critical applications
The modular design allows individual functions to be extracted and integrated into larger embedded systems requiring time management capabilities.