Ruby 2.4

Date

Date类

Parent:ObjectIncluded modules:Comparable

日期和日期时间类 - Tadayoshi Funaba 1998-2011

'date'提供了两个类:Date和DateTime。

术语和定义

一些术语和定义基于ISO 8601和JIS X 0301。

日历日期

日历日期是日历年中的特定日期,由该年的日历月内的序号标识。

在这些阶级中,这就是所谓的“公民”。

序号日期

序号日期是一年中某个日历年的特定日期,由其序号标识。

在这些课程中,这就是所谓的“序数”。

周日期

星期日期是由日历星期和星期号码标识的日期。

日历周期为一个日历年内的七天期限,从一个星期一开始,在一年内通过其序号标识; 今年的第一个日历周是包含当年的第一个周四的日历周。在公历中,这相当于包含1月4日的那一周。

在这些类别中,这就是所谓的“商业”。

朱利安日号码

Julian日数是自中午(格林威治标准时间)公元前4713年1月1日(Julian日历)起的历时天数。

在这份文件中,天文学的朱利安天数与原始的朱利安天数相同。按时间顺序排列的朱利安日数是朱利安日数的变化。它的日子从当地时间午夜开始。

在这份文件中,当简单地出现“儒略日数”一词时,它只是指“按时间顺序的儒略日数”,而不是原文。

在那些类中,这些是所谓的“ajd”和“jd”。

修改的儒略日号码

经修改的儒略日号码自公元1858年11月17日午夜(协调世界时)算起(格里历),历时天数。

在本文件中,天文修正的朱利安天数与原始修正的朱利安天数相同。按时间顺序修改的儒略日数是修正的儒略日数的变体。它的日子从当地时间午夜开始。

在这个文件中,当“修改的儒略日数”这个词出现时,它只是指“按时间顺序修改的儒略日数”,而不是原文。

在那些课上,那些是所谓的“amjd”和“mjd”。

日期

Object的一个子类,包含Comparable模块并可轻松处理日期。

Date对象是使用:: new,:: jd,:: ordinal,:: commercial,:: parse,:: strptime,:: today,Time#to_date等创建的。

require 'date' Date.new(2001,2,3) #=> #<Date: 2001-02-03 ...> Date.jd(2451944) #=> #<Date: 2001-02-03 ...> Date.ordinal(2001,34) #=> #<Date: 2001-02-03 ...> Date.commercial(2001,5,6) #=> #<Date: 2001-02-03 ...> Date.parse('2001-02-03') #=> #<Date: 2001-02-03 ...> Date.strptime('03-02-2001', '%d-%m-%Y') #=> #<Date: 2001-02-03 ...> Time.new(2001,2,3).to_date #=> #<Date: 2001-02-03 ...>

所有的日期对象都是不可变的; 因此不能修改自己。

日期对象的概念可以表示为日计数,偏移量和日历改革日期的元组。

日计数表示时间维度的绝对位置。偏移量是相对调整量,它根据日计数确定解码的本地时间。日历改革日代表新风格的开始日期。西方的古老风格是凯撒采用的儒略历。新格式是公历,这是许多国家的现行公民日历。

日计数实际上是天文学的儒略天数。此类中的偏移量通常为零,不能直接指定。

日期对象可以使用可选参数创建,日历重组日期为Julian日数,应为2298874至2426355或负/正无限。默认值是Date::ITALY(2299161 = 1582-10-15)。另请参阅sample / cal.rb。

$ ruby sample/cal.rb -c it 10 1582 October 1582 S M Tu W Th F S 1 2 3 4 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 $ ruby sample/cal.rb -c gb 9 1752 September 1752 S M Tu W Th F S 1 2 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

日期对象有各种方法。查看每个参考。

d = Date.parse('3rd Feb 2001') #=> #<Date: 2001-02-03 ...> d.year #=> 2001 d.mon #=> 2 d.mday #=> 3 d.wday #=> 6 d += 1 #=> #<Date: 2001-02-04 ...> d.strftime('%a %d %b %Y') #=> "Sun 04 Feb 2001"

常量

ABBR_DAYNAMES

英文缩写日期名称的字符串数组。第一个是“太阳”。

ABBR_MONTHNAMES

英文缩写月份名称字符串数组。第一个元素是零。

DAYNAMES

用英文表示星期几全名的字符串数组。第一个是“星期天”。

ENGLAND

英格兰及其殖民地日历改革日的朱利安日数。

GREGORIAN

格雷戈里历日历改革日的朱利安日数。

ITALY

意大利和一些天主教国家日历改革日的朱利安日数。

JULIAN

朱利安历法日历改革日的朱利安日数。

MONTHNAMES

英文全月份的字符串数组。第一个元素是零。

公共类方法

_httpdate(string) → hash 显示源文件

返回解析元素的散列。

