Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Properties | Methods | Protected Attributes
Date Struct Reference

#include <Date.h>

Overview

Microsecond-precision immutable dates.

Definition at line 70 of file Date.h.

Inheritance diagram for Date:
Object

Properties

Object object
 The superclass. More...
 
Time time
 The time. More...
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class. More...
 

Methods

Class_Date (void)
 The Date archetype. More...
 
Order compareTo (const Date *self, const Date *other)
 Compares this Date to another. More...
 
Datedate (void)
 Returns a new Date with the current Time. More...
 
DatedateWithTimeSinceNow (const Time interval)
 Returns a new Date with the given Time since now. More...
 
Dateinit (Date *self)
 
DateinitWithTime (Date *self, const Time *time)
 
Time timeSinceDate (const Date *self, const Date *date)
 
Time timeSinceNow (const Date *self)
 
Time timeSinceTime (const Date *self, const Time *time)
 
- Methods inherited from Object
Class_Object (void)
 The Object archetype. More...
 
Objectcopy (const Object *self)
 Creates a shallow copy of this Object. More...
 
void dealloc (Object *self)
 Frees all resources held by this Object. More...
 
Stringdescription (const Object *self)
 
int hash (const Object *self)
 
Objectinit (Object *self)
 Initializes this Object. More...
 
_Bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object. More...
 
_Bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership. More...
 

Protected Attributes

DateInterface * interface
 The interface. More...
 
- Protected Attributes inherited from Object
ObjectInterface * interface
 The interface. More...
 

Property Details

◆ interface

DateInterface* Date::interface
protected

The interface.

Definition at line 81 of file Date.h.

◆ object

Object Date::object

The superclass.

Definition at line 75 of file Date.h.

◆ time

Time Date::time

The time.

Definition at line 86 of file Date.h.

Method Details

◆ _Date()

Class * _Date ( void  )

The Date archetype.

Returns
The Date Class.

Definition at line 222 of file Date.c.

222 {
223 static Class *clazz;
224 static Once once;
225
226 do_once(&once, {
227 clazz = _initialize(&(const ClassDef) {
228 .name = "Date",
229 .superclass = _Object(),
230 .instanceSize = sizeof(Date),
231 .interfaceOffset = offsetof(Date, interface),
232 .interfaceSize = sizeof(DateInterface),
234 });
235 });
236
237 return clazz;
238}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
Definition: Date.c:203
long Once
The Once type.
Definition: Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition: Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition: Class.h:41
The runtime representation of a Class.
Definition: Class.h:95
Microsecond-precision immutable dates.
Definition: Date.h:70
DateInterface * interface
The interface.
Definition: Date.h:81
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition: Object.h:51
Class * _Object(void)
The Object archetype.
Definition: Object.c:136

◆ compareTo()

Order compareTo ( const Date self,
const Date other 
)

Compares this Date to another.

Parameters
selfThe Date.
otherThe Date to compare to.
Returns
The ordering of this Date compared to other.

Definition at line 74 of file Date.c.

74 {
75
76 if (other) {
77 const long seconds = self->time.tv_sec - other->time.tv_sec;
78 if (seconds == 0) {
79
80 const long microseconds = self->time.tv_usec - other->time.tv_usec;
81 if (microseconds == 0) {
82 return OrderSame;
83 }
84
85 return microseconds < 0 ? OrderAscending : OrderDescending;
86 }
87
88 return seconds < 0 ? OrderAscending : OrderDescending;
89 }
90
91 return OrderAscending;
92}
@ OrderSame
Definition: Types.h:72
@ OrderDescending
Definition: Types.h:73
@ OrderAscending
Definition: Types.h:71
Time time
The time.
Definition: Date.h:86

◆ date()

Date * date ( void  )

Returns a new Date with the current Time.

Returns
The new Date, or NULL on error.

Definition at line 98 of file Date.c.

98 {
99
100 return $$(Date, dateWithTimeSinceNow, NULL);
101}
Date * dateWithTimeSinceNow(const Time interval)
Returns a new Date with the given Time since now.

◆ dateWithTimeSinceNow()

Date * dateWithTimeSinceNow ( const Time  interval)

Returns a new Date with the given Time since now.

Returns
The new Date, or NULL on error.

◆ init()

Date * init ( Date self)

Initializes a Date with the current time.

Parameters
selfThe Date.
Returns
The initialized Date, or NULL on error.

Definition at line 131 of file Date.c.

131 {
132 return $(self, initWithTime, NULL);
133}
Date * initWithTime(Date *self, const Time *time)
Definition: Date.c:139

◆ initWithTime()

Date * initWithTime ( Date self,
const Time time 
)

Initializes a Date with the specified time.

Parameters
selfThe Date.
timeThe desired Time.
Returns
The initialized Date, or NULL on error.

Definition at line 139 of file Date.c.

139 {
140
141 self = (Date *) super(Object, self, init);
142 if (self) {
143 if (time) {
144 self->time = *time;
145 } else {
146 gettimeofday(&self->time, NULL);
147 }
148 }
149
150 return self;
151}
#define super(type, obj, method,...)
Date * init(Date *self)
Definition: Date.c:131
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46

◆ timeSinceDate()

Time timeSinceDate ( const Date self,
const Date date 
)
Parameters
selfThe Date.
dateThe Date from whence to return a Time delta.
Returns
The interval between this Date and date.
Remarks
If date is earlier than this Date, the returned interval will be negative.

Definition at line 157 of file Date.c.

157 {
158
159 assert(date);
160
161 return $(self, timeSinceTime, &date->time);
162}
Time timeSinceTime(const Date *self, const Time *time)
Definition: Date.c:183
Date * date(void)
Returns a new Date with the current Time.
Definition: Date.c:98

◆ timeSinceNow()

Time timeSinceNow ( const Date self)
Parameters
selfThe Date.
Returns
The interval between this Date and the current date.
Remarks
If this Date is later than the current date, the returned interval will be negative.

Definition at line 168 of file Date.c.

168 {
169
170 Date *date = $$(Date, date);
171
172 Time time = $(self, timeSinceDate, date);
173
174 release(date);
175
176 return time;
177}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
struct timeval Time
Time (seconds and microseconds).
Definition: Date.h:60
Time timeSinceDate(const Date *self, const Date *date)
Definition: Date.c:157

◆ timeSinceTime()

Time timeSinceTime ( const Date self,
const Time time 
)
Parameters
selfThe Date.
timeA Time value.
Returns
The interval between this Date and time.
Remarks
If time is earlier than this Date, the returned interval will be negative.

Definition at line 183 of file Date.c.

183 {
184
185 Time delta = {
186 .tv_sec = self->time.tv_sec - time->tv_sec,
187 .tv_usec = self->time.tv_usec - time->tv_usec
188 };
189
190 if (delta.tv_usec < 0) {
191 delta.tv_sec--;
192 delta.tv_usec += MSEC_PER_SEC;
193 }
194
195 return delta;
196}
#define MSEC_PER_SEC
Microseconds per second.
Definition: Date.h:50

The documentation for this struct was generated from the following files: