diff options
Diffstat (limited to 'src/third_party/timelib/docs')
| -rw-r--r-- | src/third_party/timelib/docs/date-from-iso-parts.c | 133 | ||||
| -rw-r--r-- | src/third_party/timelib/docs/date-from-parts.c | 124 | ||||
| -rw-r--r-- | src/third_party/timelib/docs/date-from-string.c | 167 | ||||
| -rw-r--r-- | src/third_party/timelib/docs/date-to-parts.c | 133 | ||||
| -rw-r--r-- | src/third_party/timelib/docs/show-tzinfo.c | 61 |
5 files changed, 618 insertions, 0 deletions
diff --git a/src/third_party/timelib/docs/date-from-iso-parts.c b/src/third_party/timelib/docs/date-from-iso-parts.c new file mode 100644 index 00000000000..9311bfa8cc3 --- /dev/null +++ b/src/third_party/timelib/docs/date-from-iso-parts.c @@ -0,0 +1,133 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017 Derick Rethans + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/* + * Example that shows how to convert a date/time in its parts, to a + * Unix timestamp. + * + * Compile with: + * gcc -ggdb3 -o date-from-parts date-from-parts.c ../timelib.a -lm + */ + +#include <stdio.h> +#include <string.h> +#include "../timelib.h" + +struct { + timelib_tzdb *db; + /* cache *tz_cache; */ +} global; + +void create_cache(timelib_tzdb *db) +{ + global.db = db; + + /* Loop over all the entries and store in tz_cache */ +} + +void cleanup_cache() +{ + if (global.db != timelib_builtin_db()) { + timelib_zoneinfo_dtor(global.db); + } + + /* Loop over all the entries in tz_cache and free */ +} + +timelib_tzinfo *cached_tzfile_wrapper(const char *tz_id, const timelib_tzdb *db, int *error) +{ + /* return tz_cache[tzid]; (pseudo code) */ + return timelib_parse_tzfile(tz_id, global.db, error); +} + +timelib_tzinfo *cached_fetch_tzinfo(const char *tz_id) +{ + int dummy_error; + + return cached_tzfile_wrapper(tz_id, global.db, &dummy_error); +} + +int main(int argc, char *argv[]) +{ + timelib_sll ty; + timelib_sll tw; + timelib_sll td; + timelib_sll th = 12; + timelib_sll ti = 50; + timelib_sll ts = 58; + timelib_sll tus = 48 * 1000; + const char *tz_id = "America/New_York"; + timelib_time *t; + timelib_tzinfo *tzi; + + if (argc < 4) { + printf("Usage:\n\tdate-from-iso-parts isoyear isoweek isoday\n\tExample: ./date-from-iso-parts 2017 23 2\n\n"); + exit(-1); + } + + ty = atoll(argv[1]); + tw = atoll(argv[2]); + td = atoll(argv[3]); + + create_cache((timelib_tzdb*) timelib_builtin_db()); + + tzi = cached_fetch_tzinfo(tz_id); + + t = timelib_time_ctor(); + timelib_date_from_isodate(ty, tw, td, &t->y, &t->m, &t->d); + t->h = th; t->i = ti; t->s = ts; + t->us = tus; + + timelib_update_ts(t, tzi); + timelib_set_timezone(t, tzi); + timelib_unixtime2gmt(t, t->sse); /* Note it says gmt in the function name */ + + +#define LLABS(y) (y < 0 ? (y * -1) : y) + + /* Show parts Y/m/d */ + { + printf( + "%s%04lld-%02lld-%02lld %02lld:%02lld:%02lld", + t->y < 0 ? "-" : "", LLABS(t->y), + t->m, t->d, t->h, t->i, t->s + ); + if (t->us > 0) { + printf(".%06lld", t->us); + } + printf("\n"); + } + + + /* Show Unix timestamp */ + { + printf("Timestamp: %lld\n", t->sse); + } + + + timelib_tzinfo_dtor(tzi); + timelib_time_dtor(t); + + cleanup_cache(); +} diff --git a/src/third_party/timelib/docs/date-from-parts.c b/src/third_party/timelib/docs/date-from-parts.c new file mode 100644 index 00000000000..886ace2916c --- /dev/null +++ b/src/third_party/timelib/docs/date-from-parts.c @@ -0,0 +1,124 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017 Derick Rethans + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/* + * Example that shows how to convert a date/time in its parts, to a + * Unix timestamp. + * + * Compile with: + * gcc -ggdb3 -o date-from-parts date-from-parts.c ../timelib.a -lm + */ + +#include <stdio.h> +#include <string.h> +#include "../timelib.h" + +struct { + timelib_tzdb *db; + /* cache *tz_cache; */ +} global; + +void create_cache(timelib_tzdb *db) +{ + global.db = db; + + /* Loop over all the entries and store in tz_cache */ +} + +void cleanup_cache() +{ + if (global.db != timelib_builtin_db()) { + timelib_zoneinfo_dtor(global.db); + } + + /* Loop over all the entries in tz_cache and free */ +} + +timelib_tzinfo *cached_tzfile_wrapper(const char *tz_id, const timelib_tzdb *db, int *error) +{ + /* return tz_cache[tzid]; (pseudo code) */ + return timelib_parse_tzfile(tz_id, global.db, error); +} + +timelib_tzinfo *cached_fetch_tzinfo(const char *tz_id) +{ + int dummy_error; + + return cached_tzfile_wrapper(tz_id, global.db, &dummy_error); +} + +int main(void) +{ + timelib_sll ty = 2017; + timelib_sll tm = 6; + timelib_sll td = 6; + timelib_sll th = 12; + timelib_sll ti = 50; + timelib_sll ts = 58; + timelib_sll tus = 713 * 1000; + const char *tz_id = "America/New_York"; + timelib_time *t; + timelib_tzinfo *tzi; + + create_cache((timelib_tzdb*) timelib_builtin_db()); + + tzi = cached_fetch_tzinfo(tz_id); + + t = timelib_time_ctor(); + t->y = ty; t->m = tm; t->d = td; + t->h = th; t->i = ti; t->s = ts; + t->us = tus; + + timelib_update_ts(t, tzi); + timelib_set_timezone(t, tzi); + timelib_unixtime2gmt(t, t->sse); /* Note it says gmt in the function name */ + + +#define LLABS(y) (y < 0 ? (y * -1) : y) + + /* Show parts Y/m/d */ + { + printf( + "%s%04lld-%02lld-%02lld %02lld:%02lld:%02lld", + t->y < 0 ? "-" : "", LLABS(t->y), + t->m, t->d, t->h, t->i, t->s + ); + if (t->us > 0) { + printf(".%06lld", t->us); + } + printf("\n"); + } + + + /* Show Unix timestamp */ + { + printf("Timestamp: %lld\n", t->sse); + } + + + timelib_tzinfo_dtor(tzi); + timelib_time_dtor(t); + + cleanup_cache(); +} diff --git a/src/third_party/timelib/docs/date-from-string.c b/src/third_party/timelib/docs/date-from-string.c new file mode 100644 index 00000000000..40fd40b5362 --- /dev/null +++ b/src/third_party/timelib/docs/date-from-string.c @@ -0,0 +1,167 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017 MongoDB, Inc. + * Copyright (c) 2017 Derick Rethans + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/* + * Example that shows how to convert a date/time in its parts, to a + * Unix timestamp. + * + * Compile with: + * gcc -ggdb3 -o date-from-string date-from-string.c ../timelib.a -lm + */ + +#include <stdio.h> +#include <string.h> +#include <time.h> +#include "../timelib.h" + +struct { + timelib_tzdb *db; + /* cache *tz_cache; */ +} global; + +void create_cache(timelib_tzdb *db) +{ + global.db = db; + + /* Loop over all the entries and store in tz_cache */ +} + +void cleanup_cache() +{ + if (global.db != timelib_builtin_db()) { + timelib_zoneinfo_dtor(global.db); + } + + /* Loop over all the entries in tz_cache and free */ +} + +timelib_tzinfo *cached_tzfile_wrapper(const char *tz_id, const timelib_tzdb *db, int *error) +{ + /* return tz_cache[tzid]; (pseudo code) */ + return timelib_parse_tzfile(tz_id, global.db, error); +} + +timelib_tzinfo *cached_fetch_tzinfo(const char *tz_id) +{ + int dummy_error; + + return cached_tzfile_wrapper(tz_id, global.db, &dummy_error); +} + +int main(int argc, char *argv[]) +{ + char *time_string = NULL; + const char *tz_id = NULL; + timelib_time *t; +#if defined(PARTIAL) + timelib_time *t_now; +#endif + timelib_tzinfo *tzi = NULL; + timelib_tzinfo *tzi_utc = NULL; + timelib_error_container *errors; + + if (argc < 2) { + printf("Usage:\n\tdate-from-string string [tzExpression]\n\n"); + exit(-1); + } + + time_string = argv[1]; + + if (argc >= 3) { + tz_id = argv[2]; + } + + create_cache((timelib_tzdb*) timelib_builtin_db()); + + if (tz_id) { + tzi = cached_fetch_tzinfo(tz_id); + } + tzi_utc = cached_fetch_tzinfo("UTC"); + +#if defined(PARTIAL) + /* Doing this means you can also supply partial date/time strings, such as + * "July 4th" */ + + /* Create "now" to fill in the holes */ + t_now = timelib_time_ctor(); + if (tzi) { + timelib_set_timezone(t_now, tzi); + } else { + timelib_set_timezone(t_now, tzi_utc); + } + timelib_unixtime2gmt(t_now, time(NULL)); +#endif + + /* Convert from string to timelib_t + * + * Passing in the "Z" at the end of the string, means the extra timezone gets ignored. + * If you *don't* want that, then compile with -DDONT_IGNORE_TZ */ + t = timelib_strtotime(time_string, strlen(time_string), &errors, global.db, cached_tzfile_wrapper); + + /* Error handling */ + if (errors->warning_count) { + printf("Warnings found while parsing '%s'\n", time_string); + } + if (errors->error_count) { + printf("Errors found while parsing '%s'\n", time_string); + } + timelib_error_container_dtor(errors); + +#if defined(PARTIAL) + /* Add missing fields */ + timelib_fill_holes(t, t_now, TIMELIB_NO_CLONE); +#endif + + if (tzi) { +#if defined(DONT_IGNORE_TZ) + timelib_set_timezone(t, tzi); +#endif + + timelib_update_ts(t, tzi); + timelib_unixtime2local(t, t->sse); + timelib_dump_date(t, 1); + } else { + timelib_update_ts(t, tzi_utc); + timelib_dump_date(t, 1); + } + + timelib_set_timezone(t, tzi_utc); + timelib_unixtime2local(t, t->sse); + + /* Show Unix timestamp */ + timelib_dump_date(t, 1); + printf("Timestamp: %lld\n", (t->sse * 1000) + (int) (t->us / 1000.0)); + + timelib_time_dtor(t); +#if defined(PARTIAL) + timelib_time_dtor(t_now); +#endif + if (tzi) { + timelib_tzinfo_dtor(tzi); + } + timelib_tzinfo_dtor(tzi_utc); + + cleanup_cache(); +} diff --git a/src/third_party/timelib/docs/date-to-parts.c b/src/third_party/timelib/docs/date-to-parts.c new file mode 100644 index 00000000000..ab1f3ffcc71 --- /dev/null +++ b/src/third_party/timelib/docs/date-to-parts.c @@ -0,0 +1,133 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017 Derick Rethans + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/* + * Example that shows how to convert a string and TZ identifier to its parts, + * in both normal and ISO week date parts. + * + * Compile with: + * gcc -ggdb3 -o date-to-parts date-to-parts.c ../timelib.a -lm + */ + +#include <stdio.h> +#include <string.h> +#include "../timelib.h" + +struct { + timelib_tzdb *db; + /* cache *tz_cache; */ +} global; + +void create_cache(timelib_tzdb *db) +{ + global.db = db; + + /* Loop over all the entries and store in tz_cache */ +} + +void cleanup_cache() +{ + if (global.db != timelib_builtin_db()) { + timelib_zoneinfo_dtor(global.db); + } + + /* Loop over all the entries in tz_cache and free */ +} + +timelib_tzinfo *cached_tzfile_wrapper(const char *tz_id, const timelib_tzdb *db, int *error) +{ + /* return tz_cache[tzid]; (pseudo code) */ + return timelib_parse_tzfile(tz_id, global.db, error); +} + +timelib_tzinfo *cached_fetch_tzinfo(const char *tz_id) +{ + int dummy_error; + + return cached_tzfile_wrapper(tz_id, global.db, &dummy_error); +} + +int main(void) +{ + const char *dt_string = "2017-06-05T11:30:09.123Z"; + const char *tz_id = "Europe/London"; + timelib_time *t; + timelib_tzinfo *tzi; + timelib_error_container *errors; + + create_cache((timelib_tzdb*) timelib_builtin_db()); + + tzi = cached_fetch_tzinfo(tz_id); + + /* Convert string to timelib_time, and hence its constituent parts */ + t = timelib_strtotime( + dt_string, strlen(dt_string), + &errors, + global.db, + cached_tzfile_wrapper + ); + timelib_update_ts(t, tzi); + timelib_set_timezone(t, tzi); + timelib_unixtime2local(t, t->sse); + + +#define LLABS(y) (y < 0 ? (y * -1) : y) + + /* Show parts Y/m/d */ + { + printf( + "%s%04lld-%02lld-%02lld %02lld:%02lld:%02lld", + t->y < 0 ? "-" : "", LLABS(t->y), + t->m, t->d, t->h, t->i, t->s + ); + if (t->us > 0) { + printf(".%03lld", (t->us / 1000)); + } + printf("\n"); + } + + + /* Show parts ISO */ + { + timelib_sll iso_year, iso_week, iso_dow; + + timelib_isodate_from_date(t->y, t->m, t->d, &iso_year, &iso_week, &iso_dow); + printf( + "%s%04lldW%02lldD%02lld %02lld:%02lld:%02lld", + iso_year < 0 ? "-" : "", LLABS(iso_year), + iso_week, iso_dow, + t->h, t->i, t->s + ); + if (t->us > 0) { + printf(".%03lld", (t->us / 1000)); + } + printf("\n"); + } + + timelib_error_container_dtor(errors); + timelib_tzinfo_dtor(tzi); + timelib_time_dtor(t); + + cleanup_cache(); +} diff --git a/src/third_party/timelib/docs/show-tzinfo.c b/src/third_party/timelib/docs/show-tzinfo.c new file mode 100644 index 00000000000..642aa05bd4f --- /dev/null +++ b/src/third_party/timelib/docs/show-tzinfo.c @@ -0,0 +1,61 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015 Derick Rethans + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#include "timelib.h" +#include <stdio.h> + +int main(int argc, char *argv[]) +{ + timelib_tzinfo *tz; + int dummy_error; + + if (argc == 3) { + timelib_tzdb *db = timelib_zoneinfo(argv[2]); + + if (!db) { + fprintf(stderr, "Cannot read timezone database in '%s'.\n", argv[2]); + return 2; + } + + tz = timelib_parse_tzfile(argv[1], db, &dummy_error); + if (!tz) { + fprintf(stderr, "Cannot read timezone identifier '%s' from database in '%s'.\n", argv[1], argv[2]); + + timelib_zoneinfo_dtor(db); + return 3; + } + + timelib_zoneinfo_dtor(db); + } else { + tz = timelib_parse_tzfile(argv[1], timelib_builtin_db(), &dummy_error); + if (!tz) { + fprintf(stderr, "Cannot read timezone identifier '%s' from built in database.\n", argv[1]); + return 4; + } + } + timelib_dump_tzinfo(tz); + + timelib_tzinfo_dtor(tz); + + return 0; +} |