static VALUE date_s__httpdate(VALUE klass, VALUE str) { return date__httpdate(str }

_iso8601(string) → hash 显示源文件

返回解析元素的散列。

static VALUE date_s__iso8601(VALUE klass, VALUE str) { return date__iso8601(str }

_jisx0301(string) → hash 显示源文件

返回解析元素的散列。

static VALUE date_s__jisx0301(VALUE klass, VALUE str) { return date__jisx0301(str }

_parse(string, comp=true) → hash 显示源文件

解析给定的日期和时间表示,并返回解析元素的散列。此方法不起验证器的作用。

如果可选的第二个参数为true并且检测到的年份在范围“00”至“99”中,则将年份视为2位数字形式并使其填满。

Date._parse('2001-02-03') #=> {:year=>2001, :mon=>2, :mday=>3}

static VALUE date_s__parse(int argc, VALUE *argv, VALUE klass) { return date_s__parse_internal(argc, argv, klass }

_rfc2822(string) → hash 显示源文件

返回解析元素的散列。

static VALUE date_s__rfc2822(VALUE klass, VALUE str) { return date__rfc2822(str }

_rfc3339(string) → hash 显示源文件

返回解析元素的散列。

static VALUE date_s__rfc3339(VALUE klass, VALUE str) { return date__rfc3339(str }

_rfc822(string) → hash 显示源文件

返回解析元素的散列。

static VALUE date_s__rfc2822(VALUE klass, VALUE str) { return date__rfc2822(str }

_strptime(string, format='%F') → hash 显示源文件

用给定的模板解析给定的日期和时间表示,并返回经过分析的元素的散列。_strptime不支持与strftime不同的标志和宽度的规范。

Date._strptime('2001-02-03', '%Y-%m-%d') #=> {:year=>2001, :mon=>2, :mday=>3}

另见strptime(3)和strftime。

static VALUE date_s__strptime(int argc, VALUE *argv, VALUE klass) { return date_s__strptime_internal(argc, argv, klass, "%F" }

_xmlschema(string) → hash 显示源文件

返回解析元素的散列。

static VALUE date_s__xmlschema(VALUE klass, VALUE str) { return date__xmlschema(str }

civil([year=-4712[, month=1[, mday=1, start=Date::ITALY]]]) → date 显示源文件

创建一个表示给定日历日期的日期对象。

在这门课上,BCE的年数是天文数字。因此,第一年的前一年是零年,零年前一年是第一年。月份和月份的日期应该是负数或正数(作为从年/月底到负数的相对月/日)。他们不应该是零。

最后一个参数应该是Julian日数,表示日历改革的日子。Date :: ITALY(2299161 = 1582-10-15),Date :: ENGLAND(2361222 = 1752-09-14),Date :: GREGORIAN(预测格列高利历)和Date :: JULIAN(Julie预设日历)可以被指定为日历改革的一天。

Date.new(2001) #=> #<Date: 2001-01-01 ...> Date.new(2001,2,3) #=> #<Date: 2001-02-03 ...> Date.new(2001,2,-1) #=> #<Date: 2001-02-28 ...>

See also ::jd.

static VALUE date_s_civil(int argc, VALUE *argv, VALUE klass) { VALUE vy, vm, vd, vsg, y, fr, fr2, ret; int m, d; double sg; rb_scan_args(argc, argv, "04", &vy, &vm, &vd, &vsg y = INT2FIX(-4712 m = 1; d = 1; fr2 = INT2FIX(0 sg = DEFAULT_SG; switch (argc) { case 4: val2sg(vsg, sg case 3: num2int_with_frac(d, positive_inf case 2: m = NUM2INT(vm case 1: y = vy; } if (guess_style(y, sg) < 0) { VALUE nth; int ry, rm, rd; if (!valid_gregorian_p(y, m, d, &nth, &ry, &rm, &rd)) rb_raise(rb_eArgError, "invalid date" ret = d_simple_new_internal(klass, nth, 0, sg, ry, rm, rd, HAVE_CIVIL } else { VALUE nth; int ry, rm, rd, rjd, ns; if (!valid_civil_p(y, m, d, sg, &nth, &ry, &rm, &rd, &rjd, &ns)) rb_raise(rb_eArgError, "invalid date" ret = d_simple_new_internal(klass, nth, rjd, sg, ry, rm, rd, HAVE_JD | HAVE_CIVIL } add_frac( return ret; }

commercial([cwyear=-4712[, cweek=1[, cwday=1, start=Date::ITALY]]]) → date 显示源文件

创建一个表示给定的星期日期的日期对象。

星期和星期应该是负数或正数(作为从年底/周到负数的相对周/日)。他们不应该是零。

Date.commercial(2001) #=> #<Date: 2001-01-01 ...> Date.commercial(2002) #=> #<Date: 2001-12-31 ...> Date.commercial(2001,5,6) #=> #<Date: 2001-02-03 ...>

gregorian_leap?(year) → bool Show source

如果给定的年份是预测格里历的闰年,则返回true。

Date.gregorian_leap?(1900) #=> false Date.gregorian_leap?(2000) #=> true

static VALUE date_s_gregorian_leap_p(VALUE klass, VALUE y) { VALUE nth; int ry; decode_year(y, -1, &nth, &ry return f_boolcast(c_gregorian_leap_p(ry) }

httpdate(string='Mon, 01 Jan -4712 00:00:00 GMT', start=Date::ITALY) → date Show source

根据某些RFC 2616格式通过解析字符串来创建新的Date对象。

Date.httpdate('Sat, 03 Feb 2001 00:00:00 GMT') #=> #<Date: 2001-02-03 ...>

static VALUE date_s_httpdate(int argc, VALUE *argv, VALUE klass) { VALUE str, sg; rb_scan_args(argc, argv, "02", &str, &sg switch (argc) { case 0: str = rb_str_new2("Mon, 01 Jan -4712 00:00:00 GMT" case 1: sg = INT2FIX(DEFAULT_SG } { VALUE hash = date_s__httpdate(klass, str return d_new_by_frags(klass, hash, sg } }

iso8601(string='-4712-01-01', start=Date::ITALY) → date Show source

根据某些典型的ISO 8601格式通过解析字符串来创建新的Date对象。

Date.iso8601('2001-02-03') #=> #<Date: 2001-02-03 ...> Date.iso8601('20010203') #=> #<Date: 2001-02-03 ...> Date.iso8601('2001-W05-6') #=> #<Date: 2001-02-03 ...>

static VALUE date_s_iso8601(int argc, VALUE *argv, VALUE klass) { VALUE str, sg; rb_scan_args(argc, argv, "02", &str, &sg switch (argc) { case 0: str = rb_str_new2("-4712-01-01" case 1: sg = INT2FIX(DEFAULT_SG } { VALUE hash = date_s__iso8601(klass, str return d_new_by_frags(klass, hash, sg } }

jd([jd=0, start=Date::ITALY]) → date Show source

创建一个表示给定的按时间顺序排列的儒略日数的日期对象。

Date.jd(2451944) #=> #<Date: 2001-02-03 ...> Date.jd(2451945) #=> #<Date: 2001-02-04 ...> Date.jd(0) #=> #<Date: -4712-01-01 ...>

另见:: new。

static VALUE date_s_jd(int argc, VALUE *argv, VALUE klass) { VALUE vjd, vsg, jd, fr, fr2, ret; double sg; rb_scan_args(argc, argv, "02", &vjd, &vsg jd = INT2FIX(0 fr2 = INT2FIX(0 sg = DEFAULT_SG; switch (argc) { case 2: val2sg(vsg, sg case 1: num2num_with_frac(jd, positive_inf } { VALUE nth; int rjd; decode_jd(jd, &nth, &rjd ret = d_simple_new_internal(klass, nth, rjd, sg, 0, 0, 0, HAVE_JD } add_frac( return ret; }

jisx0301(string='-4712-01-01', start=Date::ITALY) → date Show source

根据一些典型的JIS X 0301格式通过解析字符串来创建新的Date对象。

Date.jisx0301('H13.02.03') #=> #<Date: 2001-02-03 ...>

static VALUE date_s_jisx0301(int argc, VALUE *argv, VALUE klass) { VALUE str, sg; rb_scan_args(argc, argv, "02", &str, &sg switch (argc) { case 0: str = rb_str_new2("-4712-01-01" case 1: sg = INT2FIX(DEFAULT_SG } { VALUE hash = date_s__jisx0301(klass, str return d_new_by_frags(klass, hash, sg } }

json_create(object) Show source

通过将Julian年y,月份m,日d和日历改革日sg转换为日期来反序列化JSON字符串。

# File ext/json/lib/json/add/date.rb, line 11 def self.json_create(object) civil(*object.values_at('y', 'm', 'd', 'sg')) end

julian_leap?(year) → bool Show source

如果给定年份是预测Julian日历的闰年,则返回true。

Date.julian_leap?(1900) #=> true Date.julian_leap?(1901) #=> false

static VALUE date_s_julian_leap_p(VALUE klass, VALUE y) { VALUE nth; int ry; decode_year(y, +1, &nth, &ry return f_boolcast(c_julian_leap_p(ry) }

leap?(year) → bool Show source

如果给定的年份是预测格里历的闰年,则返回true。

Date.gregorian_leap?(1900) #=> false Date.gregorian_leap?(2000) #=> true

static VALUE date_s_gregorian_leap_p(VALUE klass, VALUE y) { VALUE nth; int ry; decode_year(y, -1, &nth, &ry return f_boolcast(c_gregorian_leap_p(ry) }

new([year=-4712[, month=1[, mday=1, start=Date::ITALY]]]) → date Show source

创建一个表示给定日历日期的日期对象。

在这个类中,BCE的年数是天文数字。因此,第一年的前一年是零年,零年前一年是第一年。月份和月份的日期应该是负数或正数(作为从年/月底到负数的相对月/日)。他们不应该是零。

最后一个参数应该是Julian日数,表示日历改革的日子。Date :: ITALY(2299161 = 1582-10-15),Date :: ENGLAND(2361222 = 1752-09-14),Date :: GREGORIAN(预示格列高利历)和Date :: JULIAN(预先Julian日历)可以被指定为日历改革的一天。

Date.new(2001) #=> #<Date: 2001-01-01 ...> Date.new(2001,2,3) #=> #<Date: 2001-02-03 ...> Date.new(2001,2,-1) #=> #<Date: 2001-02-28 ...>

另见:: jd。

static VALUE date_s_civil(int argc, VALUE *argv, VALUE klass) { VALUE vy, vm, vd, vsg, y, fr, fr2, ret; int m, d; double sg; rb_scan_args(argc, argv, "04", &vy, &vm, &vd, &vsg y = INT2FIX(-4712 m = 1; d = 1; fr2 = INT2FIX(0 sg = DEFAULT_SG; switch (argc) { case 4: val2sg(vsg, sg case 3: num2int_with_frac(d, positive_inf case 2: m = NUM2INT(vm case 1: y = vy; } if (guess_style(y, sg) < 0) { VALUE nth; int ry, rm, rd; if (!valid_gregorian_p(y, m, d, &nth, &ry, &rm, &rd)) rb_raise(rb_eArgError, "invalid date" ret = d_simple_new_internal(klass, nth, 0, sg, ry, rm, rd, HAVE_CIVIL } else { VALUE nth; int ry, rm, rd, rjd, ns; if (!valid_civil_p(y, m, d, sg, &nth, &ry, &rm, &rd, &rjd, &ns)) rb_raise(rb_eArgError, "invalid date" ret = d_simple_new_internal(klass, nth, rjd, sg, ry, rm, rd, HAVE_JD | HAVE_CIVIL } add_frac( return ret; }

ordinal([year=-4712[, yday=1, start=Date::ITALY]]) → date Show source

创建一个表示给定的序号日期的日期对象。

一年中的某一天应该是一个负数或一个正数(作为从负数到年底的相对日期)。它不应该是零。

Date.ordinal(2001) #=> #<Date: 2001-01-01 ...> Date.ordinal(2001,34) #=> #<Date: 2001-02-03 ...> Date.ordinal(2001,-1) #=> #<Date: 2001-12-31 ...>

另见:: jd和:: new。

static VALUE date_s_ordinal(int argc, VALUE *argv, VALUE klass) { VALUE vy, vd, vsg, y, fr, fr2, ret; int d; double sg; rb_scan_args(argc, argv, "03", &vy, &vd, &vsg y = INT2FIX(-4712 d = 1; fr2 = INT2FIX(0 sg = DEFAULT_SG; switch (argc) { case 3: val2sg(vsg, sg case 2: num2int_with_frac(d, positive_inf case 1: y = vy; } { VALUE nth; int ry, rd, rjd, ns; if (!valid_ordinal_p(y, d, sg, &nth, &ry, &rd, &rjd, &ns)) rb_raise(rb_eArgError, "invalid date" ret = d_simple_new_internal(klass, nth, rjd, sg, 0, 0, 0, HAVE_JD } add_frac( return ret; }

parse(string='-4712-01-01'[, comp=true, start=Date::ITALY]) → date Show source

解析给定的日期和时间表示,并创建一个日期对象。此方法不起验证器的作用。

如果可选的第二个参数为true并且检测到的年份在范围“00”至“99”中,则将年份视为2位数字形式并使其填满。

Date.parse('2001-02-03') #=> #<Date: 2001-02-03 ...> Date.parse('20010203') #=> #<Date: 2001-02-03 ...> Date.parse('3rd Feb 2001') #=> #<Date: 2001-02-03 ...>

static VALUE date_s_parse(int argc, VALUE *argv, VALUE klass) { VALUE str, comp, sg; rb_scan_args(argc, argv, "03", &str, &comp, &sg switch (argc) { case 0: str = rb_str_new2("-4712-01-01" case 1: comp = Qtrue; case 2: sg = INT2FIX(DEFAULT_SG } { VALUE argv2[2], hash; argv2[0] = str; argv2[1] = comp; hash = date_s__parse(2, argv2, klass return d_new_by_frags(klass, hash, sg } }

rfc2822(string='Mon, 1 Jan -4712 00:00:00 +0000', start=Date::ITALY) → date Show source

根据一些典型的RFC 2822格式通过解析字符串来创建新的Date对象。

Date.rfc2822('Sat, 3 Feb 2001 00:00:00 +0000') #=> #<Date: 2001-02-03 ...>

static VALUE date_s_rfc2822(int argc, VALUE *argv, VALUE klass) { VALUE str, sg; rb_scan_args(argc, argv, "02", &str, &sg switch (argc) { case 0: str = rb_str_new2("Mon, 1 Jan -4712 00:00:00 +0000" case 1: sg = INT2FIX(DEFAULT_SG } { VALUE hash = date_s__rfc2822(klass, str return d_new_by_frags(klass, hash, sg } }

rfc3339(string='-4712-01-01T00:00:00+00:00', start=Date::ITALY) → date Show source

根据某些典型的RFC 3339格式通过解析字符串来创建新的Date对象。

Date.rfc3339('2001-02-03T04:05:06+07:00') #=> #<Date: 2001-02-03 ...>

static VALUE date_s_rfc3339(int argc, VALUE *argv, VALUE klass) { VALUE str, sg; rb_scan_args(argc, argv, "02", &str, &sg switch (argc) { case 0: str = rb_str_new2("-4712-01-01T00:00:00+00:00" case 1: sg = INT2FIX(DEFAULT_SG } { VALUE hash = date_s__rfc3339(klass, str return d_new_by_frags(klass, hash, sg } }

rfc822(string='Mon, 1 Jan -4712 00:00:00 +0000', start=Date::ITALY) → date Show source

根据一些典型的RFC 2822格式通过解析字符串来创建新的Date对象。

Date.rfc2822('Sat, 3 Feb 2001 00:00:00 +0000') #=> #<Date: 2001-02-03 ...>

static VALUE date_s_rfc2822(int argc, VALUE *argv, VALUE klass) { VALUE str, sg; rb_scan_args(argc, argv, "02", &str, &sg switch (argc) { case 0: str = rb_str_new2("Mon, 1 Jan -4712 00:00:00 +0000" case 1: sg = INT2FIX(DEFAULT_SG } { VALUE hash = date_s__rfc2822(klass, str return d_new_by_frags(klass, hash, sg } }

strptime([string='-4712-01-01'[, format='%F', start=Date::ITALY]]) → date Show source

用给定的模板解析给定的日期和时间表示,并创建一个日期对象。strptime不支持与strftime不同的标志和宽度的规范。

Date.strptime('2001-02-03', '%Y-%m-%d') #=> #<Date: 2001-02-03 ...> Date.strptime('03-02-2001', '%d-%m-%Y') #=> #<Date: 2001-02-03 ...> Date.strptime('2001-034', '%Y-%j') #=> #<Date: 2001-02-03 ...> Date.strptime('2001-W05-6', '%G-W%V-%u') #=> #<Date: 2001-02-03 ...> Date.strptime('2001 04 6', '%Y %U %w') #=> #<Date: 2001-02-03 ...> Date.strptime('2001 05 6', '%Y %W %u') #=> #<Date: 2001-02-03 ...> Date.strptime('sat3feb01', '%a%d%b%y') #=> #<Date: 2001-02-03 ...>

另见strptime(3)和strftime。

static VALUE date_s_strptime(int argc, VALUE *argv, VALUE klass) { VALUE str, fmt, sg; rb_scan_args(argc, argv, "03", &str, &fmt, &sg switch (argc) { case 0: str = rb_str_new2("-4712-01-01" case 1: fmt = rb_str_new2("%F" case 2: sg = INT2FIX(DEFAULT_SG } { VALUE argv2[2], hash; argv2[0] = str; argv2[1] = fmt; hash = date_s__strptime(2, argv2, klass return d_new_by_frags(klass, hash, sg } }

today(start=Date::ITALY) → date Show source

创建一个表示当前日期的日期对象。

Date.today #=> #<Date: 2011-06-11 ...>

static VALUE date_s_today(int argc, VALUE *argv, VALUE klass) { VALUE vsg, nth, ret; double sg; time_t t; struct tm tm; int y, ry, m, d; rb_scan_args(argc, argv, "01", &vsg if (argc < 1) sg = DEFAULT_SG; else val2sg(vsg, sg if (time(&t) == -1) rb_sys_fail("time" tzset( if (!localtime_r(&t, &tm)) rb_sys_fail("localtime" y = tm.tm_year + 1900; m = tm.tm_mon + 1; d = tm.tm_mday; decode_year(INT2FIX(y), -1, &nth, &ry ret = d_simple_new_internal(klass, nth, 0, GREGORIAN, ry, m, d, HAVE_CIVIL { get_d1(ret set_sg(dat, sg } return ret; }

valid_civil?(year, month, mday, start=Date::ITALY) → bool Show source

如果给定日历日期有效,则返回true,否则返回false。

Date.valid_date?(2001,2,3) #=> true Date.valid_date?(2001,2,29) #=> false

另见:: jd和:: civil。

static VALUE date_s_valid_civil_p(int argc, VALUE *argv, VALUE klass) { VALUE vy, vm, vd, vsg; VALUE argv2[4]; rb_scan_args(argc, argv, "31", &vy, &vm, &vd, &vsg argv2[0] = vy; argv2[1] = vm; argv2[2] = vd; if (argc < 4) argv2[3] = INT2FIX(DEFAULT_SG else argv2[3] = vsg; if (NIL_P(valid_civil_sub(4, argv2, klass, 0))) return Qfalse; return Qtrue; }

valid_commercial?(cwyear, cweek, cwday, start=Date::ITALY) → bool Show source

如果给定的星期日期有效,则返回true,否则返回false。

Date.valid_commercial?(2001,5,6) #=> true Date.valid_commercial?(2001,5,8) #=> false

另见:: jd和:: commercial。

static VALUE date_s_valid_commercial_p(int argc, VALUE *argv, VALUE klass) { VALUE vy, vw, vd, vsg; VALUE argv2[4]; rb_scan_args(argc, argv, "31", &vy, &vw, &vd, &vsg argv2[0] = vy; argv2[1] = vw; argv2[2] = vd; if (argc < 4) argv2[3] = INT2FIX(DEFAULT_SG else argv2[3] = vsg; if (NIL_P(valid_commercial_sub(4, argv2, klass, 0))) return Qfalse; return Qtrue; }

valid_date?(year, month, mday, start=Date::ITALY) → bool Show source

如果给定日历日期有效,则返回true,否则返回false。

Date.valid_date?(2001,2,3) #=> true Date.valid_date?(2001,2,29) #=> false

另见:: jd和:: civil。

static VALUE date_s_valid_civil_p(int argc, VALUE *argv, VALUE klass) { VALUE vy, vm, vd, vsg; VALUE argv2[4]; rb_scan_args(argc, argv, "31", &vy, &vm, &vd, &vsg argv2[0] = vy; argv2[1] = vm; argv2[2] = vd; if (argc < 4) argv2[3] = INT2FIX(DEFAULT_SG else argv2[3] = vsg; if (NIL_P(valid_civil_sub(4, argv2, klass, 0))) return Qfalse; return Qtrue; }

valid_jd?(jd, start=Date::ITALY) → bool Show source

只是返回true。这是无稽之谈,但是对于对称而言的。

Date.valid_jd?(2451944) #=> true

另见:: jd。

static VALUE date_s_valid_jd_p(int argc, VALUE *argv, VALUE klass) { VALUE vjd, vsg; VALUE argv2[2]; rb_scan_args(argc, argv, "11", &vjd, &vsg argv2[0] = vjd; if (argc < 2) argv2[1] = INT2FIX(DEFAULT_SG else argv2[1] = vsg; if (NIL_P(valid_jd_sub(2, argv2, klass, 0))) return Qfalse; return Qtrue; }

valid_ordinal?(year, yday, start=Date::ITALY) → bool Show source

如果给定的序号有效,则返回true;否则返回false。

Date.valid_ordinal?(2001,34) #=> true Date.valid_ordinal?(2001,366) #=> false

另见:: jd和:: ordinal。

static VALUE date_s_valid_ordinal_p(int argc, VALUE *argv, VALUE klass) { VALUE vy, vd, vsg; VALUE argv2[3]; rb_scan_args(argc, argv, "21", &vy, &vd, &vsg argv2[0] = vy; argv2[1] = vd; if (argc < 3) argv2[2] = INT2FIX(DEFAULT_SG else argv2[2] = vsg; if (NIL_P(valid_ordinal_sub(3, argv2, klass, 0))) return Qfalse; return Qtrue; }

xmlschema(string='-4712-01-01', start=Date::ITALY) → date Show source

根据一些典型的XML Schema格式,通过解析字符串来创建新的Date对象。

Date.xmlschema('2001-02-03') #=> #<Date: 2001-02-03 ...>

static VALUE date_s_xmlschema(int argc, VALUE *argv, VALUE klass) { VALUE str, sg; rb_scan_args(argc, argv, "02", &str, &sg switch (argc) { case 0: str = rb_str_new2("-4712-01-01" case 1: sg = INT2FIX(DEFAULT_SG } { VALUE hash = date_s__xmlschema(klass, str return d_new_by_frags(klass, hash, sg } }

公共实例方法

d + other → date Show source

返回指向other自身后的日期对象。另一个应该是一个数值。如果另一个是分数,则假定其精度最多为纳秒。

Date.new(2001,2,3) + 1 #=> #<Date: 2001-02-04 ...> DateTime.new(2001,2,3) + Rational(1,2) #=> #<DateTime: 2001-02-03T12:00:00+00:00 ...> DateTime.new(2001,2,3) + Rational(-1,2) #=> #<DateTime: 2001-02-02T12:00:00+00:00 ...> DateTime.jd(0,12) + DateTime.new(2001,2,3).ajd #=> #<DateTime: 2001-02-03T00:00:00+00:00 ...>

static VALUE d_lite_plus(VALUE self, VALUE other) { get_d1(self switch (TYPE(other)) { case T_FIXNUM: { VALUE nth; long t; int jd; nth = m_nth(dat t = FIX2LONG(other if (DIV(t, CM_PERIOD)) { nth = f_add(nth, INT2FIX(DIV(t, CM_PERIOD)) t = MOD(t, CM_PERIOD } if (!t) jd = m_jd(dat else { jd = m_jd(dat) + (int)t; canonicalize_jd(nth, jd } if (simple_dat_p(dat)) return d_simple_new_internal(rb_obj_class(self), nth, jd, dat->s.sg, 0, 0, 0, (dat->s.flags | HAVE_JD) & ~HAVE_CIVIL else return d_complex_new_internal(rb_obj_class(self), nth, jd, dat->c.df, dat->c.sf, dat->c.of, dat->c.sg, 0, 0, 0, #ifndef USE_PACK dat->c.hour, dat->c.min, dat->c.sec, #else EX_HOUR(dat->c.pc), EX_MIN(dat->c.pc), EX_SEC(dat->c.pc), #endif (dat->c.flags | HAVE_JD) & ~HAVE_CIVIL } break; case T_BIGNUM: { VALUE nth; int jd, s; if (f_positive_p(other)) s = +1; else { s = -1; other = f_negate(other } nth = f_idiv(other, INT2FIX(CM_PERIOD) jd = FIX2INT(f_mod(other, INT2FIX(CM_PERIOD)) if (s < 0) { nth = f_negate(nth jd = -jd; } if (!jd) jd = m_jd(dat else { jd = m_jd(dat) + jd; canonicalize_jd(nth, jd } if (f_zero_p(nth)) nth = m_nth(dat else nth = f_add(m_nth(dat), nth if (simple_dat_p(dat)) return d_simple_new_internal(rb_obj_class(self), nth, jd, dat->s.sg, 0, 0, 0, (dat->s.flags | HAVE_JD) & ~HAVE_CIVIL else return d_complex_new_internal(rb_obj_class(self), nth, jd, dat->c.df, dat->c.sf, dat->c.of, dat->c.sg, 0, 0, 0, #ifndef USE_PACK dat->c.hour, dat->c.min, dat->c.sec, #else EX_HOUR(dat->c.pc), EX_MIN(dat->c.pc), EX_SEC(dat->c.pc), #endif (dat->c.flags | HAVE_JD) & ~HAVE_CIVIL } break; case T_FLOAT: { double jd, o, tmp; int s, df; VALUE nth, sf; o = RFLOAT_VALUE(other if (o > 0) s = +1; else { s = -1; o = -o; } o = modf(o, &tmp if (!floor(tmp / CM_PERIOD)) { nth = INT2FIX(0 jd = (int)tmp; } else { double i, f; f = modf(tmp / CM_PERIOD, &i nth = f_floor(DBL2NUM(i) jd = (int)(f * CM_PERIOD } o *= DAY_IN_SECONDS; o = modf(o, &tmp df = (int)tmp; o *= SECOND_IN_NANOSECONDS; sf = INT2FIX((int)round(o) if (s < 0) { jd = -jd; df = -df; sf = f_negate(sf } if (f_zero_p(sf)) sf = m_sf(dat else { sf = f_add(m_sf(dat), sf if (f_lt_p(sf, INT2FIX(0))) { df -= 1; sf = f_add(sf, INT2FIX(SECOND_IN_NANOSECONDS) } else if (f_ge_p(sf, INT2FIX(SECOND_IN_NANOSECONDS))) { df += 1; sf = f_sub(sf, INT2FIX(SECOND_IN_NANOSECONDS) } } if (!df) df = m_df(dat else { df = m_df(dat) + df; if (df < 0) { jd -= 1; df += DAY_IN_SECONDS; } else if (df >= DAY_IN_SECONDS) { jd += 1; df -= DAY_IN_SECONDS; } } if (!jd) jd = m_jd(dat else { jd = m_jd(dat) + jd; canonicalize_jd(nth, jd } if (f_zero_p(nth)) nth = m_nth(dat else nth = f_add(m_nth(dat), nth if (!df && f_zero_p(sf) && !m_of(dat)) return d_simple_new_internal(rb_obj_class(self), nth, (int)jd, m_sg(dat), 0, 0, 0, (dat->s.flags | HAVE_JD) & ~(HAVE_CIVIL | HAVE_TIME | COMPLEX_DAT) else return d_complex_new_internal(rb_obj_class(self), nth, (int)jd, df, sf, m_of(dat), m_sg(dat), 0, 0, 0, 0, 0, 0, (dat->c.flags | HAVE_JD | HAVE_DF) & ~(HAVE_CIVIL | HAVE_TIME) } break; default: expect_numeric(other other = f_to_r(other #ifdef CANONICALIZATION_FOR_MATHN if (!k_rational_p(other)) return d_lite_plus(self, other #endif /* fall through */ case T_RATIONAL: { VALUE nth, sf, t; int jd, df, s; if (wholenum_p(other)) return d_lite_plus(self, rb_rational_num(other) if (f_positive_p(other)) s = +1; else { s = -1; other = f_negate(other } nth = f_idiv(other, INT2FIX(CM_PERIOD) t = f_mod(other, INT2FIX(CM_PERIOD) jd = FIX2INT(f_idiv(t, INT2FIX(1)) t = f_mod(t, INT2FIX(1) t = f_mul(t, INT2FIX(DAY_IN_SECONDS) df = FIX2INT(f_idiv(t, INT2FIX(1)) t = f_mod(t, INT2FIX(1) sf = f_mul(t, INT2FIX(SECOND_IN_NANOSECONDS) if (s < 0) { nth = f_negate(nth jd = -jd; df = -df; sf = f_negate(sf } if (f_zero_p(sf)) sf = m_sf(dat else { sf = f_add(m_sf(dat), sf if (f_lt_p(sf, INT2FIX(0))) { df -= 1; sf = f_add(sf, INT2FIX(SECOND_IN_NANOSECONDS) } else if (f_ge_p(sf, INT2FIX(SECOND_IN_NANOSECONDS))) { df += 1; sf = f_sub(sf, INT2FIX(SECOND_IN_NANOSECONDS) } } if (!df) df = m_df(dat else { df = m_df(dat) + df; if (df < 0) { jd -= 1; df += DAY_IN_SECONDS; } else if (df >= DAY_IN_SECONDS) { jd += 1; df -= DAY_IN_SECONDS; } } if (!jd) jd = m_jd(dat else { jd = m_jd(dat) + jd; canonicalize_jd(nth, jd } if (f_zero_p(nth)) nth = m_nth(dat else nth = f_add(m_nth(dat), nth if (!df && f_zero_p(sf) && !m_of(dat)) return d_simple_new_internal(rb_obj_class(self), nth, jd, m_sg(dat), 0, 0, 0, (dat->s.flags | HAVE_JD) & ~(HAVE_CIVIL | HAVE_TIME | COMPLEX_DAT) else return d_complex_new_internal(rb_obj_class(self), nth, jd, df, sf, m_of(dat), m_sg(dat), 0, 0, 0, 0, 0, 0, (dat->c.flags | HAVE_JD | HAVE_DF) & ~(HAVE_CIVIL | HAVE_TIME) } break; } }

d - other → date or rational Show source

如果另一个日期对象返回两个日期之间的差异。如果另一个是数字值,则返回指向other自身前几天的日期对象。如果另一个是分数,则假定其精度最多为纳秒。

Date.new(2001,2,3) - 1 #=> #<Date: 2001-02-02 ...> DateTime.new(2001,2,3) - Rational(1,2) #=> #<DateTime: 2001-02-02T12:00:00+00:00 ...> Date.new(2001,2,3) - Date.new(2001) #=> (33/1) DateTime.new(2001,2,3) - DateTime.new(2001,2,2,12) #=> (1/2)

static VALUE d_lite_minus(VALUE self, VALUE other) { if (k_date_p(other)) return minus_dd(self, other switch (TYPE(other)) { case T_FIXNUM: return d_lite_plus(self, LONG2NUM(-FIX2LONG(other)) case T_FLOAT: return d_lite_plus(self, DBL2NUM(-RFLOAT_VALUE(other)) default: expect_numeric(other /* fall through */ case T_BIGNUM: case T_RATIONAL: return d_lite_plus(self, f_negate(other) } }

d << n → date Show source

返回指向n自身之前几个月的日期对象。参数n应该是一个数值。

Date.new(2001,2,3) << 1 #=> #<Date: 2001-01-03 ...> Date.new(2001,2,3) << -2 #=> #<Date: 2001-04-03 ...>

当相应的月份不存在同一天时,则使用该月份的最后一天:

Date.new(2001,3,28) << 1 #=> #<Date: 2001-02-28 ...> Date.new(2001,3,31) << 1 #=> #<Date: 2001-02-28 ...>

这也会导致以下可能意外的行为:

Date.new(2001,3,31) << 2 #=> #<Date: 2001-01-31 ...> Date.new(2001,3,31) << 1 << 1 #=> #<Date: 2001-01-28 ...> Date.new(2001,3,31) << 1 << -1 #=> #<Date: 2001-03-28 ...>

static VALUE d_lite_lshift(VALUE self, VALUE other) { expect_numeric(other return d_lite_rshift(self, f_negate(other) }

d <=> other → -1, 0, +1 or nil Show source

比较两个日期并返回-1,0,1或nil。另一个应该是日期对象或数值作为天文学的Julian天数。

Date.new(2001,2,3) <=> Date.new(2001,2,4) #=> -1 Date.new(2001,2,3) <=> Date.new(2001,2,3) #=> 0 Date.new(2001,2,3) <=> Date.new(2001,2,2) #=> 1 Date.new(2001,2,3) <=> Object.new #=> nil Date.new(2001,2,3) <=> Rational(4903887,2) #=> 0

另见Comparable。

static VALUE d_lite_cmp(VALUE self, VALUE other) { if (!k_date_p(other)) return cmp_gen(self, other { get_d2(self, other if (!(simple_dat_p(adat) && simple_dat_p(bdat) && m_gregorian_p(adat) == m_gregorian_p(bdat))) return cmp_dd(self, other { VALUE a_nth, b_nth; int a_jd, b_jd; m_canonicalize_jd(self, adat m_canonicalize_jd(other, bdat a_nth = m_nth(adat b_nth = m_nth(bdat if (f_eqeq_p(a_nth, b_nth)) { a_jd = m_jd(adat b_jd = m_jd(bdat if (a_jd == b_jd) { return INT2FIX(0 } else if (a_jd < b_jd) { return INT2FIX(-1 } else { return INT2FIX(1 } } else if (f_lt_p(a_nth, b_nth)) { return INT2FIX(-1 } else { return INT2FIX(1 } } } }

d === other → bool Show source

如果他们是同一天,则返回true。

Date.new(2001,2,3) === Date.new(2001,2,3) #=> true Date.new(2001,2,3) === Date.new(2001,2,4) #=> false DateTime.new(2001,2,3) === DateTime.new(2001,2,3,12) #=> true DateTime.new(2001,2,3) === DateTime.new(2001,2,3,0,0,0,'+24:00') #=> true DateTime.new(2001,2,3) === DateTime.new(2001,2,4,0,0,0,'+24:00') #=> false

static VALUE d_lite_equal(VALUE self, VALUE other) { if (!k_date_p(other)) return equal_gen(self, other { get_d2(self, other if (!(m_gregorian_p(adat) == m_gregorian_p(bdat))) return equal_gen(self, other { VALUE a_nth, b_nth; int a_jd, b_jd; m_canonicalize_jd(self, adat m_canonicalize_jd(other, bdat a_nth = m_nth(adat b_nth = m_nth(bdat a_jd = m_local_jd(adat b_jd = m_local_jd(bdat if (f_eqeq_p(a_nth, b_nth) && a_jd == b_jd) return Qtrue; return Qfalse; } } }

d >> n → date Show source

返回指向自身后n个月的日期对象。 参数n应该是一个数值。

Date.new(2001,2,3) >> 1 #=> #<Date: 2001-03-03 ...> Date.new(2001,2,3) >> -2 #=> #<Date: 2000-12-03 ...>

当相应的月份不存在同一天时,则使用该月份的最后一天:

Date.new(2001,1,28) >> 1 #=> #<Date: 2001-02-28 ...> Date.new(2001,1,31) >> 1 #=> #<Date: 2001-02-28 ...>

这也会导致以下可能意外的行为:

Date.new(2001,1,31) >> 2 #=> #<Date: 2001-03-31 ...> Date.new(2001,1,31) >> 1 >> 1 #=> #<Date: 2001-03-28 ...> Date.new(2001,1,31) >> 1 >> -1 #=> #<Date: 2001-01-28 ...>

static VALUE d_lite_rshift(VALUE self, VALUE other) { VALUE t, y, nth, rjd2; int m, d, rjd; double sg; get_d1(self t = f_add3(f_mul(m_real_year(dat), INT2FIX(12)), INT2FIX(m_mon(dat) - 1), other if (FIXNUM_P(t)) { long it = FIX2LONG(t y = LONG2NUM(DIV(it, 12) it = MOD(it, 12 m = (int)it + 1; } else { y = f_idiv(t, INT2FIX(12) t = f_mod(t, INT2FIX(12) m = FIX2INT(t) + 1; } d = m_mday(dat sg = m_sg(dat while (1) { int ry, rm, rd, ns; if (valid_civil_p(y, m, d, sg, &nth, &ry, &rm, &rd, &rjd, &ns)) break; if (--d < 1) rb_raise(rb_eArgError, "invalid date" } encode_jd(nth, rjd, &rjd2 return d_lite_plus(self, f_sub(rjd2, m_real_local_jd(dat)) }

ajd → rational Show source

返回天文朱利安天数。这是一个小数,它不会被偏移量调整。

DateTime.new(2001,2,3,4,5,6,'+7').ajd #=> (11769328217/4800) DateTime.new(2001,2,2,14,5,6,'-7').ajd #=> (11769328217/4800)

static VALUE d_lite_ajd(VALUE self) { get_d1(self return m_ajd(dat }

amjd → rational Show source

返回天文修正的朱利安天数。这是一个小数,它不会被偏移量调整。

DateTime.new(2001,2,3,4,5,6,'+7').amjd #=> (249325817/4800) DateTime.new(2001,2,2,14,5,6,'-7').amjd #=> (249325817/4800)

static VALUE d_lite_amjd(VALUE self) { get_d1(self return m_amjd(dat }

as_json(*) Show source

返回一个散列,它将变成一个JSON对象并表示这个对象。

# File ext/json/lib/json/add/date.rb, line 19 def as_json(*) { JSON.create_id => self.class.name, 'y' => year, 'm' => month, 'd' => day, 'sg' => start, } end

asctime → string Show source

以asctime(3)格式返回一个字符串(但末尾没有“n0”)。此方法等效于strftime('%c')。

另请参阅asctime(3)或ctime(3)。

static VALUE d_lite_asctime(VALUE self) { return strftimev("%a %b %e %H:%M:%S %Y", self, set_tmx }

ctime → string Show source

以asctime(3)格式返回一个字符串(但末尾没有“n0”)。此方法等效于strftime('%c')。

另请参阅asctime(3)或ctime(3)。

static VALUE d_lite_asctime(VALUE self) { return strftimev("%a %b %e %H:%M:%S %Y", self, set_tmx }

cwday → fixnum Show source

返回日历星期几(1-7,星期一为1)。

Date.new(2001,2,3).cwday #=> 6

static VALUE d_lite_cwday(VALUE self) { get_d1(self return INT2FIX(m_cwday(dat) }

cweek → fixnum Show source

返回日历星期编号(1-53)。

Date.new(2001,2,3).cweek #=> 5

static VALUE d_lite_cweek(VALUE self) { get_d1(self return INT2FIX(m_cweek(dat) }

cwyear → integer Show source

返回基于日历的年份。

Date.new(2001,2,3).cwyear #=> 2001 Date.new(2000,1,1).cwyear #=> 1999

static VALUE d_lite_cwyear(VALUE self) { get_d1(self return m_real_cwyear(dat }

day → fixnum Show source

返回月份的日期(1-31)。

Date.new(2001,2,3).mday #=> 3

static VALUE d_lite_mday(VALUE self) { get_d1(self return INT2FIX(m_mday(dat) }

day_fraction → rational Show source

返回当天的小数部分。

DateTime.new(2001,2,3,12).day_fraction #=> (1/2)

static VALUE d_lite_day_fraction(VALUE self) { get_d1(self if (simple_dat_p(dat)) return INT2FIX(0 return m_fr(dat }

downto(min) → enumerator Show source

downto(min){|date| ...} → self

此方法相当于step(min,-1){| date | ...}。

static VALUE d_lite_downto(VALUE self, VALUE min) { VALUE date; RETURN_ENUMERATOR(self, 1, &min date = self; while (FIX2INT(d_lite_cmp(date, min)) >= 0) { rb_yield(date date = d_lite_plus(date, INT2FIX(-1) } return self; }

england → date Show source

此方法等同于#new_start(Date :: ENGLAND)。

static VALUE d_lite_england(VALUE self) { return dup_obj_with_new_start(self, ENGLAND }

friday? → bool Show source

如果日期是星期五,则返回true。

static VALUE d_lite_friday_p(VALUE self) { get_d1(self return f_boolcast(m_wday(dat) == 5 }

gregorian → date Show source

这个方法相当于#new_start(Date :: GREGORIAN)。

static VALUE d_lite_gregorian(VALUE self) { return dup_obj_with_new_start(self, GREGORIAN }

gregorian? → bool Show source

如果日历在日历改革日期之后或之后返回true。

Date.new(1582,10,15).gregorian? #=> true (Date.new(1582,10,15) - 1).gregorian? #=> false

static VALUE d_lite_gregorian_p(VALUE self) { get_d1(self return f_boolcast(m_gregorian_p(dat) }

httpdate → string Show source

此方法相当于strftime('%a,%d%b%Y%T GMT')。另请参阅RFC 2616。

static VALUE d_lite_httpdate(VALUE self) { volatile VALUE dup = dup_obj_with_new_offset(self, 0 return strftimev("%a, %d %b %Y %T GMT", dup, set_tmx }

inspect → string Show source

将该值作为字符串返回以进行检查。

Date.new(2001,2,3).inspect #=> "#<Date: 2001-02-03 ((2451944j,0s,0n),+0s,2299161j)>" DateTime.new(2001,2,3,4,5,6,'-7').inspect #=> "#<DateTime: 2001-02-03T04:05:06-07:00 ((2451944j,39906s,0n),-25200s,2299161j)>"

static VALUE d_lite_inspect(VALUE self) { get_d1(self return mk_inspect(dat, rb_obj_class(self), self }

iso8601 → string Show source

xmlschema → string

这种方法等同于strftime('%F')。

static VALUE d_lite_iso8601(VALUE self) { return strftimev("%Y-%m-%d", self, set_tmx }

italy → date Show source

此方法相当于#new_start(Date :: ITALY)。

static VALUE d_lite_italy(VALUE self) { return dup_obj_with_new_start(self, ITALY }

jd → integer Show source

返回Julian天数。这是一个整数,由当地时间的偏移量来调整。

DateTime.new(2001,2,3,4,5,6,'+7').jd #=> 2451944 DateTime.new(2001,2,3,4,5,6,'-7').jd #=> 2451944

static VALUE d_lite_jd(VALUE self) { get_d1(self return m_real_local_jd(dat }

jisx0301 → string Show source

返回JIS X 0301格式的字符串。

Date.new(2001,2,3).jisx0301 #=> "H13.02.03"

static VALUE d_lite_jisx0301(VALUE self) { char fmtbuf[JISX0301_DATE_SIZE]; const char *fmt; get_d1(self fmt = jisx0301_date_format(fmtbuf, sizeof(fmtbuf), m_real_local_jd(dat), m_real_year(dat) return strftimev(fmt, self, set_tmx }

julian → date Show source

此方法等同于#new_start(Date :: JULIAN)。

static VALUE d_lite_julian(VALUE self) { return dup_obj_with_new_start(self, JULIAN }

julian? → bool Show source

如果日期在日历改革日期之前返回true。

Date.new(1582,10,15).julian? #=> false (Date.new(1582,10,15) - 1).julian? #=> true

static VALUE d_lite_julian_p(VALUE self) { get_d1(self return f_boolcast(m_julian_p(dat) }

ld → integer Show source

返回Lilian天数。这是一个整数,由当地时间的偏移量来调整。

Date.new(2001,2,3).ld #=> 152784

static VALUE d_lite_ld(VALUE self) { get_d1(self return f_sub(m_real_local_jd(dat), INT2FIX(2299160) }

leap? → bool Show source

如果年份是闰年,则返回true。

Date.new(2000).leap? #=> true Date.new(2001).leap? #=> false

static VALUE d_lite_leap_p(VALUE self) { int rjd, ns, ry, rm, rd; get_d1(self if (m_gregorian_p(dat)) return f_boolcast(c_gregorian_leap_p(m_year(dat)) c_civil_to_jd(m_year(dat), 3, 1, m_virtual_sg(dat), &rjd, &ns c_jd_to_civil(rjd - 1, m_virtual_sg(dat), &ry, &rm, &rd return f_boolcast(rd == 29 }

mday → fixnum Show source

返回月份的日期(1-31)。

Date.new(2001,2,3).mday #=> 3

static VALUE d_lite_mday(VALUE self) { get_d1(self return INT2FIX(m_mday(dat) }

mjd → integer Show source

返回修改的Julian天数。这是一个整数,由当地时间的偏移量来调整。

DateTime.new(2001,2,3,4,5,6,'+7').mjd #=> 51943 DateTime.new(2001,2,3,4,5,6,'-7').mjd #=> 51943

static VALUE d_lite_mjd(VALUE self) { get_d1(self return f_sub(m_real_local_jd(dat), INT2FIX(2400001) }

mon → fixnum Show source

month → fixnum

返回月份(1-12)。

Date.new(2001,2,3).mon #=> 2

static VALUE d_lite_mon(VALUE self) { get_d1(self return INT2FIX(m_mon(dat) }

monday? → bool Show source

如果日期是星期一,则返回true。

static VALUE d_lite_monday_p(VALUE self) { get_d1(self return f_boolcast(m_wday(dat) == 1 }

month → fixnum Show source

返回月份(1-12)。

Date.new(2001,2,3).mon #=> 2

static VALUE d_lite_mon(VALUE self) { get_d1(self return INT2FIX(m_mon(dat) }

new_start(start=Date::ITALY) → date Show source

复制自身并重新设定日历改革日。

d = Date.new(1582,10,15) d.new_start(Date::JULIAN) #=> #<Date: 1582-10-05 ...>

static VALUE d_lite_new_start(int argc, VALUE *argv, VALUE self) { VALUE vsg; double sg; rb_scan_args(argc, argv, "01", &vsg sg = DEFAULT_SG; if (argc >= 1) val2sg(vsg, sg return dup_obj_with_new_start(self, sg }

next → date Show source

返回表示下一天的日期对象。

static VALUE d_lite_next(VALUE self) { return d_lite_next_day(0, (VALUE *)NULL, self }

next_day(n=1) → date Show source

这个方法相当于d + n。

static VALUE d_lite_next_day(int argc, VALUE *argv, VALUE self) { VALUE n; rb_scan_args(argc, argv, "01", &n if (argc < 1) n = INT2FIX(1 return d_lite_plus(self, n }

next_month(n=1) → date Show source

这种方法相当于d >> n。

See #>> for examples.

static VALUE d_lite_next_month(int argc, VALUE *argv, VALUE self) { VALUE n; rb_scan_args(argc, argv, "01", &n if (argc < 1) n = INT2FIX(1 return d_lite_rshift(self, n }

next_year(n=1) → date Show source

这个方法相当于d >>(n * 12)。

Date.new(2001,2,3).next_year #=> #<Date: 2002-02-03 ...> Date.new(2008,2,29).next_year #=> #<Date: 2009-02-28 ...> Date.new(2008,2,29).next_year(4) #=> #<Date: 2012-02-29 ...>

另见#>>。

static VALUE d_lite_next_year(int argc, VALUE *argv, VALUE self) { VALUE n; rb_scan_args(argc, argv, "01", &n if (argc < 1) n = INT2FIX(1 return d_lite_rshift(self, f_mul(n, INT2FIX(12)) }

prev_day(n=1) → date Show source

这个方法相当于d - n。

static VALUE d_lite_prev_day(int argc, VALUE *argv, VALUE self) { VALUE n; rb_scan_args(argc, argv, "01", &n if (argc < 1) n = INT2FIX(1 return d_lite_minus(self, n }

prev_month(n=1) → date Show source

这个方法相当于d << n。

请参阅#“作为示例。

static VALUE d_lite_prev_month(int argc, VALUE *argv, VALUE self) { VALUE n; rb_scan_args(argc, argv, "01", &n if (argc < 1) n = INT2FIX(1 return d_lite_lshift(self, n }

prev_year(n=1) → date Show source

这个方法相当于d <<(n * 12)。

Date.new(2001,2,3).prev_year #=> #<Date: 2000-02-03 ...> Date.new(2008,2,29).prev_year #=> #<Date: 2007-02-28 ...> Date.new(2008,2,29).prev_year(4) #=> #<Date: 2004-02-29 ...>

另见#<<。

static VALUE d_lite_prev_year(int argc, VALUE *argv, VALUE self) { VALUE n; rb_scan_args(argc, argv, "01", &n if (argc < 1) n = INT2FIX(1 return d_lite_lshift(self, f_mul(n, INT2FIX(12)) }

rfc2822 → string Show source

rfc822→字符串

此方法等效于strftime('%a,%-d%b%Y%T%z')。

static VALUE d_lite_rfc2822(VALUE self) { return strftimev("%a, %-d %b %Y %T %z", self, set_tmx }

rfc3339 → string Show source

此方法等同于strftime('%FT%T%:z')。

static VALUE d_lite_rfc3339(VALUE self) { return strftimev("%Y-%m-%dT%H:%M:%S%:z", self, set_tmx }

rfc2822 → string Show source

rfc822→字符串

此方法等效于strftime('%a,%-d%b%Y%T%z')。

static VALUE d_lite_rfc2822(VALUE self) { return strftimev("%a, %-d %b %Y %T %z", self, set_tmx }

saturday? → bool Show source

如果日期是星期六,则返回true。

static VALUE d_lite_saturday_p(VALUE self) { get_d1(self return f_boolcast(m_wday(dat) == 6 }

start → float Show source

返回表示日历改革日的Julian日数。

Date.new(2001,2,3).start #=> 2299161.0 Date.new(2001,2,3,Date::GREGORIAN).start #=> -Infinity

static VALUE d_lite_start(VALUE self) { get_d1(self return DBL2NUM(m_sg(dat) }

step(limit, step=1) → enumerator Show source

step(limit, step=1){|date| ...} → self

迭代对给定块进行评估,该块使用日期对象。限制应该是日期对象。

Date.new(2001).step(Date.new(2001,-1,-1)).select{|d| d.sunday?}.size #=> 52

static VALUE d_lite_step(int argc, VALUE *argv, VALUE self) { VALUE limit, step, date; rb_scan_args(argc, argv, "11", &limit, &step if (argc < 2) step = INT2FIX(1 #if 0 if (f_zero_p(step)) rb_raise(rb_eArgError, "step can't be 0" #endif RETURN_ENUMERATOR(self, argc, argv date = self; switch (FIX2INT(f_cmp(step, INT2FIX(0)))) { case -1: while (FIX2INT(d_lite_cmp(date, limit)) >= 0) { rb_yield(date date = d_lite_plus(date, step } break; case 0: while (1) rb_yield(date break; case 1: while (FIX2INT(d_lite_cmp(date, limit)) <= 0) { rb_yield(date date = d_lite_plus(date, step } break; default: abort( } return self; }

strftime(format='%F') → string Show source

根据给定格式字符串中的指令格式化日期。指令以百分号(%)字符开头。任何未作为指令列出的文本都将传递到输出字符串。

该指令由一个百分号(%)字符,零个或多个标志,可选的最小字段宽度,可选的修饰符和转换说明符组成,如下所示。

%<flags><width><modifier><conversion>

标志:

- don't pad a numerical output. _ use spaces for padding. 0 use zeros for padding. ^ upcase the result string. # change case.

最小字段宽度指定最小宽度。

修饰符是“E”,“O”,“:”,“::”和“:::”。“E”和“O”被忽略。目前没有效果。

格式指南:

Date (Year, Month, Day): %Y - Year with century (can be negative, 4 digits at least) -0001, 0000, 1995, 2009, 14292, etc. %C - year / 100 (round down. 20 in 2009) %y - year % 100 (00..99) %m - Month of the year, zero-padded (01..12) %_m blank-padded ( 1..12) %-m no-padded (1..12) %B - The full month name (``January'') %^B uppercased (``JANUARY'') %b - The abbreviated month name (``Jan'') %^b uppercased (``JAN'') %h - Equivalent to %b %d - Day of the month, zero-padded (01..31) %-d no-padded (1..31) %e - Day of the month, blank-padded ( 1..31) %j - Day of the year (001..366) Time (Hour, Minute, Second, Subsecond): %H - Hour of the day, 24-hour clock, zero-padded (00..23) %k - Hour of the day, 24-hour clock, blank-padded ( 0..23) %I - Hour of the day, 12-hour clock, zero-padded (01..12) %l - Hour of the day, 12-hour clock, blank-padded ( 1..12) %P - Meridian indicator, lowercase (``am'' or ``pm'') %p - Meridian indicator, uppercase (``AM'' or ``PM'') %M - Minute of the hour (00..59) %S - Second of the minute (00..59) %L - Millisecond of the second (000..999) %N - Fractional seconds digits, default is 9 digits (nanosecond) %3N millisecond (3 digits) %15N femtosecond (15 digits) %6N microsecond (6 digits) %18N attosecond (18 digits) %9N nanosecond (9 digits) %21N zeptosecond (21 digits) %12N picosecond (12 digits) %24N yoctosecond (24 digits) Time zone: %z - Time zone as hour and minute offset from UTC (e.g. +0900) %:z - hour and minute offset from UTC with a colon (e.g. +09:00) %::z - hour, minute and second offset from UTC (e.g. +09:00:00) %:::z - hour, minute and second offset from UTC (e.g. +09, +09:30, +09:30:30) %Z - Time zone abbreviation name or something similar information. Weekday: %A - The full weekday name (``Sunday'') %^A uppercased (``SUNDAY'') %a - The abbreviated name (``Sun'') %^a uppercased (``SUN'') %u - Day of the week (Monday is 1, 1..7) %w - Day of the week (Sunday is 0, 0..6) ISO 8601 week-based year and week number: The week 1 of YYYY starts with a Monday and includes YYYY-01-04. The days in the year before the first week are in the last week of the previous year. %G - The week-based year %g - The last 2 digits of the week-based year (00..99) %V - Week number of the week-based year (01..53) Week number: The week 1 of YYYY starts with a Sunday or Monday (according to %U or %W). The days in the year before the first week are in week 0. %U - Week number of the year. The week starts with Sunday. (00..53) %W - Week number of the year. The week starts with Monday. (00..53) Seconds since the Unix Epoch: %s - Number of seconds since 1970-01-01 00:00:00 UTC. %Q - Number of milliseconds since 1970-01-01 00:00:00 UTC. Literal string: %n - Newline character (\n) %t - Tab character (\t) %% - Literal ``%'' character Combination: %c - date and time (%a %b %e %T %Y) %D - Date (%m/%d/%y) %F - The ISO 8601 date format (%Y-%m-%d) %v - VMS date (%e-%b-%Y) %x - Same as %D %X - Same as %T %r - 12-hour time (%I:%M:%S %p) %R - 24-hour time (%H:%M) %T - 24-hour time (%H:%M:%S) %+ - date(1) (%a %b %e %H:%M:%S %Z %Y)

此方法与ISO C和POSIX中定义的strftime()函数类似。几个指令(%a,%A,%b,%B,%c,%p,%r,%x,%X,%E *,%O *和%Z)在语言环境中依赖于函数。但是,这种方法是独立于区域的。因此,即使在其他系统(如C)中使用相同的格式字符串,结果也可能不同。因为存在相应的与区域无关的表示形式%D和%T,所以避免使用%x和%X是一种好的做法。

例子:

d = DateTime.new(2007,11,19,8,37,48,"-06:00") #=> #<DateTime: 2007-11-19T08:37:48-0600 ...> d.strftime("Printed on %m/%d/%Y") #=> "Printed on 11/19/2007" d.strftime("at %I:%M%p") #=> "at 08:37AM"

各种ISO 8601格式:

%Y%m%d => 20071119 Calendar date (basic) %F => 2007-11-19 Calendar date (extended) %Y-%m => 2007-11 Calendar date, reduced accuracy, specific month %Y => 2007 Calendar date, reduced accuracy, specific year %C => 20 Calendar date, reduced accuracy, specific century %Y%j => 2007323 Ordinal date (basic) %Y-%j => 2007-323 Ordinal date (extended) %GW%V%u => 2007W471 Week date (basic) %G-W%V-%u => 2007-W47-1 Week date (extended) %GW%V => 2007W47 Week date, reduced accuracy, specific week (basic) %G-W%V => 2007-W47 Week date, reduced accuracy, specific week (extended) %H%M%S => 083748 Local time (basic) %T => 08:37:48 Local time (extended) %H%M => 0837 Local time, reduced accuracy, specific minute (basic) %H:%M => 08:37 Local time, reduced accuracy, specific minute (extended) %H => 08 Local time, reduced accuracy, specific hour %H%M%S,%L => 083748,000 Local time with decimal fraction, comma as decimal sign (basic) %T,%L => 08:37:48,000 Local time with decimal fraction, comma as decimal sign (extended) %H%M%S.%L => 083748.000 Local time with decimal fraction, full stop as decimal sign (basic) %T.%L => 08:37:48.000 Local time with decimal fraction, full stop as decimal sign (extended) %H%M%S%z => 083748-0600 Local time and the difference from UTC (basic) %T%:z => 08:37:48-06:00 Local time and the difference from UTC (extended) %Y%m%dT%H%M%S%z => 20071119T083748-0600 Date and time of day for calendar date (basic) %FT%T%:z => 2007-11-19T08:37:48-06:00 Date and time of day for calendar date (extended) %Y%jT%H%M%S%z => 2007323T083748-0600 Date and time of day for ordinal date (basic) %Y-%jT%T%:z => 2007-323T08:37:48-06:00 Date and time of day for ordinal date (extended) %GW%V%uT%H%M%S%z => 2007W471T083748-0600 Date and time of day for week date (basic) %G-W%V-%uT%T%:z => 2007-W47-1T08:37:48-06:00 Date and time of day for week date (extended) %Y%m%dT%H%M => 20071119T0837 Calendar date and local time (basic) %FT%R => 2007-11-19T08:37 Calendar date and local time (extended) %Y%jT%H%MZ => 2007323T0837Z Ordinal date and UTC of day (basic) %Y-%jT%RZ => 2007-323T08:37Z Ordinal date and UTC of day (extended) %GW%V%uT%H%M%z => 2007W471T0837-0600 Week date and local time and difference from UTC (basic) %G-W%V-%uT%R%:z => 2007-W47-1T08:37-06:00 Week date and local time and difference from UTC (extended)

另请参阅strftime(3)和:: strptime。

static VALUE d_lite_strftime(int argc, VALUE *argv, VALUE self) { return date_strftime_internal(argc, argv, self, "%Y-%m-%d", set_tmx }

succ → date Show source

返回表示下一天的日期对象。

static VALUE d_lite_next(VALUE self) { return d_lite_next_day(0, (VALUE *)NULL, self }

sunday? → bool Show source

如果日期是星期日,则返回true。

static VALUE d_lite_sunday_p(VALUE self) { get_d1(self return f_boolcast(m_wday(dat) == 0 }

thursday? → bool Show source

如果日期是星期四,则返回true。

static VALUE d_lite_thursday_p(VALUE self) { get_d1(self return f_boolcast(m_wday(dat) == 4 }

to_date → self Show source

返回自身。

static VALUE date_to_date(VALUE self) { return self; }

to_datetime → datetime Show source

返回表示自我的DateTime对象。

static VALUE date_to_datetime(VALUE self) { get_d1a(self if (simple_dat_p(adat)) { VALUE new = d_lite_s_alloc_simple(cDateTime { get_d1b(new bdat->s = adat->s; return new; } } else { VALUE new = d_lite_s_alloc_complex(cDateTime { get_d1b(new bdat->c = adat->c; bdat->c.df = 0; RB_OBJ_WRITE(new, &bdat->c.sf, INT2FIX(0) #ifndef USE_PACK bdat->c.hour = 0; bdat->c.min = 0; bdat->c.sec = 0; #else bdat->c.pc = PACK5(EX_MON(adat->c.pc), EX_MDAY(adat->c.pc), 0, 0, 0 bdat->c.flags |= HAVE_DF | HAVE_TIME; #endif return new; } } }

to_json(*args) Show source

将Julian年,y月,d日和日历改革日的类名称(日期)存储为JSON字符串

# File ext/json/lib/json/add/date.rb, line 31 def to_json(*args) as_json.to_json(*args) end

to_s → string Show source

返回ISO 8601格式的字符串。(此方法不使用扩展表示。)

Date.new(2001,2,3).to_s #=> "2001-02-03"

static VALUE d_lite_to_s(VALUE self) { return strftimev("%Y-%m-%d", self, set_tmx }

to_time → time Show source

返回表示自身的Time对象。

static VALUE date_to_time(VALUE self) { get_d1(self return f_local3(rb_cTime, m_real_year(dat), INT2FIX(m_mon(dat)), INT2FIX(m_mday(dat)) }

tuesday? → bool Show source

如果日期是星期二,则返回true。

static VALUE d_lite_tuesday_p(VALUE self) { get_d1(self return f_boolcast(m_wday(dat) == 2 }

upto(max) → enumerator Show source

upto(max){|date| ...} → self

此方法等同于step(max,1){| date | ...}。

static VALUE d_lite_upto(VALUE self, VALUE max) { VALUE date; RETURN_ENUMERATOR(self, 1, &max date = self; while (FIX2INT(d_lite_cmp(date, max)) <= 0) { rb_yield(date date = d_lite_plus(date, INT2FIX(1) } return self; }

wday → fixnum Show source

返回星期几(0-6,星期日为零)。

Date.new(2001,2,3).wday #=> 6

static VALUE d_lite_wday(VALUE self) { get_d1(self return INT2FIX(m_wday(dat) }

wednesday? → bool Show source

如果日期是星期三,则返回true。

static VALUE d_lite_wednesday_p(VALUE self) { get_d1(self return f_boolcast(m_wday(dat) == 3 }

iso8601 → string Show source

xmlschema → string

这种方法等同于strftime('%F')。

static VALUE d_lite_iso8601(VALUE self) { return strftimev("%Y-%m-%d", self, set_tmx }

yday → fixnum Show source

返回一年中的某一天(1-366)。

Date.new(2001,2,3).yday #=> 34

static VALUE d_lite_yday(VALUE self) { get_d1(self return INT2FIX(m_yday(dat) }

year → integer Show source

返回年份。

Date.new(2001,2,3).year #=> 2001 (Date.new(1,1,1) - 1).year #=> 0

static VALUE d_lite_year(VALUE self) { get_d1(self return m_real_year(dat }

私有实例方法

hour → fixnum Show source

返回小时(0-23)。

DateTime.new(2001,2,3,4,5,6).hour #=> 4

static VALUE d_lite_hour(VALUE self) { get_d1(self return INT2FIX(m_hour(dat) }

min → fixnum Show source

minute → fixnum

返回分钟(0-59)。

DateTime.new(2001,2,3,4,5,6).min #=> 5

static VALUE d_lite_min(VALUE self) { get_d1(self return INT2FIX(m_min(dat) }

minute → fixnum Show source

返回分钟(0-59)。

DateTime.new(2001,2,3,4,5,6).min #=> 5

static VALUE d_lite_min(VALUE self) { get_d1(self return INT2FIX(m_min(dat) }

new_offset(offset=0) → date Show source

复制自身并重置其偏移量。

d = DateTime.new(2001,2,3,4,5,6,'-02:00') #=> #<DateTime: 2001-02-03T04:05:06-02:00 ...> d.new_offset('+09:00') #=> #<DateTime: 2001-02-03T15:05:06+09:00 ...>

static VALUE d_lite_new_offset(int argc, VALUE *argv, VALUE self) { VALUE vof; int rof; rb_scan_args(argc, argv, "01", &vof rof = 0; if (argc >= 1) val2off(vof, rof return dup_obj_with_new_offset(self, rof }

offset → rational Show source

返回偏移量。

DateTime.parse('04pm+0730').offset #=> (5/16)

static VALUE d_lite_offset(VALUE self) { get_d1(self return m_of_in_day(dat }

sec → fixnum Show source

second → fixnum

返回第二个(0-59)。

DateTime.new(2001,2,3,4,5,6).sec #=> 6

static VALUE d_lite_sec(VALUE self) { get_d1(self return INT2FIX(m_sec(dat) }

sec_fraction → rational Show source

返回秒的小数部分。

DateTime.new(2001,2,3,4,5,6.5).sec_fraction #=> (1/2)

static VALUE d_lite_sec_fraction(VALUE self) { get_d1(self return m_sf_in_sec(dat }

second → fixnum Show source

返回第二个(0-59)。

DateTime.new(2001,2,3,4,5,6).sec #=> 6

static VALUE d_lite_sec(VALUE self) { get_d1(self return INT2FIX(m_sec(dat) }

second_fraction → rational Show source

返回秒的小数部分。

DateTime.new(2001,2,3,4,5,6.5).sec_fraction #=> (1/2)

static VALUE d_lite_sec_fraction(VALUE self) { get_d1(self return m_sf_in_sec(dat }

zone → string Show source

返回时区。

DateTime.parse('04pm+0730').zone #=> "+07:30"

static VALUE d_lite_zone(VALUE self) { get_d1(self return m_zone(dat }