1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
/* ************************************************************************ */
/* */
/* Neko Standard Library */
/* Copyright (c)2005 Motion-Twin */
/* */
/* This library is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Lesser General Public */
/* License as published by the Free Software Foundation; either */
/* version 2.1 of the License, or (at your option) any later version. */
/* */
/* This library is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
/* Lesser General Public License or the LICENSE file for more details. */
/* */
/* ************************************************************************ */
#include <neko.h>
#include <time.h>
#include <stdio.h>
#include <memory.h>
/**
<doc>
<h1>Date</h1>
<p>
Date are using standard C functions in order to manipulate a 32 bit integer.
Dates are then represented as the number of seconds elapsed since 1st January
1970.
</p>
</doc>
**/
extern field id_h;
extern field id_m;
extern field id_s;
extern field id_y;
extern field id_d;
#ifdef NEKO_WINDOWS
static struct tm *localtime_r( time_t *t, struct tm *r ) {
struct tm *r2 = localtime(t);
if( r2 == NULL ) return NULL;
*r = *r2;
return r;
}
static struct tm *gmtime_r( time_t *t, struct tm *r ) {
struct tm *r2 = gmtime(t);
if( r2 == NULL ) return NULL;
*r = *r2;
return r;
}
#endif
/**
date_now : void -> 'int32
<doc>Return current date and time</doc>
**/
static value date_now() {
int t = (int)time(NULL);
return alloc_int32(t);
}
/**
date_new : string? -> 'int32
<doc>
Parse a date format. The following formats are accepted :
<ul>
<li>[null] : return current date and time</li>
<li>[YYYY-MM-DD HH:MM:SS] : full date and time</li>
<li>[YYYY-MM-DD] : date only (time will be set to midnight)</li>
<li>[HH:MM:SS] : this represent an elapsed time. It will be corrected with timezone so you can subtract it from a date.</li>
</ul>
</doc>
**/
static value date_new( value s ) {
int o = 0;
if( val_is_null(s) )
o = (int)time(NULL);
else {
struct tm t;
bool recal = true;
val_check(s,string);
memset(&t,0,sizeof(struct tm));
switch( val_strlen(s) ) {
case 19:
sscanf(val_string(s),"%4d-%2d-%2d %2d:%2d:%2d",&t.tm_year,&t.tm_mon,&t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
t.tm_isdst = -1;
break;
case 8:
sscanf(val_string(s),"%2d:%2d:%2d",&t.tm_hour,&t.tm_min,&t.tm_sec);
o = t.tm_sec + t.tm_min * 60 + t.tm_hour * 60 * 60;
recal = false;
break;
case 10:
sscanf(val_string(s),"%4d-%2d-%2d",&t.tm_year,&t.tm_mon,&t.tm_mday);
break;
default:
{
buffer b = alloc_buffer("Invalid date format : ");
val_buffer(b,s);
bfailure(b);
}
}
if( recal ) {
t.tm_year -= 1900;
t.tm_mon--;
o = (int)mktime(&t);
}
}
return alloc_int32(o);
}
/**
date_format : #int32 -> fmt:string? -> string
<doc>Format a date using [strftime]. If [fmt] is [null] then default format is used</doc>
**/
static value date_format( value o, value fmt ) {
char buf[128];
struct tm t;
time_t d;
val_check(o,int32);
if( val_is_null(fmt) )
fmt = alloc_string("%Y-%m-%d %H:%M:%S");
val_check(fmt,string);
d = val_int32(o);
if( localtime_r(&d,&t) == NULL )
neko_error();
strftime(buf,127,val_string(fmt),&t);
return alloc_string(buf);
}
/**
date_set_hour : #int32 -> h:int -> m:int -> s:int -> 'int32
<doc>Change the time of a date. Return the modified date</doc>
**/
static value date_set_hour( value o, value h, value m, value s ) {
struct tm t;
time_t d;
val_check(o,int32);
val_check(h,int);
val_check(m,int);
val_check(s,int);
d = val_int32(o);
if( localtime_r(&d,&t) == NULL )
neko_error();
t.tm_hour = val_int(h);
t.tm_min = val_int(m);
t.tm_sec = val_int(s);
d = mktime(&t);
if( d == -1 )
neko_error();
return alloc_int32(d);
}
/**
date_set_day : #int32 -> y:int -> m:int -> d:int -> 'int32
<doc>Change the day of a date. Return the modified date</doc>
**/
static value date_set_day( value o, value y, value m, value d ) {
struct tm t;
time_t date;
val_check(o,int32);
val_check(y,int);
val_check(m,int);
val_check(d,int);
date = val_int32(o);
if( localtime_r(&date,&t) == NULL )
neko_error();
t.tm_year = val_int(y) - 1900;
t.tm_mon = val_int(m) - 1;
t.tm_mday = val_int(d);
date = mktime(&t);
if( date == -1 )
neko_error();
return alloc_int32(date);
}
/**
date_get_day : #int32 -> { y => int, m => int, d => int }
<doc>Return the year month and day of a date</doc>
**/
static value date_get_day( value o ) {
value r;
struct tm t;
time_t d;
val_check(o,int32);
d = val_int32(o);
if( localtime_r(&d,&t) == NULL )
neko_error();
r = alloc_object(NULL);
alloc_field(r,id_y,alloc_int(t.tm_year + 1900));
alloc_field(r,id_m,alloc_int(t.tm_mon + 1));
alloc_field(r,id_d,alloc_int(t.tm_mday));
return r;
}
/**
date_get_hour : #int32 -> { h => int, m => int, s => int }
<doc>Return the hour minutes and seconds of a date</doc>
**/
static value date_get_hour( value o ) {
value r;
struct tm t;
time_t d;
val_check(o,int32);
d = val_int32(o);
if( localtime_r(&d,&t) == NULL )
neko_error();
r = alloc_object(NULL);
alloc_field(r,id_h,alloc_int(t.tm_hour));
alloc_field(r,id_m,alloc_int(t.tm_min));
alloc_field(r,id_s,alloc_int(t.tm_sec));
return r;
}
/**
date_get_tz : void -> int
<doc>Return the local Timezone (in seconds)</doc>
**/
static value date_get_tz() {
struct tm local;
struct tm gmt;
int diff;
time_t raw = time(NULL);
if( localtime_r(&raw, &local) == NULL || gmtime_r(&raw, &gmt) == NULL )
neko_error();
diff = (local.tm_hour - gmt.tm_hour) * 3600 + (local.tm_min - gmt.tm_min) * 60;
// adjust for different days/years
if( gmt.tm_year > local.tm_year || gmt.tm_yday > local.tm_yday )
diff -= 24 * 3600;
else if( gmt.tm_year < local.tm_year || gmt.tm_yday < local.tm_yday )
diff += 24 * 3600;
return alloc_int(diff);
}
DEFINE_PRIM(date_now,0);
DEFINE_PRIM(date_new,1);
DEFINE_PRIM(date_format,2);
DEFINE_PRIM(date_set_hour,4);
DEFINE_PRIM(date_set_day,4);
DEFINE_PRIM(date_get_hour,1);
DEFINE_PRIM(date_get_day,1);
DEFINE_PRIM(date_get_tz,0);
/* ************************************************************************ */
|