Class: Date

Inherits:
Object
  • Object
show all
Defined in:
lib/date/format.rb,
ext/date_ext/date_ext.c

Direct Known Subclasses

DateTime

Defined Under Namespace

Modules: Format

Constant Summary collapse

ITALY =

The julian day number for the day of calendar reform in Italy

LONG2NUM(RHR_JD_ITALY)
ENGLAND =

The julian day number for the day of calendar reform in England

LONG2NUM(RHR_JD_ENGLAND)
GREGORIAN =

An integer lower than the lowest supported julian day number

rhrd_start_num
JULIAN =

An integer higher than the highest supported julian day number

LONG2NUM(RHR_JD_MAX + 1)
ZONES =

A hash mapping lowercase time zone names to offsets in seconds<br />ZONES => -28800

rhrd_zones_hash
MONTHNAMES =

An array of month names<br />MONTHNAMES => ‘January’

rhrd_monthnames
ABBR_MONTHNAMES =

An array of abbreviated month names<br />ABBR_MONTHNAMES => ‘Jan’

rhrd_abbr_monthnames
DAYNAMES =

An array of day names<br />DAYNAMES => ‘Sunday’

rhrd_daynames
ABBR_DAYNAMES =

An array of abbreviated day names<br />ABBR_DAYNAMES => ‘Sun’

rhrd_abbr_daynames

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._httpdate(str) ⇒ Object

call-seq:

[ruby 1-9 only] <br />
_httpdate(str) -> Hash or nil

Attempt to parse string using the 3 HTTP formats specified in RFC 2616. Returns a Hash of values if the string was parsed, and nil if not.



796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
# File 'lib/date/format.rb', line 796

def self._httpdate(str)
  if /\A\s*(#{Format::ABBR_DAYS.keys.join('|')})\s*,\s+
      \d{2}\s+
      (#{Format::ABBR_MONTHS.keys.join('|')})\s+
      -?\d{4}\s+ # allow minus, anyway
      \d{2}:\d{2}:\d{2}\s+
      gmt\s*\z/iox =~ str
    _rfc2822(str)
  elsif /\A\s*(#{Format::DAYS.keys.join('|')})\s*,\s+
      \d{2}\s*-\s*
      (#{Format::ABBR_MONTHS.keys.join('|')})\s*-\s*
      \d{2}\s+
      \d{2}:\d{2}:\d{2}\s+
      gmt\s*\z/iox =~ str
    _parse(str)
  elsif /\A\s*(#{Format::ABBR_DAYS.keys.join('|')})\s+
      (#{Format::ABBR_MONTHS.keys.join('|')})\s+
      \d{1,2}\s+
      \d{2}:\d{2}:\d{2}\s+
      \d{4}\s*\z/iox =~ str
    _parse(str)
  end
end

._iso8601(str) ⇒ Object

call-seq:

[ruby 1-9 only] <br />
_iso8601(str) -> Hash or nil

Attempt to parse string using a wide variety of ISO 8601 based formats, including the civil, commercial, and ordinal formats. Returns a Hash of values if the string was parsed, and nil if not.



663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
# File 'lib/date/format.rb', line 663

def self._iso8601(str)
  if /\A\s*(([-+]?\d{2,}|-)-\d{2}-\d{2}|
            ([-+]?\d{2,})?-\d{3}|
            (\d{2}|\d{4})?-w\d{2}-\d|
            -w-\d)
      (t
      \d{2}:\d{2}(:\d{2}([,.]\d+)?)?
      (z|[-+]\d{2}(:?\d{2})?)?)?\s*\z/ix =~ str
    _parse(str)
  elsif /\A\s*(([-+]?(\d{2}|\d{4})|--)\d{2}\d{2}|
            ([-+]?(\d{2}|\d{4}))?\d{3}|-\d{3}|
            (\d{2}|\d{4})?w\d{2}\d)
      (t?
      \d{2}\d{2}(\d{2}([,.]\d+)?)?
      (z|[-+]\d{2}(\d{2})?)?)?\s*\z/ix =~ str
    _parse(str)
  elsif /\A\s*(\d{2}:\d{2}(:\d{2}([,.]\d+)?)?
      (z|[-+]\d{2}(:?\d{2})?)?)?\s*\z/ix =~ str
    _parse(str)
  elsif /\A\s*(\d{2}\d{2}(\d{2}([,.]\d+)?)?
      (z|[-+]\d{2}(\d{2})?)?)?\s*\z/ix =~ str
    _parse(str)
  end
end

._jisx0301(str) ⇒ Object

call-seq:

[ruby 1-9 only] <br />
_jisx0301(str) -> Hash or nil

Attempt to parse string using the JIS X 0301 date format or ISO8601 format. Returns a Hash of values if the string was parsed, and nil if not.



827
828
829
830
831
832
833
834
835
836
837
838
839
840
# File 'lib/date/format.rb', line 827

def self._jisx0301(str)
  if /\A\s*[mtsh]?\d{2}\.\d{2}\.\d{2}
      (t
      (\d{2}:\d{2}(:\d{2}([,.]\d*)?)?
      (z|[-+]\d{2}(:?\d{2})?)?)?)?\s*\z/ix =~ str
    if /\A\s*\d/ =~ str
      _parse(str.sub(/\A\s*(\d)/, 'h\1'))
    else
      _parse(str)
    end
  else
    _iso8601(str)
  end
end

._parse(str, comp = true) ⇒ Object

call-seq:

_parse(str, comp=true) -> Hash

Attempt to parse the string by trying a wide variety of date formats sequentially (unless a match is found by the fast Ragel-based parser). The comp argument determines whether to convert 2-digit years to 4-digit years. If the str is not in a supported format, an empty hash will be returned.

This method searches for a match anywhere in the string, unlike most of the other ruby 1.9-only parsing methods which require that an exact match for the entire string.



586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
# File 'lib/date/format.rb', line 586

def self._parse(str, comp=true)
  if v = _ragel_parse(str)
    return v
  end

  str = str.dup

  e = {:_ => {:comp => comp}}
  str.gsub!(/[^-+',.\/:@[:alnum:]\[\]]+/, ' ')

  _parse_time(str, e)
  _parse_day(str, e)

  _parse_eu(str, e)     ||
  _parse_us(str, e)     ||
  _parse_iso(str, e)    ||
  _parse_jis(str, e)    ||
  _parse_vms(str, e)    ||
  _parse_sla(str, e)    ||
  _parse_dot(str, e)    ||
  _parse_iso2(str, e)   ||
  _parse_year(str, e)   ||
  _parse_mon(str, e)    ||
  _parse_mday(str, e)   ||
  _parse_ddd(str, e)

  if str.sub!(/\b(bc\b|bce\b|b\.c\.|b\.c\.e\.)/i, ' ')
    if e[:year]
      e[:year] = -e[:year] + 1
    end
  end

  if str.sub!(/\A\s*(\d{1,2})\s*\z/, ' ')
    if e[:hour] && !e[:mday]
      v = $1.to_i
      if (1..31) === v
        e[:mday] = v
      end
    end
    if e[:mday] && !e[:hour]
      v = $1.to_i
      if (0..24) === v
        e[:hour] = v
      end
    end
  end

  if e[:_][:comp]
    if e[:cwyear]
      if e[:cwyear] >= 0 && e[:cwyear] <= 99
        e[:cwyear] += if e[:cwyear] >= 69
                    then 1900 else 2000 end
      end
    end
    if e[:year]
      if e[:year] >= 0 && e[:year] <= 99
        e[:year] += if e[:year] >= 69
                  then 1900 else 2000 end
      end
    end
  end

  e[:offset] ||= zone_to_diff(e[:zone]) if e[:zone]

  e.delete(:_)
  e
end

._rfc2822(str) ⇒ Object Also known as: _rfc822

call-seq:

[ruby 1-9 only] <br />
_rfc2822(str) -> Hash or nil

Attempt to parse string using the RFC 2822 format used in email. It’s similar to the preferred HTTP format except specific offsets can be used. Returns a Hash of values if the string was parsed, and nil if not.



765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
# File 'lib/date/format.rb', line 765

def self._rfc2822(str)
  if /\A\s*(?:(?:#{Format::ABBR_DAYS.keys.join('|')})\s*,\s+)?
      \d{1,2}\s+
      (?:#{Format::ABBR_MONTHS.keys.join('|')})\s+
      -?(\d{2,})\s+ # allow minus, anyway
      \d{2}:\d{2}(:\d{2})?\s*
      (?:[-+]\d{4}|ut|gmt|e[sd]t|c[sd]t|m[sd]t|p[sd]t|[a-ik-z])\s*\z/iox =~ str
    e = _parse(str, false)
    if $1.size < 4
      if e[:year] < 50
        e[:year] += 2000
      elsif e[:year] < 1000
        e[:year] += 1900
      end
    end
    e
  end
end

._rfc3339(str) ⇒ Object

call-seq:

[ruby 1-9 only] <br />
_rfc3339(str) -> Hash or nil

Attempt to parse string using the RFC 3339 format, which is the same as the ISO8601 civil format requiring a time and time zone. Returns a Hash of values if the string was parsed, and nil if not.



696
697
698
699
700
701
702
703
# File 'lib/date/format.rb', line 696

def self._rfc3339(str)
  if /\A\s*-?\d{4}-\d{2}-\d{2} # allow minus, anyway
      (t|\s)
      \d{2}:\d{2}:\d{2}(\.\d+)?
      (z|[-+]\d{2}:\d{2})\s*\z/ix =~ str
    _parse(str)
  end
end

._xmlschema(str) ⇒ Object

call-seq:

[ruby 1-9 only] <br />
_xmlschema(str) -> Hash or nil

Attempt to parse string using the XML schema format, which is similar to the ISO8601 civil format, with most parts being optional. Returns a Hash of values if the string was parsed, and nil if not.



713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
# File 'lib/date/format.rb', line 713

def self._xmlschema(str)
  if /\A\s*(-?\d{4,})(?:-(\d{2})(?:-(\d{2}))?)?
      (?:t
        (\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?)?
      (z|[-+]\d{2}:\d{2})?\s*\z/ix =~ str
    e = {}
    e[:year] = $1.to_i
    e[:mon] = $2.to_i if $2
    e[:mday] = $3.to_i if $3
    e[:hour] = $4.to_i if $4
    e[:min] = $5.to_i if $5
    e[:sec] = $6.to_i if $6
    e[:sec_fraction] = $7.to_i/10.0**$7.size if $7
    if $8
      e[:zone] = $8
      e[:offset] = zone_to_diff($8)
    end
    e
  elsif /\A\s*(\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?
      (z|[-+]\d{2}:\d{2})?\s*\z/ix =~ str
    e = {}
    e[:hour] = $1.to_i if $1
    e[:min] = $2.to_i if $2
    e[:sec] = $3.to_i if $3
    e[:sec_fraction] = $4.to_i/10.0**$4.size if $4
    if $5
      e[:zone] = $5
      e[:offset] = zone_to_diff($5)
    end
    e
  elsif /\A\s*(?:--(\d{2})(?:-(\d{2}))?|---(\d{2}))
      (z|[-+]\d{2}:\d{2})?\s*\z/ix =~ str
    e = {}
    e[:mon] = $1.to_i if $1
    e[:mday] = $2.to_i if $2
    e[:mday] = $3.to_i if $3
    if $4
      e[:zone] = $4
      e[:offset] = zone_to_diff($4)
    end
    e
  end
end

Instance Method Details

#+(n) ⇒ Date

Returns a Date that is n days after the receiver. n can be negative, in which case it returns a Date before the receiver.

Date.civil(2009, 1, 2) + 2
# => #<Date 2009-01-04>
Date.civil(2009, 1, 2) + -2
# => #<Date 2008-12-31>

Returns:



2747
2748
2749
# File 'ext/date_ext/date_ext.c', line 2747

static VALUE rhrd_op_plus(VALUE self, VALUE other) {
   return rhrd__add_days(self, NUM2LONG(other));
}

#-(n) ⇒ Date <br /> #-(date) ⇒ Integer <br /> #-(datetime) ⇒ Float

If a Numeric argument is given, it is treated as an Integer, and the number of days it represents is substracted from the receiver to return a new Date object. n can be negative, in which case the Date returned will be after the receiver.

If a Date argument is given, returns the number of days between the current date and the argument as an Integer.

If a DateTime argument is given, returns the number of days between the current date and the argument as a Float, and considers the receiver to be in the same time zone as the argument.

Other types of arguments raise a TypeError.

Date.civil(2009, 1, 2) - 2
# => #<Date 2008-12-31>
Date.civil(2009, 1, 2) - Date.civil(2009, 1, 1)
# => 1
Date.civil(2009, 1, 2) - DateTime.civil(2009, 1, 3, 12)
# => -1.5

Overloads:

  • #-(n) ⇒ Date <br />

    Returns:

  • #-(date) ⇒ Integer <br />

    Returns:

    • (Integer <br />)
  • #-(datetime) ⇒ Float

    Returns:

    • (Float)


2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
# File 'ext/date_ext/date_ext.c', line 2778

static VALUE rhrd_op_minus(VALUE self, VALUE other) {
  rhrd_t *d;
  rhrd_t *newd;
  rhrdt_t *newdt;
  Data_Get_Struct(self, rhrd_t, d);

  if (RTEST(rb_obj_is_kind_of(other, rb_cNumeric))) {
    return rhrd__add_days(self, -NUM2LONG(other));
  }
  if (RTEST((rb_obj_is_kind_of(other, rhrdt_class)))) {
    Data_Get_Struct(other, rhrdt_t, newdt);
    RHR_FILL_JD(d)
    RHRDT_FILL_JD(newdt)
    RHRDT_FILL_NANOS(newdt)
    return rb_float_new(d->jd - (newdt->jd + (double)newdt->nanos/RHR_NANOS_PER_DAYD));
  }
  if (RTEST((rb_obj_is_kind_of(other, rhrd_class)))) {
    Data_Get_Struct(other, rhrd_t, newd);
    RHR_FILL_JD(d)
    RHR_FILL_JD(newd)
    return LONG2NUM(rhrd__safe_add_long(d->jd, -newd->jd));
  }
  rb_raise(rb_eTypeError, "expected numeric or date");
}

#<<(n) ⇒ Date

Returns a Date that is n months before the receiver. n can be negative, in which case it returns a Date after the receiver.

Date.civil(2009, 1, 2) << 2
# => #<Date 2008-11-02>
Date.civil(2009, 1, 2) << -2
# => #<Date 2009-01-02>

Returns:



2731
2732
2733
# File 'ext/date_ext/date_ext.c', line 2731

static VALUE rhrd_op_left_shift(VALUE self, VALUE other) {
  return rhrd__add_months(self, -NUM2LONG(other));
}

#<=>(other) ⇒ -1, ...

If other is a Date, returns -1 if other is before the the receiver chronologically, 0 if other is the same date as the receiver, or 1 if other is after the receiver chronologically.

If other is a DateTime, return 0 if other has the same julian date as the receiver and no fractional part, -1 if other has a julian date less than the receiver’s, and 1 if other has a julian date greater than the receiver’s or a julian date the same as the receiver’s and a fractional part.

If other is a Numeric, convert it to an integer and compare it to the receiver’s julian date.

For an unrecognized type, return nil.

Returns:

  • (-1, 0, 1, nil)


2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
# File 'ext/date_ext/date_ext.c', line 2856

static VALUE rhrd_op_spaceship(VALUE self, VALUE other) {
  rhrd_t *d, *o;
  rhrdt_t *odt;
  long diff;
  Data_Get_Struct(self, rhrd_t, d);

  if (RTEST(rb_obj_is_kind_of(other, rhrdt_class))) {
    Data_Get_Struct(other, rhrdt_t, odt);
    RHR_FILL_JD(d)
    RHRDT_FILL_JD(odt)
    RHR_SPACE_SHIP(diff, d->jd, odt->jd)
    if (diff == 0) {
      RHRDT_FILL_NANOS(odt)
      RHR_SPACE_SHIP(diff, 0, odt->nanos)
    }
    return LONG2NUM(diff);
  } else if (RTEST(rb_obj_is_kind_of(other, rhrd_class))) {
    Data_Get_Struct(other, rhrd_t, o);
    return LONG2NUM(rhrd__spaceship(d, o));
  } else if (RTEST((rb_obj_is_kind_of(other, rb_cNumeric)))) {
    diff = NUM2LONG(other);
    RHR_FILL_JD(d)
    RHR_SPACE_SHIP(diff, d->jd, diff)
    return LONG2NUM(diff);
  }
  return Qnil;
}

#===(other) ⇒ Boolean

If other is a Date, returns true if other is the same date as the receiver, or false otherwise.

If other is a DateTime, return true if +other has the same julian date as the receiver, or false otherwise.

If other is a Numeric, convert it to an Integer and return true if it is equal to the receiver’s julian date, or false otherwise.

Returns:

  • (Boolean)


2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
# File 'ext/date_ext/date_ext.c', line 2816

static VALUE rhrd_op_relationship(VALUE self, VALUE other) {
  rhrd_t *d, *o;
  rhrdt_t *odt;
  long diff = 1;
  Data_Get_Struct(self, rhrd_t, d);

  if (RTEST(rb_obj_is_kind_of(other, rhrdt_class))) {
    Data_Get_Struct(other, rhrdt_t, odt);
    RHR_FILL_JD(d)
    RHRDT_FILL_JD(odt)
    diff = d->jd != odt->jd;
  } else if (RTEST(rb_obj_is_kind_of(other, rhrd_class))) {
    Data_Get_Struct(other, rhrd_t, o);
    diff = rhrd__spaceship(d, o);
  } else if (RTEST((rb_obj_is_kind_of(other, rb_cNumeric)))) {
    diff = NUM2LONG(other);
    RHR_FILL_JD(d)
    RHR_SPACE_SHIP(diff, d->jd, diff)
  }
  return diff == 0 ? Qtrue : Qfalse;
}

#>>(n) ⇒ Date

Returns a Date that is n months after the receiver. n can be negative, in which case it returns a Date before the receiver.

Date.civil(2009, 1, 2) >> 2
# => #<Date 2009-01-02>
Date.civil(2009, 1, 2) >> -2
# => #<Date 2008-11-02>

Returns:



2715
2716
2717
# File 'ext/date_ext/date_ext.c', line 2715

static VALUE rhrd_op_right_shift(VALUE self, VALUE other) {
  return rhrd__add_months(self, NUM2LONG(other));
}

#_dump(limit) ⇒ String

Returns a marshalled representation of the receiver as a String. Generally not called directly, usually called by Marshal.dump.

Returns:

  • (String)


2050
2051
2052
2053
2054
2055
# File 'ext/date_ext/date_ext.c', line 2050

static VALUE rhrd__dump(VALUE self, VALUE limit) {
  rhrd_t *d;
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_JD(d)
  return rb_marshal_dump(LONG2NUM(d->jd), LONG2NUM(NUM2LONG(limit) - 1));
}

#asctimeString Also known as: ctime

Returns a string representation of the receiver. Example:

Date.civil(2009, 1, 2).asctime
# => "Fri Jan  2 00:00:00 2009"

Returns:

  • (String)


2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
# File 'ext/date_ext/date_ext.c', line 2065

static VALUE rhrd_asctime(VALUE self) {
  VALUE s;
  rhrd_t *d;
  int len;
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_CIVIL(d)
  RHR_FILL_JD(d)

  s = rb_str_buf_new(128);
  len = snprintf(RSTRING_PTR(s), 128, "%s %s %2i 00:00:00 %04li", 
        rhrd__abbr_day_names[rhrd__jd_to_wday(d->jd)],
        rhrd__abbr_month_names[d->month],
        (int)d->day,
        d->year);
  if (len == -1 || len > 127) {
    rb_raise(rb_eNoMemError, "in Date#asctime (in snprintf)");
  }

  RHR_RETURN_RESIZED_STR(s, len)
}

#cwdayInteger

Returns the commercial week day as an Integer. Example:

Date.civil(2009, 1, 2).cwday
# => 5
Date.civil(2010, 1, 2).cwday
# => 6

Returns:

  • (Integer)


2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
# File 'ext/date_ext/date_ext.c', line 2096

static VALUE rhrd_cwday(VALUE self) {
  rhrd_t *d;
  rhrd_t n;
  RHR_CACHED_IV(self, rhrd_id_cwday)
  memset(&n, 0, sizeof(rhrd_t));
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_JD(d)
  n.jd = d->jd;
  rhrd__fill_commercial(&n);
  rhrd__set_cw_ivs(self, &n);
  return LONG2NUM(n.day);
}

#cweekInteger

Returns the commercial week as an Integer. Example:

Date.civil(2009, 1, 2).cweek
# => 1
Date.civil(2010, 1, 2).cweek
# => 53

Returns:

  • (Integer)


2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
# File 'ext/date_ext/date_ext.c', line 2119

static VALUE rhrd_cweek(VALUE self) {
  rhrd_t *d;
  rhrd_t n;
  RHR_CACHED_IV(self, rhrd_id_cweek)
  memset(&n, 0, sizeof(rhrd_t));
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_JD(d)
  n.jd = d->jd;
  rhrd__fill_commercial(&n);
  rhrd__set_cw_ivs(self, &n);
  return LONG2NUM(n.month);
}

#cwyearInteger

Returns the commercial week year as an Integer. Example:

Date.civil(2009, 1, 2).cwyear
# => 2009
Date.civil(2010, 1, 2).cwyear
# => 2009

Returns:

  • (Integer)


2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
# File 'ext/date_ext/date_ext.c', line 2142

static VALUE rhrd_cwyear(VALUE self) {
  rhrd_t *d;
  rhrd_t n;
  RHR_CACHED_IV(self, rhrd_id_cwyear)
  memset(&n, 0, sizeof(rhrd_t));
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_JD(d)
  n.jd = d->jd;
  rhrd__fill_commercial(&n);
  rhrd__set_cw_ivs(self, &n);
  return LONG2NUM(n.year);
}

#dayInteger Also known as: mday

Returns the day of the month as an Integer. Example:

Date.civil(2009, 1, 2).day
# => 2

Returns:

  • (Integer)


2163
2164
2165
2166
2167
2168
# File 'ext/date_ext/date_ext.c', line 2163

static VALUE rhrd_day(VALUE self) {
  rhrd_t *d;
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_CIVIL(d)
  return LONG2NUM(d->day);
}

#day_fraction0

Date objects due not hold fractional days, so 0 is always returned.

Returns:

  • (0)


2175
2176
2177
# File 'ext/date_ext/date_ext.c', line 2175

static VALUE rhrd_day_fraction(VALUE self) {
  return INT2FIX(0);
}

#downto(target) {|date| ... } ⇒ Date

Equivalent to calling step with the target as the first argument and -1 as the second argument. Returns self.

Date.civil(2009, 1, 2).downto(Date.civil(2009, 1, 1)) do |date|
  puts date
end
# Output:
# 2009-01-02
# 2009-01-01

Yields:

  • (date)

Returns:



2192
2193
2194
2195
2196
2197
# File 'ext/date_ext/date_ext.c', line 2192

static VALUE rhrd_downto(VALUE self, VALUE other) {
  VALUE argv[2];
  argv[0] = other;
  argv[1] = INT2FIX(-1);
  return rhrd_step(2, argv, self);
}

#eql?(date) ⇒ Boolean

Returns true only if the date given is the same date as the receiver. If date is an instance of DateTime, returns true only if date is for the same date as the receiver and has no fractional component. Otherwise, returns false. Example:

Date.civil(2009, 1, 2).eql?(Date.civil(2009, 1, 2))
# => true
Date.civil(2009, 1, 2).eql?(Date.civil(2009, 1, 1))
# => false
Date.civil(2009, 1, 2).eql?(DateTime.civil(2009, 1, 2))
# => true
Date.civil(2009, 1, 2).eql?(DateTime.civil(2009, 1, 2, 1))
# => false

Returns:

  • (Boolean)


2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
# File 'ext/date_ext/date_ext.c', line 2216

static VALUE rhrd_eql_q(VALUE self, VALUE other) {
  rhrd_t *d, *o;
  rhrdt_t *odt;
  long diff;
  Data_Get_Struct(self, rhrd_t, d);

  if (RTEST(rb_obj_is_kind_of(other, rhrdt_class))) {
    Data_Get_Struct(other, rhrdt_t, odt);
    RHR_FILL_JD(d)
    RHRDT_FILL_JD(odt)
    RHR_SPACE_SHIP(diff, d->jd, odt->jd)
    if (diff == 0) {
      RHRDT_FILL_NANOS(odt)
      RHR_SPACE_SHIP(diff, 0, odt->nanos)
    }
    return diff == 0 ? Qtrue : Qfalse;
  } else if (RTEST(rb_obj_is_kind_of(other, rhrd_class))) {
    Data_Get_Struct(other, rhrd_t, o);
    return rhrd__spaceship(d, o) == 0 ? Qtrue : Qfalse;
  }
  return Qfalse;
}

#friday?Boolean

friday?() -> true or false

Returns true if the receiver is a Friday, false otherwise.

Returns:

  • (Boolean)


3578
3579
3580
# File 'ext/date_ext/date_ext.c', line 3578

static VALUE rhrd_friday_q(VALUE self) {
  return rhrd__day_q(self, 5);
}

#gregorianDate Also known as: england, italy, julian, to_date

This library does not support modifying the day of calendar reform, so this always returns self.

Returns:



2245
2246
2247
# File 'ext/date_ext/date_ext.c', line 2245

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

#gregorian?true Also known as: ns?

This library always uses the Gregorian calendar, so this always returns true.

Returns:

  • (true)


2255
2256
2257
# File 'ext/date_ext/date_ext.c', line 2255

static VALUE rhrd_gregorian_q(VALUE self) {
  return Qtrue;
}

#hashInteger

Return an Integer hash value for the receiver, such that an equal date will have the same hash value.

Returns:

  • (Integer)


2265
2266
2267
2268
2269
2270
2271
# File 'ext/date_ext/date_ext.c', line 2265

static VALUE rhrd_hash(VALUE self) {
  rhrd_t *d;
  RHR_CACHED_IV(self, rhrd_id_hash)
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_JD(d)
  return rb_ivar_set(self, rhrd_id_hash, rb_funcall(LONG2NUM(d->jd), rhrd_id_hash, 0));
}

#httpdateObject

httpdate() -> String

Returns the receiver as a String in HTTP format. Example:

Date.civil(2009, 1, 2).httpdate
# => "Fri, 02 Jan 2009 00:00:00 GMT"


3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
# File 'ext/date_ext/date_ext.c', line 3138

static VALUE rhrd_httpdate(VALUE self) {
  VALUE s;
  rhrd_t *d;
  int len;
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_CIVIL(d)
  RHR_FILL_JD(d)

  s = rb_str_buf_new(128);
  len = snprintf(RSTRING_PTR(s), 128, "%s, %02i %s %04li 00:00:00 GMT", 
        rhrd__abbr_day_names[rhrd__jd_to_wday(d->jd)],
        (int)d->day,
        rhrd__abbr_month_names[d->month],
        d->year);
  if (len == -1 || len > 127) {
    rb_raise(rb_eNoMemError, "in Date#httpdate (in snprintf)");
  }

  RHR_RETURN_RESIZED_STR(s, len)
}

#inspectString

Return a developer-friendly string containing the civil date for the receiver. Example:

Date.civil(2009, 1, 2).inspect
# => "#<Date 2009-01-02>"

Returns:

  • (String)


2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
# File 'ext/date_ext/date_ext.c', line 2282

static VALUE rhrd_inspect(VALUE self) {
  VALUE s;
  rhrd_t *d;
  int len;
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_CIVIL(d)

  s = rb_str_buf_new(128);
  len = snprintf(RSTRING_PTR(s), 128, "#<Date %04li-%02i-%02i>",
        d->year, (int)d->month, (int)d->day);
  if (len == -1 || len > 127) {
    rb_raise(rb_eNoMemError, "in Date#inspect (in snprintf)");
  }

  RHR_RETURN_RESIZED_STR(s, len)
}

#jdInteger Also known as: ajd

Return the julian day number for the receiver as an Integer.

Date.civil(2009, 1, 2).jd
# => 2454834

Returns:

  • (Integer)


2307
2308
2309
2310
2311
2312
# File 'ext/date_ext/date_ext.c', line 2307

static VALUE rhrd_jd(VALUE self) {
  rhrd_t *d;
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_JD(d)
  return LONG2NUM(d->jd);
}

#jisx0301Object

jisx0301() -> String

Returns the receiver as a String in JIS X 0301 format. Example:

Date.civil(2009, 1, 2).jisx0301
# => "H21.01.02"


3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
# File 'ext/date_ext/date_ext.c', line 3168

static VALUE rhrd_jisx0301(VALUE self) {
  VALUE s;
  rhrd_t *d;
  int len;
  char c;
  long year;
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_CIVIL(d)
  RHR_FILL_JD(d)

  s = rb_str_buf_new(128);
  if (d->jd < 2405160) {
    len = snprintf(RSTRING_PTR(s), 128, "%04li-%02i-%02i", d->year, (int)d->month, (int)d->day);
  } else {
    if (d->jd >= 2447535) {
      c = 'H';
      year = d->year - 1988;
    } else if (d->jd >= 2424875) {
      c = 'S';
      year = d->year - 1925;
    } else if (d->jd >= 2419614) {
      c = 'T';
      year = d->year - 1911;
    } else {
      c = 'M';
      year = d->year - 1867;
    }
    len = snprintf(RSTRING_PTR(s), 128, "%c%02li.%02i.%02i", c, year, (int)d->month, (int)d->day);
  }
  if (len == -1 || len > 127) {
    rb_raise(rb_eNoMemError, "in Date#jisx0301 (in snprintf)");
  }

  RHR_RETURN_RESIZED_STR(s, len)
}

#julian?false Also known as: os?

This library always uses the Gregorian calendar, so this always returns false.

Returns:

  • (false)


2320
2321
2322
# File 'ext/date_ext/date_ext.c', line 2320

static VALUE rhrd_julian_q(VALUE self) {
  return Qfalse;
}

#ldInteger

Return the number of days since the Lilian Date (the day of calendar reform in Italy).

Date.civil(2009, 1, 2).ld
# => 155674

Returns:

  • (Integer)


2333
2334
2335
2336
2337
2338
# File 'ext/date_ext/date_ext.c', line 2333

static VALUE rhrd_ld(VALUE self) {
  rhrd_t *d;
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_JD(d)
  return LONG2NUM(d->jd - RHR_JD_LD);
}

#leap?Boolean

Return true if the current year for this date is a leap year in the Gregorian calendar, false otherwise.

Date.civil(2009, 1, 2).leap?
# => false
Date.civil(2008, 1, 2).leap?
# => true

Returns:

  • (Boolean)


2351
2352
2353
2354
2355
2356
# File 'ext/date_ext/date_ext.c', line 2351

static VALUE rhrd_leap_q(VALUE self) {
  rhrd_t *d;
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_CIVIL(d)
  return rhrd__leap_year(d->year) ? Qtrue : Qfalse;
}

#mjdInteger Also known as: amjd

Return the number of days since 1858-11-17.

Date.civil(2009, 1, 2).mjd
# => 54833

Returns:

  • (Integer)


2366
2367
2368
2369
2370
2371
# File 'ext/date_ext/date_ext.c', line 2366

static VALUE rhrd_mjd(VALUE self) {
  rhrd_t *d;
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_JD(d)
  return LONG2NUM(d->jd - RHR_JD_MJD);
}

#monday?Boolean

monday?() -> true or false

Returns true if the receiver is a Monday, false otherwise.

Returns:

  • (Boolean)


3538
3539
3540
# File 'ext/date_ext/date_ext.c', line 3538

static VALUE rhrd_monday_q(VALUE self) {
  return rhrd__day_q(self, 1);
}

#monthInteger Also known as: mon

Returns the number of the month as an Integer. Example:

Date.civil(2009, 1, 2).month
# => 1

Returns:

  • (Integer)


2381
2382
2383
2384
2385
2386
# File 'ext/date_ext/date_ext.c', line 2381

static VALUE rhrd_month(VALUE self) {
  rhrd_t *d;
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_CIVIL(d)
  return LONG2NUM(d->month);
}

#new_start(sg = nil) ⇒ Date Also known as: newsg

Returns self. Ignores an argument if given.

Returns:



2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
# File 'ext/date_ext/date_ext.c', line 2405

static VALUE rhrd_new_start(int argc, VALUE *argv, VALUE self) {
  switch(argc) {
    case 0:
    case 1:
      break;
    default:
      rb_raise(rb_eArgError, "wrong number of arguments: %i for 1", argc);
      break;
  }

  return self;
}

#nextDate Also known as: succ

Returns the Date after the receiver’s date:

Date.civil(2009, 1, 2).next
# => #<Date 2009-01-03>

Returns:



2396
2397
2398
# File 'ext/date_ext/date_ext.c', line 2396

static VALUE rhrd_next(VALUE self) {
   return rhrd__add_days(self, 1);
}

#next_day(*args) ⇒ Object

next_day(n=1) -> Date

Returns a Date n days after the receiver. If n is negative, returns a Date before the receiver.

Date.civil(2009, 1, 2).next_day
# => #<Date 2009-01-03>
Date.civil(2009, 1, 2).next_day(2)
# => #<Date 2009-01-04>


3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
# File 'ext/date_ext/date_ext.c', line 3216

static VALUE rhrd_next_day(int argc, VALUE *argv, VALUE self) {
  long i;

  switch(argc) {
    case 0:
      i = 1;
      break;
    case 1:
      i = NUM2LONG(argv[0]);
      break;
    default:
      rb_raise(rb_eArgError, "wrong number of arguments: %i for 1", argc);
      break;
  }

   return rhrd__add_days(self, i);
}

#next_month(*args) ⇒ Object

next_month(n=1) -> Date

Returns a Date n months after the receiver. If n is negative, returns a Date before the receiver.

Date.civil(2009, 1, 2).next_month
# => #<Date 2009-02-02>
Date.civil(2009, 1, 2).next_month(2)
# => #<Date 2009-03-02>


3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
# File 'ext/date_ext/date_ext.c', line 3246

static VALUE rhrd_next_month(int argc, VALUE *argv, VALUE self) {
  long i;

  switch(argc) {
    case 0:
      i = 1;
      break;
    case 1:
      i = NUM2LONG(argv[0]);
      break;
    default:
      rb_raise(rb_eArgError, "wrong number of arguments: %i for 1", argc);
      break;
  }

  return rhrd__add_months(self, i);
}

#next_year(*args) ⇒ Object

next_year(n=1) -> Date

Returns a Date n years after the receiver. If n is negative, returns a Date before the receiver.

Date.civil(2009, 1, 2).next_year
# => #<Date 2010-01-02>
Date.civil(2009, 1, 2).next_year(2)
# => #<Date 2011-01-02>


3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
# File 'ext/date_ext/date_ext.c', line 3276

static VALUE rhrd_next_year(int argc, VALUE *argv, VALUE self) {
  long i;

  switch(argc) {
    case 0:
      i = 1;
      break;
    case 1:
      i = NUM2LONG(argv[0]);
      break;
    default:
      rb_raise(rb_eArgError, "wrong number of arguments: %i for 1", argc);
      break;
  }

  return rhrd__add_years(self, i);
}

#prev_day(*args) ⇒ Object

prev_day(n=1) -> Date

Returns a Date n days before the receiver. If n is negative, returns a Date after the receiver.

Date.civil(2009, 1, 2).prev_day
# => #<Date 2009-01-01>
Date.civil(2009, 1, 2).prev_day(2)
# => #<Date 2008-12-31>


3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
# File 'ext/date_ext/date_ext.c', line 3306

static VALUE rhrd_prev_day(int argc, VALUE *argv, VALUE self) {
  long i;

  switch(argc) {
    case 0:
      i = -1;
      break;
    case 1:
      i = -NUM2LONG(argv[0]);
      break;
    default:
      rb_raise(rb_eArgError, "wrong number of arguments: %i for 1", argc);
      break;
  }

   return rhrd__add_days(self, i);
}

#prev_month(*args) ⇒ Object

prev_month(n=1) -> Date

Returns a Date n months before the receiver. If n is negative, returns a Date after the receiver.

Date.civil(2009, 1, 2).prev_month
# => #<Date 2008-12-02>
Date.civil(2009, 1, 2).prev_month(2)
# => #<Date 2008-11-02>


3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
# File 'ext/date_ext/date_ext.c', line 3336

static VALUE rhrd_prev_month(int argc, VALUE *argv, VALUE self) {
  long i;

  switch(argc) {
    case 0:
      i = -1;
      break;
    case 1:
      i = -NUM2LONG(argv[0]);
      break;
    default:
      rb_raise(rb_eArgError, "wrong number of arguments: %i for 1", argc);
      break;
  }

  return rhrd__add_months(self, i);
}

#prev_year(*args) ⇒ Object

prev_year(n=1) -> Date

Returns a Date n years before the receiver. If n is negative, returns a Date after the receiver.

Date.civil(2009, 1, 2).prev_year
# => #<Date 2008-01-02>
Date.civil(2009, 1, 2).prev_year(2)
# => #<Date 2007-01-02>


3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
# File 'ext/date_ext/date_ext.c', line 3366

static VALUE rhrd_prev_year(int argc, VALUE *argv, VALUE self) {
  long i;

  switch(argc) {
    case 0:
      i = -1;
      break;
    case 1:
      i = -NUM2LONG(argv[0]);
      break;
    default:
      rb_raise(rb_eArgError, "wrong number of arguments: %i for 1", argc);
      break;
  }

  return rhrd__add_years(self, i);
}

#rfc2822Object Also known as: rfc822

rfc2822() -> String

Returns the receiver as a String in RFC2822 format. Example:

Date.civil(2009, 1, 2).rfc2822
# => "Fri, 2 Jan 2009 00:00:00 +0000"


3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
# File 'ext/date_ext/date_ext.c', line 3393

static VALUE rhrd_rfc2822(VALUE self) {
  VALUE s;
  rhrd_t *d;
  int len;
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_CIVIL(d)
  RHR_FILL_JD(d)

  s = rb_str_buf_new(128);
  len = snprintf(RSTRING_PTR(s), 128, "%s, %i %s %04li 00:00:00 +0000", 
        rhrd__abbr_day_names[rhrd__jd_to_wday(d->jd)],
        (int)d->day,
        rhrd__abbr_month_names[d->month],
        d->year);
  if (len == -1 || len > 127) {
    rb_raise(rb_eNoMemError, "in Date#rfc2822 (in snprintf)");
  }

  RHR_RETURN_RESIZED_STR(s, len)
}

#rfc3339Object

rfc3339() -> String

Returns the receiver as a String in RFC3339 format. Example:

Date.civil(2009, 1, 2).rfc3339
# => "2009-01-02T00:00:00+00:00"


3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
# File 'ext/date_ext/date_ext.c', line 3423

static VALUE rhrd_rfc3339(VALUE self) {
  VALUE s;
  rhrd_t *d;
  int len;
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_CIVIL(d)

  s = rb_str_buf_new(128);
  len = snprintf(RSTRING_PTR(s), 128, "%04li-%02i-%02iT00:00:00+00:00", d->year, (int)d->month, (int)d->day);
  if (len == -1 || len > 127) {
    rb_raise(rb_eNoMemError, "in Date#rfc3339 (in snprintf)");
  }

  RHR_RETURN_RESIZED_STR(s, len)
}

#saturday?Boolean

saturday?() -> true or false

Returns true if the receiver is a Saturday, false otherwise.

Returns:

  • (Boolean)


3588
3589
3590
# File 'ext/date_ext/date_ext.c', line 3588

static VALUE rhrd_saturday_q(VALUE self) {
  return rhrd__day_q(self, 6);
}

#startInteger Also known as: sg

Returns a number lower than the lowest julian date that can be correctly handled. Because this library always uses the Gregorian calendar, the day of calendar reform is chosen to be less than any date that this library can handle.

Returns:

  • (Integer)


2426
2427
2428
# File 'ext/date_ext/date_ext.c', line 2426

static VALUE rhrd_start(VALUE self) {
  return rhrd_start_num;
}

#step(target, step = 1) {|date| ... } ⇒ Date

Yields Date objects between the receiver and the target date (inclusive), with step integer days between each yielded date. step can be negative, in which case the dates are yielded in reverse chronological order. Returns self in all cases.

If target is equal to the receiver, yields self once regardless of step. It target is less than receiver and step is nonnegative, or target is greater than receiver and step is nonpositive, does not yield.

Date.civil(2009, 1, 2).step(Date.civil(2009, 1, 6), 2) do |date|
  puts date
end
# Output:
# 2009-01-02
# 2009-01-04
# 2009-01-06

Yields:

  • (date)

Returns:



2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
# File 'ext/date_ext/date_ext.c', line 2451

static VALUE rhrd_step(int argc, VALUE *argv, VALUE self) {
  rhrd_t *d, *n, *newd;
  rhrdt_t *ndt;
  long step, limit, current;
  VALUE rlimit, new, rstep, klass;
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_JD(d)

  switch(argc) {
    case 1:
      step = 1;
      rstep = LONG2NUM(step);
      break;
    case 2:
      rstep = argv[1];
      step = NUM2LONG(rstep);
      break;
    default:
      rb_raise(rb_eArgError, "wrong number of arguments: %i for 2", argc);
      break;
  }
  rlimit = argv[0];
  klass = rb_obj_class(self);

#ifdef RUBY19
  if (!rb_block_given_p()) {
    return rb_funcall(self, rhrd_id_to_enum, 3, rhrd_sym_step, rlimit, rstep);
  }
#else
  rb_need_block();
#endif

  if (RTEST(rb_obj_is_kind_of(rlimit, rb_cNumeric))) {
    limit = NUM2LONG(rlimit);
  } else if (RTEST((rb_obj_is_kind_of(rlimit, rhrdt_class)))) {
    Data_Get_Struct(rlimit, rhrdt_t, ndt);
    RHRDT_FILL_JD(ndt)
    limit = ndt->jd;
  } else if (RTEST((rb_obj_is_kind_of(rlimit, rhrd_class)))) {
    Data_Get_Struct(rlimit, rhrd_t, n);
    RHR_FILL_JD(n)
    limit = n->jd;
  } else {
    rb_raise(rb_eTypeError, "expected numeric or date");
  }

  current = d->jd;
  if (limit > current) {
    if (step > 0) {
      while(limit >= current) {
        new = Data_Make_Struct(klass, rhrd_t, NULL, -1, newd);
        newd->jd = current;
        RHR_CHECK_JD(newd)
        newd->flags = RHR_HAVE_JD;
        current += step;
        rb_yield(new);
      }
    }
  } else if (limit < current) {
    if (step < 0) {
      while(limit <= current) {
        new = Data_Make_Struct(klass, rhrd_t, NULL, -1, newd);
        newd->jd = current;
        RHR_CHECK_JD(newd)
        newd->flags = RHR_HAVE_JD;
        current += step;
        rb_yield(new);
      }
    }
  } else {
    rb_yield(self);
  }

  return self;
}

#strftimeString <br /> #strftime(format) ⇒ String

If no argument is provided, returns a string in ISO8601 format, just like to_s. If an argument is provided, uses it as a format string and returns a String based on the format. The following formats parts are supported:

%a

The abbreviated day name (e.g. Fri)

%A

The full day name (e.g. Friday)

%b, %h

The abbreviated month name (e.g. Jan)

%B

The full month name (e.g. January)

%c

A full date and time representation (e.g. Fri Jan 02 13:29:39 2009)

%C

The century of the year (e.g. 20)

%d

The day of the month, with a leading zero if necessary (e.g. 02)

%e

The day of the month, with a leading space if necessary (e.g. 2)

%F

An ISO8601 date representation (e.g. 2009-01-02)

%g

The last 2 digits of the commercial week year (e.g. 09)

%G

The commercial week year (e.g. 2009)

%H

The hour of the day in 24 hour format, with a leading zero if necessary (e.g. 13)

%I

The hour of the day in 12 hour format (e.g. 01)

%j

The day of the year (e.g. 002)

%k

The hour of the day in 24 hour format, with a leading space if necessary (e.g. 13)

%l

The hour of the day in 12 hour format, with a leading space if necessary (e.g. 13)

%L

The number of milliseconds in the fractional second, with leading zeros if necessary (e.g. 079)

%m

The month of the year (e.g. 01)

%M

The minute of the hour (e.g. 29)

%n

A newline (e.g. “n”)

%N

The number of nanoseconds in the fractional second, with leading zeros if necessary (e.g. 079013023)

%p

The meridian indicator, upcased (e.g. PM)

%P

The meridian indicator, downcased (e.g. pm)

%Q

The number of milliseconds since the unix epoch (e.g. 1230902979079)

%r

A full time representation in 12 hour format (e.g. 1:29:39 PM)

%R

An hour and minute representation in 24 hour format (e.g. 13:29)

%s

The number of seconds since the unix epoch (e.g. 1230902979)

%S

The second of the minute (e.g. 39)

%t

A tab (e.g. “t”)

%T, %X

A full time representation in 24 hour format (e.g. 13:29:39)

%u

The commercial week day (e.g. 5)

%U

The week number of the current year, with Sunday as the first day of the first week (e.g. 0)

%v

A full date representation (e.g. 2-Jan-2009)

%V

The commercial week (e.g. 01)

%w

The day of the week, with Sunday as 0 and Saturday as 6 (e.g. 5)

%W

The week number of the current year, with Monday as the first day of the first week (e.g. 0)

%x, %D

A full date representation in month/day/year format (e.g. 01/02/09)

%y

The last two digits of the year (e.g. 09)

%Y

The year (e.g. 2009)

%z

The offset from UTC, without a colon (e.g. +0000)

%Z

The offset from UTC, with a colon (e.g. +00:00)

%+

A full date and time representation, including the offset (e.g. Fri Jan 2 13:29:39 +00:00 2009)

All other formats (e.g. %f, %%) are handled by removing the leading percent sign. All other text is passed through literally.

Overloads:

  • #strftimeString <br />

    Returns:

    • (String <br />)
  • #strftime(format) ⇒ String

    Returns:

    • (String)


2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
# File 'ext/date_ext/date_ext.c', line 2581

static VALUE rhrd_strftime(int argc, VALUE *argv, VALUE self) {
  rhrd_t *d;
  rhrdt_t dt;
  VALUE r;

  switch(argc) {
    case 0:
      return rhrd_to_s(self);
    case 1:
      r = rb_str_to_str(argv[0]);
      break;
    default:
      rb_raise(rb_eArgError, "wrong number of arguments: %i for 1", argc);
      break;
  }

  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_CIVIL(d)
  RHR_FILL_JD(d)
  memset(&dt, 0, sizeof(rhrdt_t));
  dt.jd = d->jd;
  dt.year = d->year;
  dt.month = d->month;
  dt.day = d->day;
  dt.flags = RHR_HAVE_CIVIL | RHR_HAVE_JD;
  return rhrd__strftime(&dt, RSTRING_PTR(r), RSTRING_LEN(r));
}

#sunday?Boolean

sunday?() -> true or false

Returns true if the receiver is a Sunday, false otherwise.

Returns:

  • (Boolean)


3528
3529
3530
# File 'ext/date_ext/date_ext.c', line 3528

static VALUE rhrd_sunday_q(VALUE self) {
  return rhrd__day_q(self, 0);
}

#thursday?Boolean

thursday?() -> true or false

Returns true if the receiver is a Thursday, false otherwise.

Returns:

  • (Boolean)


3568
3569
3570
# File 'ext/date_ext/date_ext.c', line 3568

static VALUE rhrd_thursday_q(VALUE self) {
  return rhrd__day_q(self, 4);
}

#to_datetimeObject

to_datetime() -> DateTime

Returns a DateTime equal to the receiver.

Date.civil(2009, 1, 2).to_datetime
# => #<DateTime 2009-01-02T00:00:00+00:00>


3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
# File 'ext/date_ext/date_ext.c', line 3448

static VALUE rhrd_to_datetime(VALUE self) {
  rhrd_t *d;
  rhrdt_t *dt;
  VALUE rdt = Data_Make_Struct(rhrdt_class, rhrdt_t, NULL, -1, dt);
  Data_Get_Struct(self, rhrd_t, d);

  if (RHR_HAS_CIVIL(d)) {
    dt->year = d->year;
    dt->month = d->month;
    dt->day = d->day;
    dt->flags |= RHR_HAVE_CIVIL;
  }
  if (RHR_HAS_JD(d)) {
    dt->jd = d->jd;
    dt->flags |= RHR_HAVE_JD;
  }

  return rdt;
}

#to_sString Also known as: iso8601, xmlschema

Returns the receiver as an ISO8601 formatted string.

Date.civil(2009, 1, 2).to_s
# => "2009-01-02"

Returns:

  • (String)


2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
# File 'ext/date_ext/date_ext.c', line 2617

static VALUE rhrd_to_s(VALUE self) {
  VALUE s;
  rhrd_t *d;
  int len;
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_CIVIL(d)

  s = rb_str_buf_new(128);
  len = snprintf(RSTRING_PTR(s), 128, "%04li-%02i-%02i",
        d->year, (int)d->month, (int)d->day);
  if (len == -1 || len > 127) {
    rb_raise(rb_eNoMemError, "in Date#to_s (in snprintf)");
  }

  RHR_RETURN_RESIZED_STR(s, len)
}

#to_timeObject

to_time() -> Time

Returns a Time in local time with the same year, month, and day as the receiver.

Date.civil(2009, 1, 2).to_time
# => 2009-01-02 00:00:00 -0800


3478
3479
3480
3481
3482
3483
# File 'ext/date_ext/date_ext.c', line 3478

static VALUE rhrd_to_time(VALUE self) {
  rhrd_t *d;
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_CIVIL(d)
  return rb_funcall(rb_cTime, rhrd_id_local, 3, LONG2NUM(d->year), LONG2NUM(d->month), LONG2NUM(d->day));
}

#tuesday?Boolean

tuesday?() -> true or false

Returns true if the receiver is a Tuesday, false otherwise.

Returns:

  • (Boolean)


3548
3549
3550
# File 'ext/date_ext/date_ext.c', line 3548

static VALUE rhrd_tuesday_q(VALUE self) {
  return rhrd__day_q(self, 2);
}

#upto(target) {|date| ... } ⇒ Date

Equivalent to calling step with the target as the first argument. Returns self.

Date.civil(2009, 1, 1).upto(Date.civil(2009, 1, 2)) do |date|
  puts date
end
# Output:
# 2009-01-01
# 2009-01-02

Yields:

  • (date)

Returns:



2647
2648
2649
2650
2651
# File 'ext/date_ext/date_ext.c', line 2647

static VALUE rhrd_upto(VALUE self, VALUE other) {
  VALUE argv[1];
  argv[0] = other;
  return rhrd_step(1, argv, self);
}

#wdayInteger

Returns the day of the week as an Integer, where Sunday is 0 and Saturday is 6. Example:

Date.civil(2009, 1, 2).wday
# => 5

Returns:

  • (Integer)


2662
2663
2664
2665
2666
2667
# File 'ext/date_ext/date_ext.c', line 2662

static VALUE rhrd_wday(VALUE self) {
  rhrd_t *d;
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_JD(d)
  return LONG2NUM(rhrd__jd_to_wday(d->jd));
}

#wednesday?Boolean

wednesday?() -> true or false

Returns true if the receiver is a Wednesday, false otherwise.

Returns:

  • (Boolean)


3558
3559
3560
# File 'ext/date_ext/date_ext.c', line 3558

static VALUE rhrd_wednesday_q(VALUE self) {
  return rhrd__day_q(self, 3);
}

#ydayInteger

Returns the day of the year as an Integer, where January 1st is 1 and December 31 is 365 (or 366 if the year is a leap year). Example:

Date.civil(2009, 2, 2).yday
# => 33

Returns:

  • (Integer)


2679
2680
2681
2682
2683
2684
# File 'ext/date_ext/date_ext.c', line 2679

static VALUE rhrd_yday(VALUE self) {
  rhrd_t *d;
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_CIVIL(d)
  return LONG2NUM(rhrd__ordinal_day(d->year, d->month, d->day));
}

#yearInteger

Returns the year as an Integer. Example:

Date.civil(2009, 1, 2).year
# => 2009

Returns:

  • (Integer)


2694
2695
2696
2697
2698
2699
# File 'ext/date_ext/date_ext.c', line 2694

static VALUE rhrd_year(VALUE self) {
  rhrd_t *d;
  Data_Get_Struct(self, rhrd_t, d);
  RHR_FILL_CIVIL(d)
  return LONG2NUM(d->year);
}