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

#include <DateFormatter.h>

Overview

Date formatting and parsing.

Definition at line 48 of file DateFormatter.h.

Inheritance diagram for DateFormatter:
Object

Properties

const char * fmt
 The UTF-8 encoded format string. More...
 
Object object
 The superclass. More...
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class. More...
 

Methods

Class_DateFormatter (void)
 The DateFormatter archetype. More...
 
DatedateFromCharacters (const DateFormatter *self, const char *chars)
 Parses a Date from the specified UTF-8 encoded C string. More...
 
DatedateFromString (const DateFormatter *self, const String *string)
 Parses a Date from the specified String. More...
 
DateFormatterinitWithFormat (DateFormatter *self, const char *fmt)
 Initializes a DateFormatter with the specified format string. More...
 
StringstringFromDate (const DateFormatter *self, const Date *date)
 Yields a String representation of the specified Date instance. More...
 
- 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

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

Property Details

◆ fmt

const char* DateFormatter::fmt

The UTF-8 encoded format string.

Definition at line 64 of file DateFormatter.h.

◆ interface

DateFormatterInterface* DateFormatter::interface
protected

The interface.

Definition at line 59 of file DateFormatter.h.

◆ object

Object DateFormatter::object

The superclass.

Definition at line 53 of file DateFormatter.h.

Method Details

◆ _DateFormatter()

Class * _DateFormatter ( void  )

The DateFormatter archetype.

Returns
The DateFormatter Class.

Definition at line 132 of file DateFormatter.c.

132 {
133 static Class *clazz;
134 static Once once;
135
136 do_once(&once, {
137 clazz = _initialize(&(const ClassDef) {
138 .name = "DateFormatter",
139 .superclass = _Object(),
140 .instanceSize = sizeof(DateFormatter),
141 .interfaceOffset = offsetof(DateFormatter, interface),
142 .interfaceSize = sizeof(DateFormatterInterface),
144 });
145 });
146
147 return clazz;
148}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
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
Date formatting and parsing.
Definition: DateFormatter.h:48
DateFormatterInterface * interface
The interface.
Definition: DateFormatter.h:59
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

◆ dateFromCharacters()

Date * dateFromCharacters ( const DateFormatter self,
const char *  chars 
)

Parses a Date from the specified UTF-8 encoded C string.

Parameters
selfThe DateFormatter.
charsThe UTF-8 encoded C string to parse.
Returns
A Date instance, or NULL on error.

Definition at line 40 of file DateFormatter.c.

40 {
41
42#if defined(_WIN32)
43 fprintf(stderr, "WARNING: %s: not implemented (Windows)\n", __func__);
44#else
45 if (chars) {
46 struct tm time;
47
48 const char *res = strptime(chars, self->fmt, &time);
49 if (res) {
50 const Time t = { .tv_sec = mktime(&time) };
51 return $(alloc(Date), initWithTime, &t);
52 }
53 }
54#endif
55
56 return NULL;
57}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
static Date * initWithTime(Date *self, const Time *time)
Definition: Date.c:139
struct timeval Time
Time (seconds and microseconds).
Definition: Date.h:60
const char * fmt
The UTF-8 encoded format string.
Definition: DateFormatter.h:64
Microsecond-precision immutable dates.
Definition: Date.h:70

◆ dateFromString()

Date * dateFromString ( const DateFormatter self,
const String string 
)

Parses a Date from the specified String.

Parameters
selfThe DateFormatter.
stringThe String to parse.
Returns
A Date instance, or NULL on error.

Definition at line 63 of file DateFormatter.c.

63 {
64
65 if (string) {
66 return $(self, dateFromCharacters, string->chars);
67 }
68
69 return NULL;
70}
static MutableString * string(void)
Date * dateFromCharacters(const DateFormatter *self, const char *chars)
Parses a Date from the specified UTF-8 encoded C string.
Definition: DateFormatter.c:40
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition: String.h:85

◆ initWithFormat()

DateFormatter * initWithFormat ( DateFormatter self,
const char *  fmt 
)

Initializes a DateFormatter with the specified format string.

Parameters
selfThe DateFormatter.
fmtThe format string.
Returns
The initialized DateFormatter, or NULL on error.

Definition at line 76 of file DateFormatter.c.

76 {
77
78 self = (DateFormatter *) super(Object, self, init);
79 if (self) {
80 self->fmt = fmt ?: DATEFORMAT_ISO8601;
81 }
82
83 return self;
84}
#define super(type, obj, method,...)
#define DATEFORMAT_ISO8601
ISO8601 date format.
Definition: DateFormatter.h:37
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Object * init(Object *self)
Initializes this Object.
Definition: Object.c:83

◆ stringFromDate()

String * stringFromDate ( const DateFormatter self,
const Date date 
)

Yields a String representation of the specified Date instance.

Parameters
selfThe DateFormatter.
dateThe Date to format.
Returns
The String representation of the Date, or NULL on error.

Definition at line 90 of file DateFormatter.c.

90 {
91
92 const time_t seconds = date->time.tv_sec;
93 struct tm time;
94
95#if defined(_WIN32)
96 int err = localtime_s(&time, &seconds);
97 assert(err == 0);
98#else
99 ident res = localtime_r(&seconds, &time);
100 assert(res == &time);
101#endif
102
103 char *str = calloc(1024, sizeof(char));
104 assert(str);
105
106 const size_t length = strftime(str, 1024, self->fmt, &time);
107 str = realloc(str, length + 1 * sizeof(char));
108 assert(str);
109
110 return $(alloc(String), initWithMemory, str, length);
111}
static Data * initWithMemory(Data *self, ident mem, size_t length)
Definition: Data.c:209
static Date * date(void)
Definition: Date.c:98
String * str(const char *fmt,...)
Definition: String.c:739
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
Time time
The time.
Definition: Date.h:86
Immutable UTF-8 strings.
Definition: String.h:69

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