Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
DateFormatter.c
Go to the documentation of this file.
1/*
2 * Objectively: Ultra-lightweight object oriented framework for GNU C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include "Config.h"
25
26#include <assert.h>
27#include <stdio.h>
28#include <stdlib.h>
29
30#include "DateFormatter.h"
31
32#define _Class _DateFormatter
33
34#pragma mark - DateFormatter
35
40static Date *dateFromCharacters(const DateFormatter *self, const char *chars) {
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}
58
63static Date *dateFromString(const DateFormatter *self, const String *string) {
64
65 if (string) {
66 return $(self, dateFromCharacters, string->chars);
67 }
68
69 return NULL;
70}
71
76static DateFormatter *initWithFormat(DateFormatter *self, const char *fmt) {
77
78 self = (DateFormatter *) super(Object, self, init);
79 if (self) {
80 self->fmt = fmt ?: DATEFORMAT_ISO8601;
81 }
82
83 return self;
84}
85
90static String *stringFromDate(const DateFormatter *self, const Date *date) {
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}
112
113#pragma mark - Class lifecycle
114
118static void initialize(Class *clazz) {
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}
127
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}
149
150#undef _Class
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
#define super(type, obj, method,...)
struct timeval Time
Time (seconds and microseconds).
Definition: Date.h:60
static void initialize(Class *clazz)
Date formatting and parsing.
#define DATEFORMAT_ISO8601
ISO8601 date format.
Definition: DateFormatter.h:37
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
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
ident interface
The interface of the Class.
Definition: Class.h:105
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
Data * initWithMemory(Data *self, ident mem, size_t length)
Initializes this Data, taking ownership of the specified memory.
Definition: Data.c:209
Date formatting and parsing.
Definition: DateFormatter.h:48
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
Class * _DateFormatter(void)
The DateFormatter archetype.
Date * dateFromCharacters(const DateFormatter *self, const char *chars)
Parses a Date from the specified UTF-8 encoded C string.
Definition: DateFormatter.c:40
const char * fmt
The UTF-8 encoded format string.
Definition: DateFormatter.h:64
Date * dateFromString(const DateFormatter *self, const String *string)
Parses a Date from the specified String.
Definition: DateFormatter.c:63
Microsecond-precision immutable dates.
Definition: Date.h:70
Date * initWithTime(Date *self, const Time *time)
Definition: Date.c:139
Time time
The time.
Definition: Date.h:86
Date * date(void)
Returns a new Date with the current Time.
Definition: Date.c:98
MutableString * string(void)
Returns a new MutableString.
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Class * _Object(void)
The Object archetype.
Definition: Object.c:136
Immutable UTF-8 strings.
Definition: String.h:69
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition: String.h:85
OBJECTIVELY_EXPORT String * str(const char *fmt,...)
A convenience function for instantiating Strings.
Definition: String.c:739