Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyanny/3361843 to your computer and use it in GitHub Desktop.
Save kyanny/3361843 to your computer and use it in GitHub Desktop.
irb(main):001:0> post = Post.create
(0.1ms) begin transaction
SQL (22.0ms) INSERT INTO "posts" ("body", "created_at", "title", "updated_at") VALUES (?, ?, ?, ?) [["body", nil], ["created_at", Thu, 16 Aug 2012 02:42:10 JST +09:00], ["title", nil], ["updated_at", Thu, 16 Aug 2012 02:42:10 JST +09:00]]
(2.4ms) commit transaction
=> #<Post id: 3, title: nil, body: nil, created_at: "2012-08-15 17:42:10", updated_at: "2012-08-15 17:42:10">
irb(main):002:0> post
=> #<Post id: 3, title: nil, body: nil, created_at: "2012-08-15 17:42:10", updated_at: "2012-08-15 17:42:10">
irb(main):003:0> post.created_at
=> Thu, 16 Aug 2012 02:42:10 JST +09:00
# sqlite> select * from posts where id = 3;
# id = 3
# title =
# body =
# created_at = 2012-08-16 02:42:10.358138
# updated_at = 2012-08-16 02:42:10.358138
irb(main):001:0> post = Post.create
(0.1ms) begin transaction
SQL (23.9ms) INSERT INTO "posts" ("body", "created_at", "title", "updated_at") VALUES (?, ?, ?, ?) [["body", nil], ["created_at", Thu, 16 Aug 2012 02:38:10 JST +09:00], ["title", nil], ["updated_at", Thu, 16 Aug 2012 02:38:10 JST +09:00]]
(1.1ms) commit transaction
=> #<Post id: 2, title: nil, body: nil, created_at: "2012-08-15 17:38:10", updated_at: "2012-08-15 17:38:10">
irb(main):002:0> post
=> #<Post id: 2, title: nil, body: nil, created_at: "2012-08-15 17:38:10", updated_at: "2012-08-15 17:38:10">
irb(main):003:0> post.created_at
=> Thu, 16 Aug 2012 02:38:10 JST +09:00
# sqlite> select * from posts where id = 2;
# id = 2
# title =
# body =
# created_at = 2012-08-15 17:38:10.206518
# updated_at = 2012-08-15 17:38:10.206518
irb(main):001:0> post = Post.create
(0.1ms) begin transaction
SQL (26.1ms) INSERT INTO "posts" ("body", "created_at", "title", "updated_at") VALUES (?, ?, ?, ?) [["body", nil], ["created_at", Wed, 15 Aug 2012 17:35:06 UTC +00:00], ["title", nil], ["updated_at", Wed, 15 Aug 2012 17:35:06 UTC +00:00]]
(1.3ms) commit transaction
=> #<Post id: 1, title: nil, body: nil, created_at: "2012-08-15 17:35:06", updated_at: "2012-08-15 17:35:06">
irb(main):002:0> post
=> #<Post id: 1, title: nil, body: nil, created_at: "2012-08-15 17:35:06", updated_at: "2012-08-15 17:35:06">
irb(main):003:0> post.created_at
=> Wed, 15 Aug 2012 17:35:06 UTC +00:00
# sqlite> select * from posts where id = 1;
# id = 1
# title =
# body =
# created_at = 2012-08-15 17:35:06.143798
# updated_at = 2012-08-15 17:35:06.143798
irb(main):001:0> Post.where(created_at: Time.now.beginning_of_day..Time.now.end_of_day).count
(0.2ms) SELECT COUNT(*) FROM "posts" WHERE ("posts"."created_at" BETWEEN '2012-08-15 15:00:00.000000' AND '2012-08-16 14:59:59.999999')
=> 5
irb(main):002:0> Post.where('strftime("%Y-%m-%d", created_at) = ?', Date.today).count
(0.3ms) SELECT COUNT(*) FROM "posts" WHERE (strftime("%Y-%m-%d", created_at) = '2012-08-16')
=> 1
irb(main):025:0> post = Post.create(publish_date: Date.today)
(0.1ms) begin transaction
SQL (0.7ms) INSERT INTO "posts" ("body", "created_at", "publish_date", "title", "updated_at") VALUES (?, ?, ?, ?, ?) [["body", nil], ["created_at", Thu, 16 Aug 2012 03:00:29 JST +09:00], ["publish_date", Thu, 16 Aug 2012], ["title", nil], ["updated_at", Thu, 16 Aug 2012 03:00:29 JST +09:00]]
(2.0ms) commit transaction
=> #<Post id: 6, title: nil, body: nil, created_at: "2012-08-15 18:00:29", updated_at: "2012-08-15 18:00:29", publish_date: "2012-08-16">
irb(main):026:0> post
=> #<Post id: 6, title: nil, body: nil, created_at: "2012-08-15 18:00:29", updated_at: "2012-08-15 18:00:29", publish_date: "2012-08-16">
irb(main):027:0> post.publish_date
=> Thu, 16 Aug 2012
irb(main):028:0> post.reload
Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 6]]
=> #<Post id: 6, title: nil, body: nil, created_at: "2012-08-15 18:00:29", updated_at: "2012-08-15 18:00:29", publish_date: "2012-08-16">
irb(main):029:0> post.publish_date
=> Thu, 16 Aug 2012
irb(main):032:0> post = Post.create(publish_date: Time.now.beginning_of_day)
(0.1ms) begin transaction
SQL (1.6ms) INSERT INTO "posts" ("body", "created_at", "publish_date", "title", "updated_at") VALUES (?, ?, ?, ?, ?) [["body", nil], ["created_at", Thu, 16 Aug 2012 03:01:15 JST +09:00], ["publish_date", 2012-08-16 00:00:00 +0900], ["title", nil], ["updated_at", Thu, 16 Aug 2012 03:01:15 JST +09:00]]
(2.1ms) commit transaction
=> #<Post id: 7, title: nil, body: nil, created_at: "2012-08-15 18:01:15", updated_at: "2012-08-15 18:01:15", publish_date: "2012-08-16 00:00:00">
irb(main):033:0> post
=> #<Post id: 7, title: nil, body: nil, created_at: "2012-08-15 18:01:15", updated_at: "2012-08-15 18:01:15", publish_date: "2012-08-16 00:00:00">
irb(main):034:0> post.publish_date
=> 2012-08-16 00:00:00 +0900
irb(main):035:0> post.reload
Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 7]]
=> #<Post id: 7, title: nil, body: nil, created_at: "2012-08-15 18:01:15", updated_at: "2012-08-15 18:01:15", publish_date: "2012-08-15">
irb(main):036:0> post.publish_date
=> Wed, 15 Aug 2012
# <tt>:db</tt> format outputs time in UTC; all others output time in local.
# Uses TimeWithZone's +strftime+, so <tt>%Z</tt> and <tt>%z</tt> work correctly.
def to_s(format = :default)
if format == :db
utc.to_s(format)
elsif formatter = ::Time::DATE_FORMATS[format]
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
else
"#{time.strftime("%Y-%m-%d %H:%M:%S")} #{formatted_offset(false, 'UTC')}" # mimicking Ruby 1.9 Time#to_s format
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment