Ruby: include/ruby/internal/intern/time.h File Reference

Ruby 4.0.6p0 (2026-07-14 revision 03b6d3f8898a28604fe6cb00eae3226b821168f4)

Functions
time.h File Reference

Public APIs related to rb_cTime. More...

`#include "ruby/internal/config.h"`
`#include "ruby/internal/attr/nonnull.h"`
`#include "ruby/internal/dllexport.h"`
`#include "ruby/internal/value.h"`

Go to the source code of this file.

## Functions
void | rb_timespec_now (struct timespec *ts)
| Fills the current time into the given struct.
VALUE | rb_time_new (time_t sec, long usec)
| Creates an instance of rb_cTime with the given time and the local timezone.
VALUE | rb_time_nano_new (time_t sec, long nsec)
| Identical to rb_time_new(), except it accepts the time in nanoseconds resolution.
VALUE | rb_time_timespec_new (const struct timespec *ts, int offset)
| Creates an instance of rb_cTime, with given time and offset.
VALUE | rb_time_num_new (VALUE timev, VALUE off)
| Identical to rb_time_timespec_new(), except it takes Ruby values instead of C structs.
struct timeval | rb_time_interval (VALUE num)
| Creates a "time interval".
struct timeval | rb_time_timeval (VALUE time)
| Converts an instance of rb_cTime to a struct timeval that represents the identical point of time.
struct timespec | rb_time_timespec (VALUE time)
| Identical to rb_time_timeval(), except for return type.
struct timespec | rb_time_timespec_interval (VALUE num)
| Identical to rb_time_interval(), except for return type.
VALUE | rb_time_utc_offset (VALUE time)
| Queries the offset, in seconds between the time zone of the time and the UTC.

## Detailed Description

Public APIs related to rb_cTime.

Author
  Ruby developers ruby-.nosp@m.core.nosp@m.@ruby.nosp@m.-lan.nosp@m.g.org

Copyright
  This file is a part of the programming language Ruby. Permission is hereby granted, to either redistribute and/or modify this file, provided that the conditions mentioned in the file COPYING are met. Consult the file for details.

Warning
  Symbols prefixed with either RBIMPL or rbimpl are implementation details. Don't take them as canon. They could rapidly appear then vanish. The name (path) of this header file is also an implementation detail. Do not expect it to persist at the place it is now. Developers are free to move it anywhere anytime at will.

Note
  To ruby-core: remember that this header can be possibly recursively included from extension libraries written in C++. Do not expect for instance __VA_ARGS__ is always available. We assume C99 for ruby itself but we don't assume languages of extension libraries. They could be written in C++98.

Definition in file time.h.

## Function Documentation

## ◆ rb_time_interval()

struct timeval rb_time_interval | ( | VALUE | num | ) |

Creates a "time interval".

This basically converts an instance of rb_cNumeric into a struct timeval, but for instance negative time interval must not exist.

Parameters

  [in] | num | An instance of rb_cNumeric.

Exceptions

  rb_eArgError | `num` is negative.
  rb_eRangeError | `num` is out of range of `timeval::tv_sec`.

Returns
  A struct that represents the identical time to num.

Definition at line 2949 of file time.c.

Referenced by rb_io_set_timeout(), rb_io_wait(), and rb_time_interval().

## ◆ rb_time_nano_new()

VALUE rb_time_nano_new | ( | time_t | sec,
|  | long | nsec )

Identical to rb_time_new(), except it accepts the time in nanoseconds resolution.

Parameters

  [in] | sec | Seconds since the UNIX epoch.
  [in] | nsec | Subsecond part, in nanoseconds resolution.

Exceptions

  rb_eRangeError | Cannot express the time.

Returns
  An allocated instance of rb_cTime.

Definition at line 2801 of file time.c.

Referenced by rb_time_nano_new(), and rb_time_timespec_new().

## ◆ rb_time_new()

VALUE rb_time_new | ( | time_t | sec,
|  | long | usec )

Creates an instance of rb_cTime with the given time and the local timezone.

