📄 mtime.mx
字号:
#endiftypedef int date;#define date_nil ((date) int_nil)@c#include "mal_config.h"#include "mtime.h"@- daytimeDaytime values are also stored as the number of milliseconds that passed sincethe start of the day (i.e. midnight).@htypedef int daytime;#define daytime_nil ((daytime) int_nil)@- timestampTimestamp is implemented as a record that contains a date and a time (GMT).@htypedef struct {#ifndef WORDS_BIGENDIAN daytime msecs; date days;#else date days; daytime msecs;#endif} timestamp;@- rulerules are used to define the start and end of DST. It uses the 25 lower bits of an int.@htypedef union { struct { unsigned int month:4, /* values: [1..12] */ minutes:11, /* values: [0:1439] */ day:6, /* values: [-31..-1,1..31] */ weekday:4, /* values: [-7..-1,1..7] */ empty:7; /* rule uses just 32-7=25 bits */ } s; int asint; /* the same, seen as single value */} rule;@c#define get_rule(r) ((r).s.weekday | ((r).s.day<<6) | ((r).s.minutes<<10) | ((r).s.month<<21))#define set_rule(r,i) { (r).s.weekday = (i)&15;\ (r).s.day = ((i)&(63<<6))>>6;\ (r).s.minutes = ((i)&(2047<<10))>>10;\ (r).s.month = ((i)&(15<<21))>>21; \ (r).s.empty = 0; /* to please valgrind */ }/* phony zero values, used to get negative numbers from unsigned sub-integers in rule */#define WEEKDAY_ZERO 8#define DAY_ZERO 32#define OFFSET_ZERO 4096@- timezoneA timezone consists of an offset and two DST rules, all crammed into one lng.@htypedef struct { /* we had this as bitfields in one unsigned long long, but native sun CC does not eat that. */ unsigned int dst:1, off1:6, dst_start:25; unsigned int off2:7, dst_end:25;} tzone;@c/* as the offset field got split in two, we need macros to get and set them */#define get_offset(z) (((int) (((z)->off1 << 7) + (z)->off2)) - OFFSET_ZERO)#define set_offset(z,i) { (z)->off1 = (((i)+4096)&8064) >> 7; (z)->off2 = ((i)+OFFSET_ZERO)&127; }tzone tzone_local;@hmtime_export tzone tzone_local;mtime_export void fromdate(int n, int *d, int *m, int *y);mtime_export void fromtime(int n, int *hour, int *min, int *sec, int *msec);mtime_export int daytime_tz_fromstr(str buf, int *len, daytime **ret);mtime_export str MTIMEcurrent_timestamp(timestamp *t);mtime_export str MTIMEcurrent_date(date *d);mtime_export str MTIMEcurrent_time(daytime *t);mtime_export int timestamp_tostr(str *buf, int *len, timestamp *val);mtime_export int timestamp_diff(lng *ret, timestamp *v1, timestamp *v2);mtime_export str MTIMEnil2date(date *ret, int *src);mtime_export str MTIMEdate2date(date *ret, date *src);mtime_export str MTIMEdaytime2daytime(daytime *ret, daytime *src);mtime_export str MTIMEsecs2daytime(daytime *ret, lng *src);mtime_export str MTIMEtimestamp2timestamp(timestamp *ret, timestamp *src);mtime_export str MTIMErule_fromstr(rule *ret, str *s);mtime_export str MTIMEprelude(void);mtime_export str MTIMEepilogue(void);mtime_export str MTIMEsynonyms(bit *allow);mtime_export str MTIMEoldduration(int *ndays, str *s);mtime_export str MTIMEolddate(date *d, str *buf);mtime_export str MTIMEtimezone(tzone *z, str *name);mtime_export str MTIMElocal_timezone(lng *res);mtime_export str MTIMEtzone_set_local(int res, tzone *z);mtime_export str MTIMEtzone_get_local(tzone *z);mtime_export str MTIMEmonth_from_str(int *ret, str *month);mtime_export str MTIMEmonth_to_str(str *ret, int *month);mtime_export str MTIMEday_from_str(int *ret, str *day);mtime_export str MTIMEday_to_str(str *ret, int *day);mtime_export str MTIMEdate_date(date *d, date *s);mtime_export str MTIMEdate_tostr(str *ret, date *d);mtime_export str MTIMEdate_fromstr(date *ret, str *s);mtime_export str MTIMEdate_create(date *ret, int *year, int *month, int *day);mtime_export str MTIMEdaytime_tostr(str *ret, daytime *d);mtime_export str MTIMEdaytime_create(daytime *ret, int *hour, int *min, int *sec, int *msec);mtime_export str MTIMEtimestamp_fromstr(timestamp *ret, str *d);mtime_export str MTIMEtimestamp_timestamp(timestamp *d, timestamp *s);mtime_export str MTIMEtimestamp_create(timestamp *ret, date *d, daytime *t, tzone *z);mtime_export str MTIMEtimestamp_create_default(timestamp *ret, date *d, daytime *t);mtime_export str MTIMEtimestamp_create_from_date(timestamp *ret, date *d);mtime_export str MTIMEdate_extract_year(int *ret, date *v);mtime_export str MTIMEdate_extract_month(int *ret, date *v);mtime_export str MTIMEdate_extract_day(int *ret, date *v);mtime_export str MTIMEdate_extract_dayofyear(int *ret, date *v);mtime_export str MTIMEdate_extract_weekofyear(int *ret, date *v);mtime_export str MTIMEdate_extract_dayofweek(int *ret, date *v);mtime_export str MTIMEdaytime_extract_hours(int *ret, daytime *v);mtime_export str MTIMEdaytime_extract_minutes(int *ret, daytime *v);mtime_export str MTIMEdaytime_extract_seconds(int *ret, daytime *v);mtime_export str MTIMEdaytime_extract_milliseconds(int *ret, daytime *v);mtime_export str MTIMEtimestamp_extract_daytime(daytime *ret, timestamp *t, tzone *z);mtime_export str MTIMEtimestamp_extract_daytime_default(daytime *ret, timestamp *t);mtime_export str MTIMEtimestamp_extract_date(date *ret, timestamp *t, tzone *z);mtime_export str MTIMEtimestamp_extract_date_default(date *ret, timestamp *t);mtime_export str MTIMEdate_addyears(date *ret, date *v, int *delta);mtime_export str MTIMEdate_adddays(date *ret, date *v, int *delta);mtime_export str MTIMEdate_addmonths(date *ret, date *v, int *delta);mtime_export str MTIMEdate_diff(int *ret, date *v1, date *v2);mtime_export str MTIMEtimestamp_add(timestamp *ret, timestamp *v, lng *msecs);mtime_export str MTIMEtimestamp_diff(lng *ret, timestamp *v1, timestamp *v2);mtime_export str MTIMEtimestamp_inside_dst(bit *ret, timestamp *p, tzone *z);mtime_export str MTIMEtimestamp_isnil(bit *retval, timestamp *val);mtime_export str MTIMEtimestamp_EQ(bit *retval, timestamp *val1, timestamp *val2);mtime_export str MTIMEtimestamp_NEQ(bit *retval, timestamp *val1, timestamp *val2);mtime_export str MTIMEtimestamp_year(int *ret, timestamp *t);mtime_export str MTIMEtimestamp_month(int *ret, timestamp *t);mtime_export str MTIMEtimestamp_day(int *ret, timestamp *t);mtime_export str MTIMEtimestamp_hours(int *ret, timestamp *t);mtime_export str MTIMEtimestamp_minutes(int *ret, timestamp *t);mtime_export str MTIMEtimestamp_seconds(int *ret, timestamp *t);mtime_export str MTIMEtimestamp_milliseconds(int *ret, timestamp *t);mtime_export str MTIMEsql_year(int *ret, int *t);mtime_export str MTIMEsql_month(int *ret, int *t);mtime_export str MTIMEsql_day(int *ret, lng *t);mtime_export str MTIMEsql_hours(int *ret, lng *t);mtime_export str MTIMEsql_minutes(int *ret, lng *t);mtime_export str MTIMEsql_seconds(int *ret, lng *t);mtime_export str MTIMEtimestamp_LT(bit *retval, timestamp *val1, timestamp *val2);mtime_export str MTIMEtimestamp_LE(bit *retval, timestamp *val1, timestamp *val2);mtime_export str MTIMEtimestamp_GT(bit *retval, timestamp *val1, timestamp *val2);mtime_export str MTIMEtimestamp_GE(bit *retval, timestamp *val1, timestamp *val2);mtime_export str MTIMErule_tostr(str *s, rule *r);mtime_export str MTIMErule_fromstr(rule *ret, str *s);mtime_export str MTIMErule_create(rule *ret, int *month, int *day, int *weekday, int *minutes);mtime_export str MTIMEtzone_create_dst(tzone *ret, int *minutes, rule *start, rule *end);mtime_export str MTIMEtzone_create(tzone *ret, int *minutes);mtime_export str MTIMErule_extract_month(int *ret, rule *r);mtime_export str MTIMErule_extract_day(int *ret, rule *r);mtime_export str MTIMErule_extract_weekday(int *ret, rule *r);mtime_export str MTIMErule_extract_minutes(int *ret, rule *r);mtime_export str MTIMEtzone_extract_start(rule *ret, tzone *t);mtime_export str MTIMEtzone_extract_end(rule *ret, tzone *t);mtime_export str MTIMEtzone_extract_minutes(int *ret, tzone *t);mtime_export str MTIMEdate_sub_sec_interval_wrap(date *ret, date *t, int *sec);mtime_export str MTIMEdate_sub_sec_interval_lng_wrap(date *ret, date *t, lng *sec);mtime_export str MTIMEdate_add_sec_interval_wrap(date *ret, date *t, int *sec);mtime_export str MTIMEdate_add_sec_interval_lng_wrap(date *ret, date *t, lng *sec);mtime_export str MTIMEdate_add_month_interval_wrap(date *ret, date *t, int *months);mtime_export str MTIMEtimestamp_add_sec_interval_lng_wrap(timestamp *ret, timestamp *t, lng *sec);mtime_export str MTIMEtimestamp_sub_sec_interval_lng_wrap(timestamp *ret, timestamp *t, lng *sec);mtime_export str MTIMEtimestamp_sub_month_interval_wrap(timestamp *ret, timestamp *t, int *months);mtime_export str MTIMEtimestamp_add_month_interval_wrap(timestamp *ret, timestamp *t, int *months);mtime_export str MTIMEtime_sub_sec_interval_wrap(daytime *ret, daytime *t, lng *sec);mtime_export str MTIMEtime_add_sec_interval_wrap(daytime *ret, daytime *t, lng *sec);mtime_export str MTIMEcompute_rule_foryear(date *ret, rule *val, int *year);mtime_export str MTIMEtzone_tostr(str *s, tzone *ret);mtime_export str MTIMEtzone_fromstr(tzone *ret, str *s);mtime_export str MTIMEdaytime_fromstr(daytime *ret, str *s);mtime_export str MTIMEmsecs(lng *ret, int *d, int *h, int *m, int *s, int *ms);mtime_export str MTIMEmsec(lng *r);mtime_export str MTIMEdaytime1(daytime *ret, int *h);mtime_export str MTIMEdaytime2(daytime *ret, int *h, int *m);mtime_export str MTIMEdaytime3(daytime *ret, int *h, int *m, int *s);mtime_export str MTIMEunix_epoch(timestamp *ret);mtime_export str MTIMEepoch(timestamp *ret);mtime_export str MTIMEepoch2lng(lng *ret, timestamp *t);mtime_export str MTIMEtimestamp(timestamp *ret, int *sec);mtime_export str MTIMEruleDef0(rule *ret, int *m, int *d, int *w, int *h, int *mint);mtime_export str MTIMEruleDef1(rule *ret, int *m, str *dnme, int *w, int *h, int *mint);mtime_export str MTIMEruleDef2(rule *ret, int *m, str *dnme, int *w, int *mint);mtime_export str MTIMEcurrent_timestamp(timestamp *t);mtime_export str MTIMEcurrent_date(date *d);mtime_export str MTIMEcurrent_time(daytime *t);mtime_export int date_fromstr(str buf, int *len, date **d);mtime_export int date_tostr(str *buf, int *len, date *val);mtime_export int daytime_fromstr(str buf, int *len, daytime **ret);mtime_export int daytime_tostr(str *buf, int *len, daytime *val);mtime_export int timestamp_fromstr(str buf, int *len, timestamp **ret);mtime_export int timestamp_tostr(str *buf, int *len, timestamp *val);mtime_export int tzone_fromstr(str buf, int *len, tzone **d);mtime_export int tzone_tostr(str *buf, int *len, tzone *z);mtime_export int rule_fromstr(str buf, int *len, rule **d);mtime_export int rule_tostr(str *buf, int *len, rule *r);mtime_export int tzone_fromstr(str buf, int *len, tzone **d);@= ExtractExportmtime_export str MTIME@1_extract_@2_bulk(int *ret, int *bid);@h@:ExtractExport(date,year)@@:ExtractExport(date,month)@@:ExtractExport(date,day)@@:ExtractExport(daytime,hours)@@:ExtractExport(daytime,minutes)@@:ExtractExport(daytime,seconds)@@:ExtractExport(daytime,milliseconds)@mtime_export int TYPE_date;mtime_export int TYPE_daytime;mtime_export int TYPE_timestamp;mtime_export int TYPE_tzone;mtime_export int TYPE_rule;@+ Defines@cstr MONTHS[13] = { NULL, "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"};str DAYS[8] = { NULL, "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};str COUNT1[7] = { NULL, "first", "second", "third", "fourth", "fifth", "last" };str COUNT2[7] = { NULL, "1st", "2nd", "3rd", "4th", "5th", "last" };int NODAYS[13] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };date DATE_MAX, DATE_MIN; /* often used dates; computed once */#define YEAR_MAX 5867411#define YEAR_MIN -YEAR_MAX#define MONTHDAYS(m,y) (((m)!=2)?NODAYS[m]:leapyear(y)?29:28)#define YEARDAYS(y) (leapyear(y)?366:365)#define LEAPYEARS(y) (leapyears(y)+((y)>=0))#define DATE(d,m,y) ((m)>0&&(m)<=12&&(d)>0&&(y)!=0&&(y)>=YEAR_MIN&&(y)<=YEAR_MAX&&(d)<=MONTHDAYS(m,y))#define TIME(h,m,s,x) ((h)>=0&&(h)<24&&(m)>=0&&(m)<60&&(s)>=0&&(s)<60&&(x)>=0 &&(x)<1000)#define LOWER(c) (((c) >= 'A' && (c) <= 'Z') ? (c)+'a'-'A' : (c))@+ auxiliary functions@c#define tz_isnil(z) (get_offset(&(z)) == get_offset(tz_nilptr))#define ts_isnil(t) ((t).days == ts_nilptr->days && (t).msecs == ts_nilptr->msecs)static union { timestamp ts; lng nilval;} ts_nil;static union { tzone tz; lng nilval;} tz_nil;static timestamp *ts_nilptr = NULL;static tzone *tz_nilptr = NULL;static void date_prelude(void);int TYPE_date;int TYPE_daytime;int TYPE_timestamp;int TYPE_tzone;int TYPE_rule;bat *monettime_prelude(void){ ts_nil.nilval = lng_nil; tz_nil.nilval = lng_nil; ts_nilptr = &ts_nil.ts; tz_nilptr = &tz_nil.tz; TYPE_date = ATOMindex("date"); TYPE_daytime = ATOMindex("daytime"); TYPE_timestamp = ATOMindex("timestamp"); TYPE_tzone = ATOMindex("timezone"); TYPE_rule = ATOMindex("rule"); date_prelude(); return NULL;}voidmonettime_epilogue(void){}timestamp *timestamp_null(void){ return (ts_nilptr);}tzone *tzone_null(void){ return (tz_nilptr);}static int synonyms = TRUE;intmonettime_synonyms(bit *allow){ if (*allow != bit_nil) synonyms = *allow; return GDK_SUCCEED;}static INLINE intleapyear(int year){ return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);}static INLINE intleapyears(int year){ /* count the 4-fold years that passed since jan-1-0 */ int y4 = year / 4; /* count the 100-fold years */ int y100 = year / 100; /* count the 400-fold years */ int y400 = year / 400; return y4 + y400 - y100; /* may be negative */}static INLINE datetodate(int day, int month, int year){ date n = date_nil; if (DATE(day, month, year)) { if (year < 0) year++; /* HACK: hide year 0 */ for (n = (date) (day - 1); --month > 0; n += MONTHDAYS(month, year)) ; /* current year does not count as leapyear */ n += 365 * year + LEAPYEARS(year >= 0 ? year - 1 : year); } return n;}voidfromdate(int n, int *d, int *m, int *y){ int month, year = n / 365; int day = (n - year * 365) - LEAPYEARS(year >= 0 ? year - 1 : year); if (n < 0) { year--; while (day >= 0) { year++; day -= YEARDAYS(year); } day = YEARDAYS(year) + day; } else { while (day < 0) { year--; day += YEARDAYS(year); } } day++; for (month = 1; month <= 12; month++) { int days = MONTHDAYS(month, year); if (day <= days) break; day -= days; } if (n != int_nil) { *d = day; *m = month; *y = (year <= 0) ? year - 1 : year; /* HACK: hide year 0 */ } else { *d = *m = *y = int_nil; }}static INLINE daytimetotime(int hour, int min, int sec, int msec){ if (TIME(hour, min, sec, msec)) { return (daytime) (((((hour * 60) + min) * 60) + sec) * 1000 + msec); } return daytime_nil;}voidfromtime(int n, int *hour, int *min, int *sec, int *msec){ if (n != int_nil) { *hour = n / 3600000; n -= (*hour) * 3600000; *min = n / 60000; n -= (*min) * 60000; *sec = n / 1000; n -= (*sec) * 1000; *msec = n; } else { *hour = *min = *sec = *msec = int_nil; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -