Class: Date

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

Direct Known Subclasses

DateTime

Defined Under Namespace

Modules: Format Classes: Infinity

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)
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.



872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
# File 'lib/date/format.rb', line 872

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.



739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
# File 'lib/date/format.rb', line 739

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.



903
904
905
906
907
908
909
910
911
912
913
914
915
916
# File 'lib/date/format.rb', line 903

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.



662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# File 'lib/date/format.rb', line 662

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.



841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
# File 'lib/date/format.rb', line 841

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.



772
773
774
775
776
777
778
779
# File 'lib/date/format.rb', line 772

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.



789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
# File 'lib/date/format.rb', line 789

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:



2799
2800
2801
# File 'ext/date_ext/date_ext.c', line 2799

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)


2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
# File 'ext/date_ext/date_ext.c', line 2830

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:



2783
2784
2785
# File 'ext/date_ext/date_ext.c', line 2783

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)


2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
# File 'ext/date_ext/date_ext.c', line 2908

static VALUE rhrd_op_spaceship(VALUE self, VALUE other) {
  if (RTEST(rb_obj_is_kind_of(self, rhrdt_class))) {
    rhrdt_t *dt, *odt;
    rhrd_t *od;
    double diff;
    int res;

    if (RTEST(rb_obj_is_kind_of(other, rhrdt_class))) {
      self = rhrdt__new_offset(self, 0.0);
      other = rhrdt__new_offset(other, 0.0);
      Data_Get_Struct(self, rhrdt_t, dt);
      Data_Get_Struct(other, rhrdt_t, odt);
      return LONG2NUM(rhrdt__spaceship(dt, odt));
    }
    if (RTEST(rb_obj_is_kind_of(other, rhrd_class))) {
      Data_Get_Struct(self, rhrdt_t, dt);
      Data_Get_Struct(other, rhrd_t, od);
      RHRDT_FILL_JD(dt)
      RHR_FILL_JD(od)
      RHR_SPACE_SHIP(res, dt->jd, od->jd)
      if (res == 0) {
        RHRDT_FILL_NANOS(dt)
        RHR_SPACE_SHIP(res, dt->nanos, 0)
      }
      return LONG2NUM(res);
    }
    if (RTEST((rb_obj_is_kind_of(other, rb_cNumeric)))) {
      Data_Get_Struct(self, rhrdt_t, dt);
      diff = NUM2DBL(other);
      RHRDT_FILL_JD(dt)
      RHR_SPACE_SHIP(res, dt->jd, (long)diff)
      if (res == 0) {
        RHRDT_FILL_NANOS(dt)
        RHR_SPACE_SHIP(res, dt->nanos, llround((diff - floor(diff)) * RHR_NANOS_PER_DAY))
      }
      return LONG2NUM(res);
    }
  } else {
    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)


2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
# File 'ext/date_ext/date_ext.c', line 2868

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:



2767
2768
2769
# File 'ext/date_ext/date_ext.c', line 2767

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)


2067
2068
2069
2070
2071
2072
# File 'ext/date_ext/date_ext.c', line 2067

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)


2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
# File 'ext/date_ext/date_ext.c', line 2082

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 %2d 00:00:00 %04ld", 
        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)
}

#cloneDate

Returns a clone of the receiver.

Returns:



2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
# File 'ext/date_ext/date_ext.c', line 2108

static VALUE rhrd_clone(VALUE self) {
  rhrd_t *d, *nd;
  VALUE rd = rb_call_super(0, NULL);
  if (!rb_obj_is_kind_of(self, rhrdt_class)) {
    Data_Get_Struct(self, rhrd_t, d);
    Data_Get_Struct(rd, rhrd_t, nd);
    memcpy(nd, d, sizeof(rhrd_t));
  }
  return rd;
}

#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)


2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
# File 'ext/date_ext/date_ext.c', line 2129

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)


2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
# File 'ext/date_ext/date_ext.c', line 2152

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)


2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
# File 'ext/date_ext/date_ext.c', line 2175

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)


2196
2197
2198
2199
2200
2201
# File 'ext/date_ext/date_ext.c', line 2196

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)


2208
2209
2210
# File 'ext/date_ext/date_ext.c', line 2208

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:



