jjzjj

date_default_timezone_set

全部标签

ruby - 为什么 Date 类的新实例的默认年份等于 -4712?

运行:require'date'y=Date.newputs"defaultyearis#{y.year}"输出:defaultyearis-4712Date对象的年份默认为-4712是否有任何特殊原因?我认为也许有技术(或传统)原因。我用谷歌搜索并在Ruby文档(2.2.3)中查找了它,但我找不到关于为什么的答案。 最佳答案 公元前4713年1月1日是Julianday的开始,来自维基:TheJulianDayNumber(JDN)istheintegerassignedtoawholesolardayintheJulianday

Ruby 习语 : method call or else default

在Ruby中执行此操作的正确方法是什么?defcallOrElse(obj,method,default)ifobj.respond_to?(method)obj.__send__(method)elsedefaultendend 最佳答案 因为还没有提供答案:result=obj.methodrescuedefault与@slhck一样,当我知道obj很有可能不会响应方法时,我可能不会使用它,但这是一个选项。 关于Ruby习语:methodcallorelsedefault,我们在St

Ruby Date 1 个月前到 String

我需要在Ruby中计算从今天起1个月的日期,并将其转换为String格式:yyyy-dd-mmThh:MM:ss(e.g.2014-08-26T00:00:00)我试过:(DateTime.now-Date.today.prev_month).to_datetime.strftime("%FT%T")但我得到一个方法不存在的异常。 最佳答案 试试这个:require'date'd=Date.today.prev_month#orDate.today.next_month,dependingwhatyouwant=>#d.strfti

ruby-on-rails - 将 Date 对象转换为 TimeWithZone

我需要将Date对象转换为TimeWithZone对象,表示给定时区中那一天的开始。以下方法可行,但似乎太复杂了,因为它需要我将日期转换为字符串:?>date=Date.parse("2010-02-17")=>Wed,17Feb2010>>ActiveSupport::TimeZone['EasternTime(US&Canada)'].parse(date.to_s)=>Wed,17Feb201000:00:00EST-05:00>>ActiveSupport::TimeZone['UTC'].parse(date.to_s)=>Wed,17Feb201000:00:00UTC00

ruby-on-rails - 如何匹配YYYY-MM-DDThh :mm:ssTZD timezone format in ruby

我想检查在ruby​​中输入的日期的iso8601格式,比如start_date="2011/05/05"应该匹配2011-05-05T00格式:00:00-04:00并相应地返回错误。我们应该在这里使用正则表达式还是为此提供任何方法? 最佳答案 听起来像你想要的Time.iso8601:require'time'iso=Time.iso8601(start_date)参见thisblogpost获取更多信息。编辑:这是一个简短但完整的有效测试程序:require'time'text="2011-05-05T00:00:00-04:

ruby - Chef : Can a variable set within one ruby_block be used later in a recipe?

假设我有一个变量directory_list,我在名为get_directory_list的ruby​​_block中定义和设置了它。我可以稍后在我的Recipe中使用directory_list吗,或者编译/收敛过程会阻止这种情况吗?例子:ruby_block"get_file_list"doblockdotransferred_files=Dir['/some/dir/*']endendtransferred_files.eachdo|file|file"#{file}"dogroup"woohoo"user"woohoo"endend 最佳答案

Ruby 设置类 : equality of sets

根据RubySet类的文档,“==如果两个集合相等,则返回true。每对元素的相等性根据Object#eql?定义。可以使用Date对象来演示其本质,其中包含不同Date对象但具有相同日期的集合比较相等:require'set'd1=Date.today#=>Thu,30Sep2010putsd1.object_id#=>2211539680d2=Date.today+1#=>Fri,01Oct2010putsd2.object_id#=>2211522320set1=Set.new([d1,d2])d11=Date.today#=>Thu,30Sep2010putsd11.objec

ruby - Date::today 未定义?

为什么只有Date类的某些方法没有显式加载:require'date'线?例如:irb(main):002:0>Date.todayNoMethodError:undefinedmethod`today'forDate:Classfrom(irb):2from/Users/mwlang/.rvm/rubies/ruby-2.0.0-p0/bin/irb:16:in`'然后……irb(main):003:0>require'date'=>true导致...irb(main):004:0>Date.today=>#http://ruby-doc.org/stdlib-2.0/libdoc/

Ruby - fork 进程上的 set_trace_func

我想实现以下目标:构建一个Ruby命令行实用程序来注册一些set_trace_func事件,然后调用您传递给它的任何ruby​​可执行参数(比如rspec)。注册的事件然后转移到调用的命令。myutility的伪代码:set_trace_func()#Setsomeeventshereexec(ARGV.join(''))#Executeargumentpassed然后调用myutilityrspec。我的目标是实际在任意命令上注册跟踪点(只要它们使用ruby​​垫片)。我尝试过的事情:exec不起作用,原因很明显(它完全取代了进程)。popen、系统、反引号。这些启动了一个独立的过程

ruby - ruby Date.today 和 DateTime.now 的日期错误

我已经使用RVM安装了ruby​​-1.8.6-p383。系统ruby是1.9.1_p378-1我在使用ruby​​1.8时从Date.today和DateTime.now得到错误的日期。而Time.now是正确的:irb(main):002:0>DateTime.now.to_s=>"2126--1-10618T11:23:43+00:00"irb(main):004:0>Date.today.to_s=>"2126--1-10618"irb(main):005:0>Time.now=>ThuJan2811:55:27+00002010如果我切换到ruby​​1.9,一切都很好:ir