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.2. Dekodowanie daty, dokładne i przybliżone

Problem

Mając dany łańcuch reprezentujący datę lub datę i czas, chcemy przekształcić go na równoważny obiekt Date. Konwencja (format) zapisu łańcucha nie jest znana a priori.

Rozwiązanie

Najlepiej wykorzystać do tego celu metodę Date.parse lub DateTime.parse. Metody te stosują zaawansowane podejście heurystyczne w celu „odgadnięcia” formatu, w jakim zapisano łańcuch wejściowy, i podejście to bardzo często daje zadowalające rezultaty:

require 'date' Date.parse('2/9/2007').to_s # => "2007-02-09" DateTime.parse('02-09-2007 12:30:44 AM').to_s # => "2007-09-02T00:30:44Z" DateTime.parse('02-09-2007 12:30:44 PM EST').to_s # => "2007-09-02T12:30:44-0500" Date.parse('Wednesday, January 10, 2001').to_s ...
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