Created
November 6, 2013 22:55
-
-
Save lian/7345710 to your computer and use it in GitHub Desktop.
locale.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ffi' | |
class Time | |
module Strftime | |
extend FFI::Library | |
ffi_lib 'c' | |
attach_function :setlocale, [:int, :string], :uint | |
attach_function :strftime, [:string, :long, :string, :pointer], :long | |
setlocale(LC_ALL = 6, "de_DE") | |
end | |
def strftime_c(format) | |
struct = to_a | |
struct[-2] = struct[-2] ? 1 : 0 | |
tm = FFI::MemoryPointer.from_string(struct.pack('iiiiiiiilZ*')) | |
str = "\xff" * 1024 | |
len = Strftime.strftime(str, str.length, format, tm) | |
str[0, len] | |
end | |
end | |
now = Time.now | |
p ruby: now.strftime('%a') | |
p libc: now.strftime_c('%a') | |
__END__ | |
require 'dl' | |
require 'dl/import' | |
module Time::Strftime | |
extend DL::Importer | |
begin | |
dlload 'libc.so' | |
rescue DL::DLError | |
begin | |
dlload 'libc.so.6' | |
rescue DL::DLError | |
dlload 'libSystem.dylib' | |
end | |
end | |
extern 'long strftime(char *, long, char *, struct tm *)' | |
extern 'char *setlocale(int, char *)' | |
end | |
class Time | |
def strftime_c format | |
struct = to_a | |
struct[-2] = struct[-2] ? 1 : 0 | |
tm = struct.pack 'iiiiiiiilZ*' | |
str = "\0" * 1024 | |
len = Time::Strftime.strftime str, str.length, format, tm | |
str[0, len] | |
end | |
end | |
puts `locale` | |
Time::Strftime.setlocale(6, "") | |
now = Time.now | |
p ruby: now.strftime('%a') | |
p libc: now.strftime_c('%a') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment