Skip to Main Content
Ruby. Receptury
book

Ruby. Receptury

by Lucas Carlson, Leonard Richardson
November 2012
Intermediate to advanced content levelIntermediate to advanced
888 pages
54h 46m
Polish
Helion
Content preview from Ruby. Receptury

3.5. Arytmetyka dat

Problem

Należy obliczyć dystans dzielący dwie daty bądź wyznaczyć datę odległą o ustalony dystans — w przód lub wstecz — od pewnej daty odniesienia.

Rozwiązanie

Ponieważ obiekty klas Time, DateTime i Date reprezentowane są w postaci liczb, można do nich dodawać i odejmować od nich liczby. W przypadku obiektów Time powoduje to dodanie (odjęcie) odpowiedniej liczby sekund, w przypadku dwóch pozostałych klas — odpowiedniej liczby dni.

require 'date' y2k = Time.gm(2000, 1, 1) # => Sat Jan 01 00:00:00 UTC 2000 y2k + 1 # => Sat Jan 01 00:00:01 UTC 2000 y2k - 1 # => Fri Dec 31 23:59:59 UTC 1999 y2k + (60 * 60 * 24 * 365) # => Sun Dec 31 00:00:00 UTC 2000 y2k_dt = DateTime.new(2000, 1, 1) (y2k_dt + 1).to_s # => "2000-01-02T00:00:00Z" ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

ReasonML Quick Start Guide

ReasonML Quick Start Guide

Raphael Rafatpanah, Bruno Joseph D'mello
Microsoft Excel: Formeln & Funktionen - Das Maxibuch, 3., aktualisierte und erweiterte Auflage

Microsoft Excel: Formeln & Funktionen - Das Maxibuch, 3., aktualisierte und erweiterte Auflage

Egbert Jeschke, Eckehard Pfeifer, Helmut Reinke, Sara Unverhau, Bodo Fienitz
Mastering Ruby Closures

Mastering Ruby Closures

Benjamin Tan Wei Hao

Publisher Resources

ISBN: 9781457177453Errata Page