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



2752
2753
2754
# File 'ext/date_ext/date_ext.c', line 2752

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)


2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
# File 'ext/date_ext/date_ext.c', line 2783

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:



2736
2737
2738
# File 'ext/date_ext/date_ext.c', line 2736

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)


2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
# File 'ext/date_ext/date_ext.c', line 2861

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)


2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
# File 'ext/date_ext/date_ext.c', line 2821

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:



2720
2721
2722
# File 'ext/date_ext/date_ext.c', line 2720

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)


2055
2056
2057
2058
2059
2060
# File 'ext/date_ext/date_ext.c', line 2055

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)


2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
# File 'ext/date_ext/date_ext.c', line 2070

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)


2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
# File 'ext/date_ext/date_ext.c', line 2101

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)


2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
# File 'ext/date_ext/date_ext.c', line 2124

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)


2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
# File 'ext/date_ext/date_ext.c', line 2147

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)


2168
2169
2170
2171
2172
2173
# File 'ext/date_ext/date_ext.c', line 2168

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)


2180
2181
2182
# File 'ext/date_ext/date_ext.c', line 2180

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:



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

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)


2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
# File 'ext/date_ext/date_ext.c', line 2221

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)


3583
3584
3585
# File 'ext/date_ext/date_ext.c', line 3583

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:



2250
2251
2252
# File 'ext/date_ext/date_ext.c', line 2250

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)


2260
2261
2262
# File 'ext/date_ext/date_ext.c', line 2260

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)


2270
2271
2272
2273
2274
2275
2276
# File 'ext/date_ext/date_ext.c', line 2270

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"


3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
# File 'ext/date_ext/date_ext.c', line 3143

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)


2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
# File 'ext/date_ext/date_ext.c', line 2287

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)


2312
2313
2314
2315
2316
2317
# File 'ext/date_ext/date_ext.c', line 2312

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"


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
3203
3204
3205
3206
3207
# File 'ext/date_ext/date_ext.c', line 3173

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)


2325
2326
2327
# File 'ext/date_ext/date_ext.c', line 2325

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)


2338
2339
2340
2341
2342
2343
# File 'ext/date_ext/date_ext.c', line 2338

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)


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

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)


2371
2372
2373
2374
2375
2376
# File 'ext/date_ext/date_ext.c', line 2371

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)


3543
3544
3545
# File 'ext/date_ext/date_ext.c', line 3543

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)


2386
2387
2388
2389
2390
2391
# File 'ext/date_ext/date_ext.c', line 2386

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:



2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
# File 'ext/date_ext/date_ext.c', line 2410

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:



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

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>


3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
# File 'ext/date_ext/date_ext.c', line 3221

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>


3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
# File 'ext/date_ext/date_ext.c', line 3251

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>


3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
# File 'ext/date_ext/date_ext.c', line 3281

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>


3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
# File 'ext/date_ext/date_ext.c', line 3311

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>


3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
# File 'ext/date_ext/date_ext.c', line 3341

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>


3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
# File 'ext/date_ext/date_ext.c', line 3371

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"


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

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"


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

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)


3593
3594
3595
# File 'ext/date_ext/date_ext.c', line 3593

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)


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

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:



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
2526
2527
2528
2529
2530
# File 'ext/date_ext/date_ext.c', line 2456

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)


2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
# File 'ext/date_ext/date_ext.c', line 2586

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)


3533
3534
3535
# File 'ext/date_ext/date_ext.c', line 3533

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)


3573
3574
3575
# File 'ext/date_ext/date_ext.c', line 3573

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>


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

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)


2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
# File 'ext/date_ext/date_ext.c', line 2622

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


3483
3484
3485
3486
3487
3488
# File 'ext/date_ext/date_ext.c', line 3483

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)


3553
3554
3555
# File 'ext/date_ext/date_ext.c', line 3553

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:



2652
2653
2654
2655
2656
# File 'ext/date_ext/date_ext.c', line 2652

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)


2667
2668
2669
2670
2671
2672
# File 'ext/date_ext/date_ext.c', line 2667

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)


3563
3564
3565
# File 'ext/date_ext/date_ext.c', line 3563

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)


2684
2685
2686
2687
2688
2689
# File 'ext/date_ext/date_ext.c', line 2684

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)


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

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