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

Go to the source code of this file.

Macros

#define _Class   _DateFormatter
 

Functions

Class_DateFormatter (void)
 
static DatedateFromCharacters (const DateFormatter *self, const char *chars)
 
static DatedateFromString (const DateFormatter *self, const String *string)
 
static void initialize (Class *clazz)
 
static DateFormatterinitWithFormat (DateFormatter *self, const char *fmt)
 
static StringstringFromDate (const DateFormatter *self, const Date *date)
 

Macro Definition Documentation

◆ _Class

#define _Class   _DateFormatter

Definition at line 32 of file DateFormatter.c.

Function Documentation

◆ _DateFormatter()

Class * _DateFormatter ( void  )

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

◆ dateFromCharacters()

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

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
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
Date * initWithTime(Date *self, const Time *time)
Definition: Date.c:139

◆ dateFromString()

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

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}
Date * dateFromCharacters(const DateFormatter *self, const char *chars)
Parses a Date from the specified UTF-8 encoded C string.
Definition: DateFormatter.c:40
MutableString * string(void)
Returns a new MutableString.
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition: String.h:85

◆ initialize()

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

Definition at line 118 of file DateFormatter.c.

118 {
119
120 ((DateFormatterInterface *) clazz->interface)->dateFromCharacters = dateFromCharacters;
121 ((DateFormatterInterface *) clazz->interface)->dateFromString = dateFromString;
122 ((DateFormatterInterface *) clazz->interface)->initWithFormat = initWithFormat;
123 ((DateFormatterInterface *) clazz->interface)->stringFromDate = stringFromDate;
124
125 tzset();
126}
ident interface
The interface of the Class.
Definition: Class.h:105
String * stringFromDate(const DateFormatter *self, const Date *date)
Yields a String representation of the specified Date instance.
Definition: DateFormatter.c:90
DateFormatter * initWithFormat(DateFormatter *self, const char *fmt)
Initializes a DateFormatter with the specified format string.
Definition: DateFormatter.c:76
Date * dateFromString(const DateFormatter *self, const String *string)
Parses a Date from the specified String.
Definition: DateFormatter.c:63

◆ initWithFormat()

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

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
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46

◆ stringFromDate()

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

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}
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
Data * initWithMemory(Data *self, ident mem, size_t length)
Initializes this Data, taking ownership of the specified memory.
Definition: Data.c:209
Time time
The time.
Definition: Date.h:86
Date * date(void)
Returns a new Date with the current Time.
Definition: Date.c:98
Immutable UTF-8 strings.
Definition: String.h:69
OBJECTIVELY_EXPORT String * str(const char *fmt,...)
A convenience function for instantiating Strings.
Definition: String.c:739