Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 3877678

Browse files
committed
[[ TimeZoneLibrary ]] Tweak timezone library for use with LCB
This patch exposes tzsetlcl to enable setting the locale manually by calling into the library, and adds the ability to set the search path for the timezone database files.
1 parent 1cd9705 commit 3877678

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

extensions/libraries/timezone/tz/localtime.c

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#define LOCALTIME_IMPLEMENTATION
1414
#include "private.h"
15+
#include "tz.h"
1516

1617
#include "tzfile.h"
1718
#include <fcntl.h>
@@ -357,7 +358,46 @@ union input_buffer {
357358
};
358359

359360
/* TZDIR with a trailing '/' rather than a trailing '\0'. */
360-
static char const tzdirslash[sizeof TZDIR] = TZDIR "/";
361+
static char *s_tzdirslash = NULL;
362+
363+
bool tzsetdir(const char* dir)
364+
{
365+
if (s_tzdirslash != NULL)
366+
free(s_tzdirslash);
367+
368+
size_t t_length = strlen(dir);
369+
s_tzdirslash = malloc(t_length + 2);
370+
if (s_tzdirslash == NULL)
371+
return false;
372+
373+
strcpy(s_tzdirslash, dir);
374+
s_tzdirslash[t_length] = '/';
375+
// Nul terminate
376+
s_tzdirslash[t_length + 1] = '\0';
377+
return true;
378+
}
379+
380+
static const char* tzgetdirslash()
381+
{
382+
if (s_tzdirslash == NULL)
383+
tzsetdir(TZDIR);
384+
385+
return s_tzdirslash;
386+
}
387+
388+
#define tzdirslash tzgetdirslash()
389+
#ifdef _WINDOWS
390+
#include <windows.h>
391+
#ifndef PATH_MAX
392+
#define PATH_MAX MAX_PATH
393+
#endif
394+
#endif
395+
396+
#if !defined(__HAVE_SSIZE_T__)
397+
typedef intptr_t ssize_t;
398+
399+
# define SSIZE_MAX INTPTR_MAX
400+
#endif
361401

362402
/* Local storage needed for 'tzloadbody'. */
363403
union local_storage {
@@ -372,7 +412,7 @@ union local_storage {
372412

373413
/* The file name to be opened. */
374414
char fullname[BIGGEST(sizeof (struct file_analysis),
375-
sizeof tzdirslash + 1024)];
415+
PATH_MAX)];
376416
};
377417

378418
/* Load tz data from the file named NAME into *SP. Read extended
@@ -409,14 +449,14 @@ tzloadbody(char const *name, struct state *sp, bool doextend,
409449
#endif
410450
if (!doaccess) {
411451
size_t namelen = strlen(name);
412-
if (sizeof lsp->fullname - sizeof tzdirslash <= namelen)
452+
if (sizeof lsp->fullname - strlen(tzdirslash) <= namelen)
413453
return ENAMETOOLONG;
414454

415455
/* Create a string "TZDIR/NAME". Using sprintf here
416456
would pull in stdio (and would fail if the
417457
resulting string length exceeded INT_MAX!). */
418-
memcpy(lsp->fullname, tzdirslash, sizeof tzdirslash);
419-
strcpy(lsp->fullname + sizeof tzdirslash, name);
458+
memcpy(lsp->fullname, tzdirslash, strlen(tzdirslash));
459+
strcpy(lsp->fullname + strlen(tzdirslash), name);
420460

421461
/* Set doaccess if '.' (as in "../") shows up in name. */
422462
if (strchr(name, '.'))
@@ -1316,7 +1356,7 @@ zoneinit(struct state *sp, char const *name)
13161356
}
13171357
}
13181358

1319-
static void
1359+
void
13201360
tzsetlcl(char const *name)
13211361
{
13221362
struct state *sp = lclptr;
@@ -1353,7 +1393,8 @@ tzsetwall(void)
13531393
static void
13541394
tzset_unlocked(void)
13551395
{
1356-
tzsetlcl(getenv("TZ"));
1396+
// Control locale externally using tzsetlcl
1397+
//tzsetlcl(getenv("TZ"));
13571398
}
13581399

13591400
void
@@ -1536,11 +1577,13 @@ localtime_tzset(time_t const *timep, struct tm *tmp, bool setname)
15361577
return tmp;
15371578
}
15381579

1580+
#if 0
15391581
struct tm *
15401582
localtime(const time_t *timep)
15411583
{
15421584
return localtime_tzset(timep, &tm, true);
15431585
}
1586+
#endif
15441587

15451588
struct tm *
15461589
localtime_r(const time_t *timep, struct tm *tmp)
@@ -1582,11 +1625,13 @@ gmtime_r(const time_t *timep, struct tm *tmp)
15821625
return gmtsub(gmtptr, timep, 0, tmp);
15831626
}
15841627

1628+
#if 0
15851629
struct tm *
15861630
gmtime(const time_t *timep)
15871631
{
15881632
return gmtime_r(timep, &tm);
15891633
}
1634+
#endif
15901635

15911636
#ifdef STD_INSPIRED
15921637

@@ -1731,6 +1776,7 @@ timesub(const time_t *timep, int_fast32_t offset,
17311776
return NULL;
17321777
}
17331778

1779+
#if 0
17341780
char *
17351781
ctime(const time_t *timep)
17361782
{
@@ -1743,6 +1789,7 @@ ctime(const time_t *timep)
17431789
struct tm *tmp = localtime(timep);
17441790
return tmp ? asctime(tmp) : NULL;
17451791
}
1792+
#endif
17461793

17471794
char *
17481795
ctime_r(const time_t *timep, char *buf)
@@ -2163,7 +2210,7 @@ mktime_z(struct state *sp, struct tm *tmp)
21632210
#endif
21642211

21652212
time_t
2166-
mktime(struct tm *tmp)
2213+
mktime_tz(struct tm *tmp)
21672214
{
21682215
time_t t;
21692216
int err = lock();

extensions/libraries/timezone/tz/private.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@
104104
# define NETBSD_INSPIRED 1
105105
#endif
106106

107+
#ifdef WIN32
108+
#undef HAVE_UNISTD_H
109+
#undef HAVE_SYS_WAIT_H
110+
#undef HAVE_POSIX_DECLS
111+
#endif
112+
107113
#if HAVE_INCOMPATIBLE_CTIME_R
108114
#define asctime_r _incompatible_asctime_r
109115
#define ctime_r _incompatible_ctime_r

0 commit comments

Comments
 (0)