| 1 | /* |
| 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | * |
| 6 | * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V. |
| 7 | */ |
| 8 | |
| 9 | #ifndef _MONETTIME_H_ |
| 10 | #define _MONETTIME_H_ |
| 11 | |
| 12 | #include "mal.h" |
| 13 | #include <time.h> /* for time_t */ |
| 14 | |
| 15 | mal_export int TYPE_date; |
| 16 | typedef int date; /* we use 26 bits out of 32 */ |
| 17 | #define date_nil ((date) {int_nil}) |
| 18 | #define is_date_nil(x) ((x) == date_nil) |
| 19 | |
| 20 | mal_export int TYPE_daytime; |
| 21 | typedef lng daytime; /* we use 37 bits out of 64 */ |
| 22 | #define daytime_nil ((daytime) {lng_nil}) |
| 23 | #define is_daytime_nil(x) ((x) == daytime_nil) |
| 24 | |
| 25 | mal_export int TYPE_timestamp; |
| 26 | typedef lng timestamp; /* we use 26+37=63 bits out of 64 */ |
| 27 | #define timestamp_nil ((timestamp) {lng_nil}) |
| 28 | #define is_timestamp_nil(x) ((x) == timestamp_nil) |
| 29 | |
| 30 | /* functions to manipulate date, daytime, and timestamp values */ |
| 31 | #define HOUR_USEC (60*60*LL_CONSTANT(1000000)) /* usec in an hour */ |
| 32 | #define DAY_USEC (24*HOUR_USEC) /* usec in a day */ |
| 33 | |
| 34 | mal_export date date_create(int year, int month, int day); |
| 35 | mal_export date date_add_day(date dt, int days); |
| 36 | mal_export date date_add_month(date dt, int months); |
| 37 | #define date_add_year(d, y) date_add_month(d, (y) * 12) |
| 38 | mal_export int date_diff(date d1, date d2); |
| 39 | mal_export int date_dayofweek(date dt); /* Monday=1, Sunday=7 */ |
| 40 | mal_export int date_weekofyear(date dt); |
| 41 | mal_export int date_dayofyear(date dt); |
| 42 | mal_export int date_year(date dt); |
| 43 | mal_export int date_month(date dt); |
| 44 | mal_export int date_day(date dt); |
| 45 | |
| 46 | mal_export daytime daytime_create(int hour, int minute, int second, int usec); |
| 47 | mal_export daytime daytime_add_usec(daytime tm, lng usec); |
| 48 | #define daytime_add_sec(t, s) daytime_add_usec(t, (s)*LL_CONSTANT(1000000)) |
| 49 | #define daytime_add_min(t, m) daytime_add_sec(t, (m) * 60) |
| 50 | #define daytime_add_hour(t, h) daytime_add_min(t, (h) * 60) |
| 51 | |
| 52 | /* add to daytime, but wrap around (modulo a full day) */ |
| 53 | mal_export daytime daytime_add_usec_modulo(daytime tm, lng usec); |
| 54 | #define daytime_add_sec_modulo(t, s) daytime_add_usec_modulo(t, (s)*LL_CONSTANT(1000000)) |
| 55 | #define daytime_add_min_modulo(t, m) daytime_add_sec_modulo(t, (m) * 60) |
| 56 | #define daytime_add_hour_modulo(t, h) daytime_add_min_modulo(t, (h) * 60) |
| 57 | mal_export int daytime_hour(daytime tm); |
| 58 | mal_export int daytime_min(daytime tm); |
| 59 | mal_export int daytime_sec(daytime tm); |
| 60 | mal_export int daytime_usec(daytime tm); |
| 61 | mal_export int daytime_sec_usec(daytime tm); |
| 62 | |
| 63 | mal_export timestamp timestamp_fromtime(time_t timeval); |
| 64 | mal_export timestamp timestamp_fromusec(lng usec); |
| 65 | mal_export timestamp timestamp_fromdate(date dt); |
| 66 | mal_export timestamp timestamp_create(date dt, daytime tm); |
| 67 | mal_export timestamp timestamp_current(void); |
| 68 | mal_export timestamp timestamp_add_usec(timestamp t, lng usec); |
| 69 | #define timestamp_add_sec(t, s) timestamp_add_usec(t, (s)*LL_CONSTANT(1000000)) |
| 70 | #define timestamp_add_min(t, m) timestamp_add_sec(t, (m) * 60) |
| 71 | #define timestamp_add_hour(t, h) timestamp_add_min(t, (h) * 60) |
| 72 | #define timestamp_add_day(t, d) timestamp_add_hour(t, (d) * 24) |
| 73 | mal_export timestamp timestamp_add_month(timestamp t, int m); |
| 74 | #define timestamp_add_year(t, y) timestamp_add_month(t, (y) * 12) |
| 75 | mal_export date timestamp_date(timestamp t); |
| 76 | mal_export daytime timestamp_daytime(timestamp t); |
| 77 | mal_export lng timestamp_diff(timestamp t1, timestamp t2); |
| 78 | |
| 79 | /* interfaces for GDK level atoms date, daytime, and timestamp */ |
| 80 | mal_export ssize_t date_fromstr(const char *buf, size_t *len, date **d, bool external); |
| 81 | mal_export ssize_t date_tostr(str *buf, size_t *len, const date *val, bool external); |
| 82 | mal_export ssize_t daytime_tz_fromstr(const char *buf, size_t *len, daytime **d, bool external); |
| 83 | mal_export ssize_t daytime_fromstr(const char *buf, size_t *len, daytime **d, bool external); |
| 84 | mal_export ssize_t daytime_precision_tostr(str *buf, size_t *len, const daytime dt, int precision, bool external); |
| 85 | mal_export ssize_t daytime_tostr(str *buf, size_t *len, const daytime *val, bool external); |
| 86 | mal_export ssize_t timestamp_fromstr(const char *buf, size_t *len, timestamp **d, bool external); |
| 87 | mal_export ssize_t timestamp_tz_fromstr(const char *buf, size_t *len, timestamp **ret, bool external); |
| 88 | mal_export ssize_t timestamp_tostr(str *buf, size_t *len, const timestamp *val, bool external); |
| 89 | mal_export ssize_t timestamp_precision_tostr(str *buf, size_t *len, timestamp val, int precision, bool external); |
| 90 | |
| 91 | mal_export str MTIMEanalyticalrangebounds(BAT *r, BAT *b, BAT *p, BAT *l, |
| 92 | const void* restrict bound, |
| 93 | int tp1, int tp2, bool preceding, |
| 94 | lng first_half); |
| 95 | |
| 96 | #endif /* _MONETTIME_H_ */ |
| 97 | |