AmiTimeKeeper/locale.c

185 lines
5.0 KiB
C

/*-
* Copyright (c) 2020 Carsten Sonne Larsen <cs@innolan.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "locale.h"
#include "global.h"
#include "string.h"
#include "ptz.h"
#include "mem.h"
#include <proto/timer.h>
#include <proto/locale.h>
#include <proto/utility.h>
struct AppLocale *OpenAppLocale(struct Locale *locale)
{
int i, j;
struct AppLocale *l = AllocStructSafe(struct AppLocale);
struct Locale *loc = locale;
if (!loc)
{
loc = OpenLocale(NULL);
}
if (loc == NULL)
{
return NULL;
}
l->X_fmt = (const char *)loc->loc_TimeFormat;
l->x_fmt = (const char *)loc->loc_ShortDateFormat;
l->c_fmt = (const char *)loc->loc_DateTimeFormat;
l->am = (const char *)GetLocaleStr(loc, AM_STR);
l->pm = (const char *)GetLocaleStr(loc, PM_STR);
l->date_fmt = "%a %b %e %H:%M:%S %Z %Y";
j = 0;
for (i = 1; i < 8; i++)
{
l->weekday[j++] = (const char *)GetLocaleStr(loc, i);
}
j = 0;
for (i = 8; i < 15; i++)
{
l->wday[j++] = (const char *)GetLocaleStr(loc, i);
}
j = 0;
for (i = 15; i < 27; i++)
{
l->month[j++] = (const char *)GetLocaleStr(loc, i);
}
j = 0;
for (i = 27; i < 39; i++)
{
l->mon[j++] = (const char *)GetLocaleStr(loc, i);
}
l->AmigaLocale = loc;
return l;
}
void CloseAppLocale(struct AppLocale *lc)
{
if (lc->AmigaLocale)
{
CloseLocale(lc->AmigaLocale);
}
FreeMemSafe(lc);
}
void GetTimezoneText(struct AppLocale *loc, char *text, long parens)
{
if (loc == NULL || text == NULL)
{
return;
}
if (Timezone == NULL)
{
long hoursOffset = (loc->AmigaLocale->loc_GMTOffset / -60L);
long minutsOffset = (loc->AmigaLocale->loc_GMTOffset % -60L);
SNPrintf(text, TIMEZONE_TEXT_LEN, "UTC%s%02ld:%02ld",
(loc->AmigaLocale->loc_GMTOffset < 0L) ? "+" : "",
hoursOffset,
minutsOffset);
}
else if (parens == ZONE_IN_PARENS)
{
SNPrintf(text, TIMEZONE_TEXT_LEN, "UTC%s%02ld:%02ld (%s)",
PosixTimezoneSignChar(&Timezone->current),
Timezone->current.offset.hours,
Timezone->current.offset.minutes,
Timezone->current.abbreviation);
}
else if (parens == OFFSET_IN_PARENS)
{
SNPrintf(text, TIMEZONE_TEXT_LEN, "%s (UTC%s%02ld:%02ld)",
Timezone->current.abbreviation,
PosixTimezoneSignChar(&Timezone->current),
Timezone->current.offset.hours,
Timezone->current.offset.minutes);
}
}
char* GetTimeText(struct AppLocale *loc, ULONG time)
{
char *out;
struct ClockData cd;
struct timeval tv;
ULONG t = time;
out = AllocStringSafe(128);
if (loc == NULL)
{
return out;
}
if (t == LOCAL_TIME_NOW)
{
GetSysTime(&tv);
t = (ULONG)tv.tv_secs;
}
Amiga2Date(t, &cd);
if (Timezone == NULL)
{
long hoursOffset = (loc->AmigaLocale->loc_GMTOffset / -60L);
long minutsOffset = (loc->AmigaLocale->loc_GMTOffset % -60L);
SNPrintf(out, 128, "%s %02ld %s %02ld:%02ld:%02ld%s%02ld%02ld %ld",
loc->weekday[cd.wday],
(long)cd.mday,
loc->month[cd.month - 1],
(long)cd.hour,
(long)cd.min,
(long)cd.sec,
(loc->AmigaLocale->loc_GMTOffset < 0L) ? "+" : "",
hoursOffset,
minutsOffset,
(long)cd.year);
}
else
{
SNPrintf(out, 128, "%s %02ld %s %02ld:%02ld:%02ld %s %ld",
loc->weekday[cd.wday],
(long)cd.mday,
loc->month[cd.month - 1],
(long)cd.hour,
(long)cd.min,
(long)cd.sec,
Timezone->current.abbreviation,
(long)cd.year);
}
return out;
}