Giter VIP home page Giter VIP logo

date's Introduction

Date

A subclass of Object that includes the Comparable module and easily handles date.

Installation

Add this line to your application's Gemfile:

gem 'date'

And then execute:

$ bundle

Or install it yourself as:

$ gem install date

Usage

require 'date'

A Date object is created with Date::new, Date::jd, Date::ordinal, Date::commercial, Date::parse, Date::strptime, Date::today, Time#to_date, etc.

require 'date'

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

All Date objects are immutable; hence cannot modify themselves.

The concept of a date object can be represented as a tuple of the day count, the offset and the day of calendar reform.

The day count denotes the absolute position of a temporal dimension. The offset is relative adjustment, which determines decoded local time with the day count. The day of calendar reform denotes the start day of the new style. The old style of the West is the Julian calendar which was adopted by Caesar. The new style is the Gregorian calendar, which is the current civil calendar of many countries.

The day count is virtually the astronomical Julian day number. The offset in this class is usually zero, and cannot be specified directly.

A Date object can be created with an optional argument, the day of calendar reform as a Julian day number, which should be 2298874 to 2426355 or negative/positive infinity. The default value is Date::ITALY (2299161=1582-10-15). See also sample/cal.rb.

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

A Date object has various methods. See each reference.

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

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/date.

License

The gem is available as open source under the terms of the 2-Clause BSD License.

date's People

Contributors

akr avatar amatsuda avatar ayumin avatar burdettelamar avatar byroot avatar dependabot[bot] avatar drbrain avatar glaszig avatar hsbt avatar jeremyevans avatar k0kubun avatar mame avatar marcandre avatar matzbot avatar mrkn avatar msnexploder avatar nagachika avatar nobu avatar nurse avatar olleolleolle avatar p0pemaru avatar shyouhei avatar sonots avatar stomar avatar tricknotes avatar unak avatar yugui avatar yui-knk avatar znz avatar zverok avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

date's Issues

JRuby support

JRuby provides its own extension for the date library, and this will need to be incorporated into the released gem. We will do the work on this.

DateTime._strptime("...", "%z") behaves differently from Ruby 3.2

They may be ultimate cases, so may not be to fix. But reporting just in case.

require 'date'
puts DateTime._strptime("UTC+99:13", "%z")
puts DateTime._strptime("UTC+99:13:14", "%z")
puts DateTime._strptime("UTC+912:42", "%z")
puts DateTime._strptime("UTC+912:42:49", "%z")
puts DateTime._strptime("UTC-99:13", "%z")
puts DateTime._strptime("UTC-99:13:14", "%z")
puts DateTime._strptime("UTC-912:42", "%z")
puts DateTime._strptime("UTC-912:42:49", "%z")

Until Ruby 3.1 (tested with 2.7.7, 3.0.5, 3.1.3)

{:zone=>"UTC+99:13", :offset=>357180}
{:zone=>"UTC+99:13:14", :offset=>357194}
{:zone=>"UTC+912:42", :offset=>3285720}
{:zone=>"UTC+912:42:49", :offset=>3285769}
{:zone=>"UTC-99:13", :offset=>-357180}
{:zone=>"UTC-99:13:14", :offset=>-357194}
{:zone=>"UTC-912:42", :offset=>-3285720}
{:zone=>"UTC-912:42:49", :offset=>-3285769}

From Ruby 3.2 (tested with Ruby 3.2.1)

{:zone=>"UTC+99:13", :offset=>nil}
{:zone=>"UTC+99:13:14", :offset=>nil}
{:zone=>"UTC+912:42", :offset=>nil}
{:zone=>"UTC+912:42:49", :offset=>nil}
{:zone=>"UTC-99:13", :offset=>nil}
{:zone=>"UTC-99:13:14", :offset=>nil}
{:zone=>"UTC-912:42", :offset=>nil}
{:zone=>"UTC-912:42:49", :offset=>nil}

Strange behavior with invalid string date

ruby version: 2.6.5
Code:

$ irb
require 'date'
puts  Date.parse('-1111-2-2').year # => -1111

expected behavior: ruby throws ArgumentError due to invalid string passed
real behavior: negative year returns.

Is it ok?

Date.commercial method does not account for leap year

The Date.commercial method does NOT account for a leap year.

irb(main):099:0> d = Date.parse('2021-01-03')
=> #<Date: 2021-01-03 ((2459218j,0s,0n),+0s,2299161j)>
irb(main):100:0> 
irb(main):101:0> d.year
=> 2021
irb(main):102:0> d.cweek
=> 53
irb(main):103:0> 
irb(main):104:0> 
irb(main):105:0> 
irb(main):106:0> Date.commercial(d.year,d.cweek)
Traceback (most recent call last):
        3: from /usr/bin/irb:11:in `<main>'
        2: from (irb):106
        1: from (irb):106:in `commercial'
ArgumentError (invalid date)
irb(main):107:0>

DateTime object not comparable with ActiveSupport::TimeWithZone?

This expectation started to fail after I installed this gem v.3.3.3

# order.purchased_at = Sun, 26 Jan 0020 00:00:00 UTC +00:00
# order.purchased_at.class == ActiveSupport::TimeWithZone
expect(order.purchased_at).to eq 'Sun, 26 Jan 0020 00:00:00 UTC +00:00'.to_datetime

If I remove this gem the expectation passes

Incorrect version in CVE-2021-41817 advisory

On GHSA-qg54-694p-wgpp, it says:

Affected versions
>= 3.2.0, < 3.2.1
>= 3.1.0, < 3.1.2
>= 3.0.0, < 3.0.2
< 2.0.1
Patched versions
3.2.1
3.1.2
3.0.2
2.01

