You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ruby(Sinatra框架)时间格式转换求助:将ISO8601时间转为Unix时间戳

Fixing Unix Timestamp Conversion in Sinatra (Ruby)

Hey there! Let's break down why you're getting nil and get that time conversion working properly.

First, let's cover the most likely causes and solutions step by step:

1. Check if created_at actually has a valid value

The most common reason your code returns nil is that the created_at variable itself is nil (or an empty string). If you pass nil to Time.parse, it returns nil, and any subsequent method calls (like in_time_zone) will also return nil.

To debug this, add a quick check to see what created_at holds:

puts "Debug: created_at value is #{created_at.inspect}"

If this outputs nil or an unexpected string, you'll need to fix where created_at is coming from (e.g., ensure your database or API returns the correct time string).

2. Make sure you have ActiveSupport loaded

Methods like in_time_zone and beginning_of_day aren't part of Ruby's standard library—they come from ActiveSupport (the utility library that powers Rails). Sinatra doesn't include this by default, so you need to add it to your project:

  1. Add the gem to your Gemfile:
gem 'activesupport'
  1. Run bundle install
  2. Require the time-specific extensions in your Sinatra app:
require 'active_support/time'

(You can also use require 'active_support/all' if you want all ActiveSupport features, but loading only what you need is more efficient.)

3. Use Time.iso8601 instead of Time.parse for reliability

Your created_at format (2021-06-25T00:00:00.000Z) is a standard ISO 8601 timestamp. While Time.parse can handle this, Time.iso8601 is purpose-built for this format and less likely to have parsing issues with edge cases.

Working Example Code

Here's a refined version of your code with checks and error handling:

require 'sinatra'
require 'active_support/time'

# Example route to test conversion
get '/convert-timestamp' do
  # Replace this with where your created_at value comes from (DB, params, etc.)
  created_at = "2021-06-25T00:00:00.000Z"

  if created_at.blank?
    return "Error: No valid created_at value provided"
  end

  begin
    # Parse the ISO time, convert to Jakarta timezone, get start of day, then timestamp
    unix_timestamp = Time.iso8601(created_at).in_time_zone('Asia/Jakarta').beginning_of_day.to_i
    "Converted Unix Timestamp: #{unix_timestamp}"
  rescue ArgumentError => e
    "Error parsing time: #{e.message}"
  end
end

For your example created_at value (2021-06-25T00:00:00.000Z), this should return 1624569600 (the Unix timestamp for June 25, 2021 00:00:00 in Asia/Jakarta).

Quick Notes

  • Always validate that created_at exists before parsing it to avoid nil errors.
  • Using begin-rescue catches invalid time formats and prevents your app from crashing.
  • If you don't want to use ActiveSupport, you can calculate the Jakarta timezone offset manually, but that's more error-prone (timezones have daylight saving changes, which ActiveSupport handles automatically).

内容的提问来源于stack exchange,提问作者Ramadhani Baharzah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 14:27:48