2225
2226
2227
2228
2229
2230
# File 'ext/date_ext/date_ext.c', line 2225

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

#dupDate

Returns a dup of the receiver.

Returns:



2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
# File 'ext/date_ext/date_ext.c', line 2237

static VALUE rhrd_dup(VALUE self) {
  rhrd_t *d, *nd;
  VALUE rd = rb_call_super(0, NULL);
  if (!rb_obj_is_kind_of(self, rhrdt_class)) {
    Data_Get_Struct(self, rhrd_t, d);
    Data_Get_Struct(rd, rhrd_t, nd);
    memcpy(nd, d, sizeof(rhrd_t));
  }
  return rd;
}

#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)


2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
# File 'ext/date_ext/date_ext.c', line 2265

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)


3669
3670
3671
# File 'ext/date_ext/date_ext.c', line 3669

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:



2294
2295
2296
# File 'ext/date_ext/date_ext.c', line 2294

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)


2304
2305
2306
# File 'ext/date_ext/date_ext.c', line 2304

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)


2314
2315
2316
2317
2318
2319
2320
# File 'ext/date_ext/date_ext.c', line 2314

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"


3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
# File 'ext/date_ext/date_ext.c', line 3229

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, %02d %s %04ld 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)


2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
# File 'ext/date_ext/date_ext.c', line 2331

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 %04ld-%02d-%02d>",
        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)


2356
2357
2358
2359
2360
2361
# File 'ext/date_ext/date_ext.c', line 2356

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"


3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
# File 'ext/date_ext/date_ext.c', line 3259

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, "%04ld-%02d-%02d", 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%02ld.%02d.%02d", 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)


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

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)


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

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)


2400
2401
2402
2403
2404
2405
# File 'ext/date_ext/date_ext.c', line 2400

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)


2415
2416
2417
2418
2419
2420
# File 'ext/date_ext/date_ext.c', line 2415

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)


3629
3630
3631
# File 'ext/date_ext/date_ext.c', line 3629

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)


2430
2431
2432
2433
2434
2435
# File 'ext/date_ext/date_ext.c', line 2430

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:



2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
# File 'ext/date_ext/date_ext.c', line 2454

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: %d 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:



2445
2446
2447
# File 'ext/date_ext/date_ext.c', line 2445

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>


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

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: %d 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>


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

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: %d 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>


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

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: %d 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>


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

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: %d 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>


3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
# File 'ext/date_ext/date_ext.c', line 3427

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: %d 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>


3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
# File 'ext/date_ext/date_ext.c', line 3457

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: %d 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"


3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
# File 'ext/date_ext/date_ext.c', line 3484

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, %d %s %04ld 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"


3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
# File 'ext/date_ext/date_ext.c', line 3514

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, "%04ld-%02d-%02dT00: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)


3679
3680
3681
# File 'ext/date_ext/date_ext.c', line 3679

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)


2475
2476
2477
# File 'ext/date_ext/date_ext.c', line 2475

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:



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
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
# File 'ext/date_ext/date_ext.c', line 2500

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);
      if (step == 0) {
        rb_raise(rb_eArgError, "step can't be 0");
      }
      break;
    default:
      rb_raise(rb_eArgError, "wrong number of arguments: %d 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)


2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
# File 'ext/date_ext/date_ext.c', line 2633

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: %d 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)


3619
3620
3621
# File 'ext/date_ext/date_ext.c', line 3619

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)


3659
3660
3661
# File 'ext/date_ext/date_ext.c', line 3659

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>


3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
# File 'ext/date_ext/date_ext.c', line 3539

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)


2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
# File 'ext/date_ext/date_ext.c', line 2669

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, "%04ld-%02d-%02d",
        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


3569
3570
3571
3572
3573
3574
# File 'ext/date_ext/date_ext.c', line 3569

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)


3639
3640
3641
# File 'ext/date_ext/date_ext.c', line 3639

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:



2699
2700
2701
2702
2703
# File 'ext/date_ext/date_ext.c', line 2699

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)


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

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)


3649
3650
3651
# File 'ext/date_ext/date_ext.c', line 3649

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)


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

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)


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

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