Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Macros | Functions
Date.c File Reference
#include <assert.h>
#include "Date.h"
#include "Hash.h"

Go to the source code of this file.

Macros

#define _Class   _Date
 

Functions

Class_Date (void)
 
static Order compareTo (const Date *self, const Date *other)
 
static Datedate (void)
 
static DatedateWithTimeSinceNow (const Time *interval)
 
static int hash (const Object *self)
 
static Dateinit (Date *self)
 
static void initialize (Class *clazz)
 
static DateinitWithTime (Date *self, const Time *time)
 
static _Bool isEqual (const Object *self, const Object *other)
 
static Time timeSinceDate (const Date *self, const Date *date)
 
static Time timeSinceNow (const Date *self)
 
static Time timeSinceTime (const Date *self, const Time *time)
 

Macro Definition Documentation

◆ _Class

#define _Class   _Date

Definition at line 29 of file Date.c.

Function Documentation

◆ _Date()

Class * _Date ( void  )

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
Class * _Object(void)
The Object archetype.
Definition: Object.c:136

◆ compareTo()

static Order compareTo ( const Date self,
const Date other 
)
static

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()

static Date * date ( void  )
static

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()

static Date * dateWithTimeSinceNow ( const Time interval)
static

Definition at line 107 of file Date.c.

107 {
108
109 Date *date = $(alloc(Date), init);
110 if (date) {
111 if (interval) {
112 date->time.tv_sec += interval->tv_sec;
113 date->time.tv_usec += interval->tv_usec;
114 if (date->time.tv_usec >= MSEC_PER_SEC) {
115 date->time.tv_sec++;
116 date->time.tv_usec -= MSEC_PER_SEC;
117 } else if (date->time.tv_usec < 0) {
118 date->time.tv_sec--;
119 date->time.tv_usec += MSEC_PER_SEC;
120 }
121 }
122 }
123
124 return date;
125}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
#define MSEC_PER_SEC
Microseconds per second.
Definition: Date.h:50
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
Date * date(void)
Returns a new Date with the current Time.
Definition: Date.c:98

◆ hash()

static int hash ( const Object self)
static
See also
Object::hash(const Object *)

Definition at line 36 of file Date.c.

36 {
37
38 Date *this = (Date *) self;
39
40 int hash = HASH_SEED;
41
42 hash = HashForInteger(hash, this->time.tv_sec);
43 hash = HashForInteger(hash, this->time.tv_usec);
44
45 return hash;
46}
int HashForInteger(int hash, const long integer)
Accumulates the hash value of integer into hash.
Definition: Hash.c:62
#define HASH_SEED
The hash seed value.
Definition: Hash.h:37
int hash(const Object *self)
Definition: Array.c:80

◆ init()

static Date * init ( Date self)
static

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

◆ initialize()

static void initialize ( Class clazz)
static
See also
Class::initialize(Class *)

Definition at line 203 of file Date.c.

203 {
204
205 ((ObjectInterface *) clazz->interface)->hash = hash;
206 ((ObjectInterface *) clazz->interface)->isEqual = isEqual;
207
208 ((DateInterface *) clazz->interface)->compareTo = compareTo;
209 ((DateInterface *) clazz->interface)->date = date;
210 ((DateInterface *) clazz->interface)->dateWithTimeSinceNow = dateWithTimeSinceNow;
211 ((DateInterface *) clazz->interface)->init = init;
212 ((DateInterface *) clazz->interface)->initWithTime = initWithTime;
213 ((DateInterface *) clazz->interface)->timeSinceDate = timeSinceDate;
214 ((DateInterface *) clazz->interface)->timeSinceNow = timeSinceNow;
215 ((DateInterface *) clazz->interface)->timeSinceTime = timeSinceTime;
216}
ident interface
The interface of the Class.
Definition: Class.h:105
Time timeSinceTime(const Date *self, const Time *time)
Definition: Date.c:183
Time timeSinceNow(const Date *self)
Definition: Date.c:168
Date * init(Date *self)
Definition: Date.c:131
Order compareTo(const Date *self, const Date *other)
Compares this Date to another.
Definition: Date.c:74
Time timeSinceDate(const Date *self, const Date *date)
Definition: Date.c:157
_Bool isEqual(const Object *self, const Object *other)
Tests equality of the other Object.
Definition: Array.c:96

◆ initWithTime()

static Date * initWithTime ( Date self,
const Time time 
)
static

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,...)
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46

◆ isEqual()

static _Bool isEqual ( const Object self,
const Object other 
)
static
See also
Object::isEqual(const Object *, const Object *)

Definition at line 51 of file Date.c.

51 {
52
53 if (super(Object, self, isEqual, other)) {
54 return true;
55 }
56
57 if (other && $(other, isKindOfClass, _Date())) {
58
59 const Date *this = (Date *) self;
60 const Date *that = (Date *) other;
61
62 return $(this, compareTo, that) == OrderSame;
63 }
64
65 return false;
66}
Class * _Date(void)
The Date archetype.
Definition: Date.c:222
_Bool isKindOfClass(const Object *self, const Class *clazz)
Tests for Class hierarchy membership.
Definition: Object.c:101

◆ timeSinceDate()

static Time timeSinceDate ( const Date self,
const Date date 
)
static

Definition at line 157 of file Date.c.

157 {
158
159 assert(date);
160
161 return $(self, timeSinceTime, &date->time);
162}

◆ timeSinceNow()

static Time timeSinceNow ( const Date self)
static

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

◆ timeSinceTime()

static Time timeSinceTime ( const Date self,
const Time time 
)
static

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}