mrbgems/mruby-strftime/README.md
Time#strftime implementation for mruby.
This gem provides the strftime method for Time objects in mruby, enabling
formatted time string output using standard format directives.
require 'mruby-strftime'
t = Time.new(2023, 12, 25, 10, 30, 45)
t.strftime("%Y-%m-%d") #=> "2023-12-25"
t.strftime("%H:%M:%S") #=> "10:30:45"
t.strftime("%Y-%m-%d %H:%M:%S") #=> "2023-12-25 10:30:45"
t.strftime("%A, %B %d, %Y") #=> "Monday, December 25, 2023"
The strftime method supports standard format directives. The exact set of
available directives depends on your system's strftime(3) implementation.
Common directives include:
%Y - Year with century (e.g., 2023)%y - Year without century (00-99)%m - Month of the year (01-12)%B - Full month name (e.g., "December")%b - Abbreviated month name (e.g., "Dec")%d - Day of the month (01-31)%j - Day of the year (001-366)%H - Hour of the day, 24-hour clock (00-23)%I - Hour of the day, 12-hour clock (01-12)%M - Minute of the hour (00-59)%S - Second of the minute (00-60)%p - AM/PM indicator%Z - Timezone name or abbreviation%A - Full weekday name (e.g., "Monday")%a - Abbreviated weekday name (e.g., "Mon")%w - Day of the week (0-6, Sunday is 0)%u - Day of the week (1-7, Monday is 1)%c - Preferred date and time representation%x - Preferred date representation%X - Preferred time representation%F - ISO 8601 date format (equivalent to %Y-%m-%d)%T - ISO 8601 time format (equivalent to %H:%M:%S)%% - Literal % character%n - Newline character%t - Tab characterThe method respects the timezone of the Time object:
t_utc = Time.utc(2023, 12, 25, 12, 0, 0)
t_local = Time.local(2023, 12, 25, 12, 0, 0)
t_utc.strftime("%Y-%m-%d %H:%M:%S %Z")
#=> "2023-12-25 12:00:00 UTC"
t_local.strftime("%Y-%m-%d %H:%M:%S %z")
#=> "2023-12-25 12:00:00 +0900" (example for JST)
Unlike some implementations, mruby-strftime correctly handles NUL bytes (\0)
embedded in format strings, preserving them in the output:
t = Time.gm(2023, 12, 25)
result = t.strftime("year\0%Y")
result.length #=> 9 (includes the NUL byte)
This behavior maintains Ruby's string semantics where strings are length-based rather than null-terminated.
The implementation uses dynamic buffer allocation:
This gem uses the system's strftime(3) function for formatting. The exact
behavior of format directives may vary slightly between platforms, particularly
for:
mruby-time - Required for Time class supportAdd to your build_config.rb:
conf.gem :core => 'mruby-strftime'
Or include via gembox:
conf.gembox 'stdlib-ext' # Includes mruby-strftime
MIT License - See mruby's main license file for details.