Parameters

  [in] | sec | Seconds since the UNIX epoch.
  [in] | usec | Subsecond part, in microseconds resolution.

Exceptions

  rb_eRangeError | Cannot express the time.

Returns
  An allocated instance of rb_cTime.

Definition at line 2792 of file time.c.

Referenced by rb_time_new().

## ◆ rb_time_num_new()

VALUE rb_time_num_new | ( | VALUE | timev,
|  | VALUE | off )

Identical to rb_time_timespec_new(), except it takes Ruby values instead of C structs.

Parameters

  [in] | timev | Something numeric. Currently Integers, Rationals, and Floats are accepted.
  [in] | off | Offset specifier. As of 2.7 this argument is heavily extended to take following kinds of objects: RUBY_Qundef ... means UTC. rb_cString ... "+12:34" etc. A mysterious "zone" object. This is largely undocumented. However the initial intent was that we want to accept ActiveSupport::TimeZone here. Other gems could also be possible... But how to make an acceptable class is beyond this document.

Exceptions

  rb_eArgError | Malformed `off`.

Returns
  An allocated instance of rb_cTime.

Definition at line 2832 of file time.c.

Referenced by rb_time_num_new().

## ◆ rb_time_timespec()

struct timespec rb_time_timespec | ( | VALUE | time | ) |

Identical to rb_time_timeval(), except for return type.

Parameters

  [in] | time | Instance of either rb_cTime or rb_cNumeric.

Exceptions

  rb_eRangeError | `time` is out of range of `timeval::tv_sec`.

Returns
  A struct that represents the identical time to num.

Definition at line 2972 of file time.c.

Referenced by rb_time_timespec().

## ◆ rb_time_timespec_interval()

struct timespec rb_time_timespec_interval | ( | VALUE | num | ) |

Identical to rb_time_interval(), except for return type.

Parameters

  [in] | num | An instance of rb_cNumeric.

Exceptions

  rb_eArgError | `num` is negative.
  rb_eRangeError | `num` is out of range of `timespec::tv_sec`.

Returns
  A struct that represents the identical time to num.

Definition at line 2986 of file time.c.

Referenced by rb_time_timespec_interval().

## ◆ rb_time_timespec_new()

VALUE rb_time_timespec_new | ( | const struct timespec * | ts,
|  | int | offset )

Creates an instance of rb_cTime, with given time and offset.

Parameters

  [in] | ts | Time specifier.
  [in] | offset | Offset specifier, can take following values: INT_MAX: ts is in local time. INT_MAX - 1: ts is in UTC. -86400 to 86400: fixed timezone.

Exceptions

  rb_eArgError | Malformed `offset`.

Returns
  An allocated instance of rb_cTime.

Definition at line 2809 of file time.c.

Referenced by rb_time_timespec_new().

## ◆ rb_time_timeval()

struct timeval rb_time_timeval | ( | VALUE | time | ) |

Converts an instance of rb_cTime to a struct timeval that represents the identical point of time.

It can also take something numeric; would consider it as a UNIX time then.

Parameters

  [in] | time | Instance of either rb_cTime or rb_cNumeric.

Exceptions

  rb_eRangeError | `time` is out of range of `timeval::tv_sec`.

Returns
  A struct that represents the identical time to num.

Definition at line 2955 of file time.c.

Referenced by rb_thread_sleep(), and rb_time_timeval().

## ◆ rb_time_utc_offset()

VALUE rb_time_utc_offset | ( | VALUE | time | ) |

Queries the offset, in seconds between the time zone of the time and the UTC.

Parameters

  [in] | time | An instance of rb_cTime.

Returns
  Numeric offset.

Definition at line 5079 of file time.c.

Referenced by rb_time_utc_offset().

## ◆ rb_timespec_now()

void rb_timespec_now | ( | struct timespec * | ts | ) |

Fills the current time into the given struct.

Parameters

  [out] | ts | Return buffer.

Exceptions

  rb_eSystemCallError | Access denied for hardware clock.

Postcondition
  Current time is stored in *ts.

Definition at line 2003 of file time.c.