But obviously 2.01 is not a correct version, it should 2.0.1.

And the suggested remediation is then also buggy:

Remediation

Upgrade date to version 2.01 or later. For example:

gem "date", ">= 2.01"

Date#jd and Datetime#jd provide incorrect result

using ruby 2.7.0

Expected:

Date.jd 2459014.5
#<Date: 2020-06-14 ((2459014j,0s,0n),+0s,2299161j)>

DateTime.jd 2459014.5
=> #<DateTime: 2020-06-14T00:00:00+00:00 ((2459014j,0s,0n),+0s,2299161j)>

Actual:

Date.jd 2459014.5
=> #<Date: 2020-06-13 ((2459014j,43200s,0n),+0s,2299161j)>

DateTime.jd 2459014.5
=> #<DateTime: 2020-06-13T12:00:00+00:00 ((2459014j,43200s,0n),+0s,2299161j)>

Problem seems to be that Julian dates should start from noon UTC, which is not the current behaviour.

Behaviour change when comparing `Date` and `Time` between previous versions and 3.3.0

Hi there,

I'm not sure this is the right place to put such issue, but I found an issue after updating from 3.0.1 to 3.3.3.

I tracked the change in behaviour to start appearing between versions 3.2.2 and 3.3.0, here's and example of the change:

with version 3.2.2

require "time" 
require "date"

Time.parse("1000-01-01T00:00:00Z")
# date v3.2.2 => "1000-01-01T00:00:00.000Z"

Time.parse("1000-01-01T00:00:00Z").to_date
# date v3.2.2 => Wed, 27 Dec 0999

Date.parse("1000-01-01") == Time.parse("1000-01-01T00:00:00Z")
# date v3.2.2 => true

Time.parse("1000-01-01T00:00:00Z") == Date.parse("1000-01-01")
# date v3.2.2 => true

with version 3.3.0

require "time" 
require "date"

Time.parse("1000-01-01T00:00:00Z")
# date v3.3.3 => "1000-01-01T00:00:00.000Z"

Time.parse("1000-01-01T00:00:00Z").to_date
# date v3.3.0 => Wed, 27 Dec 0999

Date.parse("1000-01-01") == Time.parse("1000-01-01T00:00:00Z")
# date v3.3.3 => false

Time.parse("1000-01-01T00:00:00Z") == Date.parse("1000-01-01")
# date v3.3.3 => false

As you can see I'm working with date in the Julian calendar and comparing them used to work before 3.3.0.

Now I'm not sure if and what is the bug there, as this was maybe already an issue:

Time.parse("1000-01-01T00:00:00Z").to_date
# => Wed, 27 Dec 0999

But in the end, this change of behaviour surfaced and I wanted to be sure it's reported.

cannot load complex into simple in ext/date/date_core.c - Marshal.load(hexdump)

after an upgrade from Ruby 2.5 to Ruby 2.7.1 we are confronted with the following problem:

  def test_marshall
    hexdump = "0408553a09446174655b0b690069006902c0a8553a0d526174696f6e616c5b076c2b0800001a71180269029dff6900660c32323939313631"
    ODBA::Marshal.load(hexdump)
    @group.instance_eval('@logs = {Time.local(2011,2,3) => "value"}')
    assert_equal([2011], @group.years)
  end

Looking at the source code of ruby 2.7.1 I find the string cannot load complex into simple in ext/date/date_core.c. Therefore I think it a problem when restoring a date object.

Question: What exactly changed between Ruby 2.5.0 and 2.7.1 in regards to Marshal.load(hexdump)?

There seems to be subtle change in the marshaling/unmarshalling of the date format between Ruby 2.5.0 and 2.7.1

Loading logs with a marshalled dump done with Ruby 2.5.0 will not load with Ruby 2.7.1. ;(

New release

the last release for this gem was in 2017, when ruby 2.5 was released. Since then, a few changes have been made in 2018 and released in 2.6. But there has not been a new release of the gem.

It would be ๐Ÿ’ฏ if a new release can be organised to release the new changes.

[BUG] Compiler Error 'RUBY_TYPED_FROZEN_SHAREABLE' undeclared here

Hello,
currently i cant update some gems, because i get this error:

date_core.c:2980:57: error: 'RUBY_TYPED_FROZEN_SHAREABLE' undeclared here (not in a function)
2980 | RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED|RUBY_TYPED_FROZEN_SHAREABLE,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
make: *** [Makefile:245: date_core.o] Error 1

I update as user, not as root.
Complete output from "LANG=C gem update --debug --verbose --backtrace 2>&1 | tee output.txt"
output.txt

[Feature] Add Kernel#Date when require 'date'

Reference to: https://bugs.ruby-lang.org/issues/17022

What if we have a method Kernel#Date that delegates to Date.parse or Date.iso8601 ?

Dates could be written more naturally, right?

Date("2020-01-13") - Date("2019-01-13")

Capitalized methods are defined on the Kernel module to facilitate construction of basic types:

Kernel.methods.grep(/^[A-Z].*/)
#=> [:Rational, :Complex, :URI, :Integer, :Float, :String, :Array, :Hash, :Pathname]

The method BigDecimal is added when the corresponding library is required:

require 'bigdecimal'
Kernel.methods.grep(/^[A-Z].*/)
#=> [:Rational, :Complex, :BigDecimal, :URI, :Integer, :Float, :String, :Array, :Hash, :Pathname]

The proposal is to do the same for Date, when we do require 'date', a method Kernel#Date would be added.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.