| 1 |
#ifndef RBIMPL_INTERN_TIME_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_INTERN_TIME_H |
| 3 |
/** |
| 4 |
* @file |
| 5 |
* @author Ruby developers <[email protected]> |
| 6 |
* @copyright This file is a part of the programming language Ruby. |
| 7 |
* Permission is hereby granted, to either redistribute and/or |
| 8 |
* modify this file, provided that the conditions mentioned in the |
| 9 |
* file COPYING are met. Consult the file for details. |
| 10 |
* @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are |
| 11 |
* implementation details. Don't take them as canon. They could |
| 12 |
* rapidly appear then vanish. The name (path) of this header file |
| 13 |
* is also an implementation detail. Do not expect it to persist |
| 14 |
* at the place it is now. Developers are free to move it anywhere |
| 15 |
* anytime at will. |
| 16 |
* @note To ruby-core: remember that this header can be possibly |
| 17 |
* recursively included from extension libraries written in C++. |
| 18 |
* Do not expect for instance `__VA_ARGS__` is always available. |
| 19 |
* We assume C99 for ruby itself but we don't assume languages of |
| 20 |
* extension libraries. They could be written in C++98. |
| 21 |
* @brief Public APIs related to ::rb_cTime. |
| 22 |
*/ |
| 23 |
#include "ruby/internal/config.h" |
| 24 |
|
| 25 |
#ifdef HAVE_TIME_H |
| 26 |
# include <time.h> /* for time_t */ |
| 27 |
#endif |
| 28 |
|
| 29 |
#include "ruby/internal/attr/nonnull.h" |
| 30 |
#include "ruby/internal/dllexport.h" |
| 31 |
#include "ruby/internal/value.h" |
| 32 |
|
| 33 |
RBIMPL_SYMBOL_EXPORT_BEGIN() |
| 34 |
|
| 35 |
struct timespec; |
| 36 |
struct timeval; |
| 37 |
|
| 38 |
/* time.c */ |
| 39 |
|
| 40 |
RBIMPL_ATTR_NONNULL(()) |
| 41 |
/** |
| 42 |
* Fills the current time into the given struct. |
| 43 |
* |
| 44 |
* @param[out] ts Return buffer. |
| 45 |
* @exception rb_eSystemCallError Access denied for hardware clock. |
| 46 |
* @post Current time is stored in `*ts`. |
| 47 |
*/ |
| 48 |
void rb_timespec_now(struct timespec *ts); |
| 49 |
|
| 50 |
/** |
| 51 |
* Creates an instance of ::rb_cTime with the given time and the local |
| 52 |
* timezone. |
| 53 |
* |
| 54 |
* @param[in] sec Seconds since the UNIX epoch. |
| 55 |
* @param[in] usec Subsecond part, in microseconds resolution. |
| 56 |
* @exception rb_eRangeError Cannot express the time. |
| 57 |
* @return An allocated instance of ::rb_cTime. |
| 58 |
*/ |
| 59 |
VALUE rb_time_new(time_t sec, long usec); |
| 60 |
|
| 61 |
/** |
| 62 |
* Identical to rb_time_new(), except it accepts the time in nanoseconds |
| 63 |
* resolution. |
| 64 |
* |
| 65 |
* @param[in] sec Seconds since the UNIX epoch. |
| 66 |
* @param[in] nsec Subsecond part, in nanoseconds resolution. |
| 67 |
* @exception rb_eRangeError Cannot express the time. |
| 68 |
* @return An allocated instance of ::rb_cTime. |
| 69 |
*/ |
| 70 |
VALUE rb_time_nano_new(time_t sec, long nsec); |
| 71 |
|
| 72 |
RBIMPL_ATTR_NONNULL(()) |
| 73 |
/** |
| 74 |
* Creates an instance of ::rb_cTime, with given time and offset. |
| 75 |
* |
| 76 |
* @param[in] ts Time specifier. |
| 77 |
* @param[in] offset Offset specifier, can take following values: |
| 78 |
* - `INT_MAX`: `ts` is in local time. |
| 79 |
* - `INT_MAX - 1`: `ts` is in UTC. |
| 80 |
* - `-86400` to `86400`: fixed timezone. |
| 81 |
* @exception rb_eArgError Malformed `offset`. |
| 82 |
* @return An allocated instance of ::rb_cTime. |
| 83 |
*/ |
| 84 |
VALUE rb_time_timespec_new(const struct timespec *ts, int offset); |
| 85 |
|
| 86 |
/** |
| 87 |
* Identical to rb_time_timespec_new(), except it takes Ruby values instead of |
| 88 |
* C structs. |
| 89 |
* |
| 90 |
* @param[in] timev Something numeric. Currently Integers, Rationals, |
| 91 |
* and Floats are accepted. |
| 92 |
* @param[in] off Offset specifier. As of 2.7 this argument is |
| 93 |
* heavily extended to take following kinds of |
| 94 |
* objects: |
| 95 |
* - ::RUBY_Qundef ... means UTC. |
| 96 |
* - ::rb_cString ... "+12:34" etc. |
| 97 |
* - A mysterious "zone" object. This is largely |
| 98 |
* undocumented. However the initial intent was |
| 99 |
* that we want to accept |
| 100 |
* `ActiveSupport::TimeZone` here. Other gems |
| 101 |
* could also be possible... But how to make an |
| 102 |
* acceptable class is beyond this document. |
| 103 |
* @exception rb_eArgError Malformed `off`. |
| 104 |
* @return An allocated instance of ::rb_cTime. |
| 105 |
*/ |
| 106 |
VALUE rb_time_num_new(VALUE timev, VALUE off); |
| 107 |
|
| 108 |
/** |
| 109 |
* Creates a "time interval". This basically converts an instance of |
| 110 |
* ::rb_cNumeric into a struct `timeval`, but for instance negative time |
| 111 |
* interval must not exist. |
| 112 |
* |
| 113 |
* @param[in] num An instance of ::rb_cNumeric. |
| 114 |
* @exception rb_eArgError `num` is negative. |
| 115 |
* @exception rb_eRangeError `num` is out of range of `timeval::tv_sec`. |
| 116 |
* @return A struct that represents the identical time to `num`. |
| 117 |
*/ |
| 118 |
struct timeval rb_time_interval(VALUE num); |
| 119 |
|
| 120 |
/** |
| 121 |
* Converts an instance of rb_cTime to a struct timeval that represents the |
| 122 |
* identical point of time. It can also take something numeric; would consider |
| 123 |
* it as a UNIX time then. |
| 124 |
* |
| 125 |
* @param[in] time Instance of either ::rb_cTime or ::rb_cNumeric. |
| 126 |
* @exception rb_eRangeError `time` is out of range of `timeval::tv_sec`. |
| 127 |
* @return A struct that represents the identical time to `num`. |
| 128 |
*/ |
| 129 |
struct timeval rb_time_timeval(VALUE time); |
| 130 |
|
| 131 |
/** |
| 132 |
* Identical to rb_time_timeval(), except for return type. |
| 133 |
* |
| 134 |
* @param[in] time Instance of either ::rb_cTime or ::rb_cNumeric. |
| 135 |
* @exception rb_eRangeError `time` is out of range of `timeval::tv_sec`. |
| 136 |
* @return A struct that represents the identical time to `num`. |
| 137 |
*/ |
| 138 |
struct timespec rb_time_timespec(VALUE time); |
| 139 |
|
| 140 |
/** |
| 141 |
* Identical to rb_time_interval(), except for return type. |
| 142 |
* |
| 143 |
* @param[in] num An instance of ::rb_cNumeric. |
| 144 |
* @exception rb_eArgError `num` is negative. |
| 145 |
* @exception rb_eRangeError `num` is out of range of `timespec::tv_sec`. |
| 146 |
* @return A struct that represents the identical time to `num`. |
| 147 |
*/ |
| 148 |
struct timespec rb_time_timespec_interval(VALUE num); |
| 149 |
|
| 150 |
/** |
| 151 |
* Queries the offset, in seconds between the time zone of the time and the |
| 152 |
* UTC. |
| 153 |
* |
| 154 |
* @param[in] time An instance of ::rb_cTime. |
| 155 |
* @return Numeric offset. |
| 156 |
*/ |
| 157 |
VALUE rb_time_utc_offset(VALUE time); |
| 158 |
|
| 159 |
RBIMPL_SYMBOL_EXPORT_END() |
| 160 |
|
| 161 |
#endif /* RBIMPL_INTERN_TIME_H */ |