Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 15:06 02 May 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Time

Author Message
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1676
Posted: 05:33am 15 Apr 2017
Copy link to clipboard 
Print this post

Is it possible to convert Unix time to local or GMT or UTC time on the micromite?
Has anyone any code for doing it if it is now possible?
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8592
Posted: 07:33am 15 Apr 2017
Copy link to clipboard 
Print this post

This is the code in C, I'm sure someone can help convert it. It looks a bit complex but it is really quite simple

% = MOD
&& = AND
|| = OR
! = NOT
== means = in an IF statement
--y1 = y=y-1
a+=b = a=a+b
while { = DO WHILE
...
} LOOP

use integer divide \ thoughout rather than /

all variables are integers


enum _WEEK_DAYS_ {
327 SUNDAY,
328 MONDAY,
329 TUESDAY,
330 WEDNESDAY,
331 THURSDAY,
332 FRIDAY,
333 SATURDAY
334 };
335
336 /**
337 Enumerated labels for the months.
338 */
339 enum _MONTHS_ {
340 JANUARY,
341 FEBRUARY,
342 MARCH,
343 APRIL,
344 MAY,
345 JUNE,
346 JULY,
347 AUGUST,
348 SEPTEMBER,
349 OCTOBER,
350 NOVEMBER,
351 DECEMBER
352 };
struct tm {
145 int8_t tm_sec; /**< seconds after the minute - [ 0 to 59 ] */
146 int8_t tm_min; /**< minutes after the hour - [ 0 to 59 ] */
147 int8_t tm_hour; /**< hours since midnight - [ 0 to 23 ] */
148 int8_t tm_mday; /**< day of the month - [ 1 to 31 ] */
149 int8_t tm_wday; /**< days since Sunday - [ 0 to 6 ] */
150 int8_t tm_mon; /**< months since January - [ 0 to 11 ] */
151 int16_t tm_year; /**< years since 1900 */
152 int16_t tm_yday; /**< days since January 1 - [ 0 to 365 ] */
153 int16_t tm_isdst; /**< Daylight Saving Time flag */
154 };
#define YEAR0 1900 /* the first year */
#define EPOCH_YR 1970 /* EPOCH = Jan 1 1970 00:00:00 */
#define SECS_DAY (24L * 60L * 60L)
#define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) % 400)))
#define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
#define FIRSTSUNDAY(timp) (((timp)->tm_yday - (timp)->tm_wday + 420) % 7)
#define FIRSTDAYOF(timp) (((timp)->tm_wday - (timp)->tm_yday + 420) % 7)
const int _ytab[2][12] = {
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};
/* Number of days per month (except for February in leap years). */
static const int monoff[] = {
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
};

static int
is_leap_year(int year)
{
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}

static int
leap_days(int y1, int y2)
{
--y1;
--y2;
return (y2/4 - y1/4) - (y2/100 - y1/100) + (y2/400 - y1/400);
}
time_t
timegm(const struct tm *tm)
{
int year;
time_t days;
time_t hours;
time_t minutes;
time_t seconds;

year = 1900 + tm->tm_year;
days = 365 * (year - 1970) + leap_days(1970, year);
days += monoff[tm->tm_mon];

if (tm->tm_mon > 1 && is_leap_year(year))
++days;
days += tm->tm_mday - 1;

hours = days * 24 + tm->tm_hour;
minutes = hours * 60 + tm->tm_min;
seconds = minutes * 60 + tm->tm_sec;

return seconds;
}
struct tm * gmtime(const time_t *timer)
{
static struct tm br_time;
register struct tm *timep = &br_time;
time_t time = *timer;
register unsigned long dayclock, dayno;
int year = EPOCH_YR;

dayclock = (unsigned long)time % SECS_DAY;
dayno = (unsigned long)time / SECS_DAY;

timep->tm_sec = dayclock % 60;
timep->tm_min = (dayclock % 3600) / 60;
timep->tm_hour = dayclock / 3600;
timep->tm_wday = (dayno + 4) % 7; /* day 0 was a thursday */
while (dayno >= YEARSIZE(year)) {
dayno -= YEARSIZE(year);
year++;
}
timep->tm_year = year - YEAR0;
timep->tm_yday = dayno;
timep->tm_mon = 0;
while (dayno >= _ytab[LEAPYEAR(year)][timep->tm_mon]) {
dayno -= _ytab[LEAPYEAR(year)][timep->tm_mon];
timep->tm_mon++;
}
timep->tm_mday = dayno + 1;
timep->tm_isdst = 0;

return timep;
}
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2794
Posted: 08:41am 15 Apr 2017
Copy link to clipboard 
Print this post

Sorry for my 'C' ignorance but with something like:

timep->tm_sec = dayclock % 60;

does the left hand side of the '=' symbol refer to the variable name; OR is there some arithmetic stuff happening with the '->' inclusion of the left hand side? Put another way - what does this translate into in MMBASIC?

WW


For everything Micromite visit micromite.org

Direct Email: whitewizzard@micromite.o
 
jman

Guru

Joined: 12/06/2011
Location: New Zealand
Posts: 711
Posted: 09:26am 15 Apr 2017
Copy link to clipboard 
Print this post

Hi
I do it like this the code is a little old but take a look here NTP Clock

LB% = LB% - 2208988800 ' - 70 Years start @ 1/1/1970 Epoch time

Jan2014 = LB% - 1388534400 'Start at Jan 1/1/2014
TimeZone = (TZ*60*60) 'Set TimeZone
Jan2014=Jan2014+TimeZone 'Adjust for Timezone
Year=2014 'Set Year to 2014

GetYear:
LEAP = Year Mod 4 '0 if leap year
If LEAP = 0 Then
SecondsInYear = 31622400 '366*60*60**24
Else
SecondsInYear = 31536000 '365*60*60*24
EndIf
If Jan2014 > SecondsInYear Then
Jan2014 = Jan2014 - SecondsInyear
Year = Year + 1
GoTo GetYear
EndIf

Month = 1 ' Start with Month = 1

GetMonth:
SecondsInMonth = Months(Month)
If Leap = 0 Then
If Month = 2 Then
SecondsInMonth=LeapMnth
EndIf
EndIf
If Jan2014 >= SecondsInMonth Then
Jan2014 = Jan2014 - SecondsInMonth
Month = Month+1
GoTo GetMonth
EndIf

FindDays:
Day = Int(Jan2014/86400)
Jan2014 = Jan2014 Mod 86400
Hour = Int(Jan2014/3600)
Jan2014 = Jan2014 Mod 3600
Min = Int(Jan2014/60)
Jan2014 = Jan2014 Mod 60
Sec = Jan2014
Day = Day + 1

If DST = 1 Then
GoSub DsTime
If CurrDT < DsStop Or CurrDT > DsStart Then
Hour = Hour + 1
EndIf
EndIf

If Hour=24 Then hour=0

Date$=Str$(Day)+"/"+Str$(Month)+"/"+Str$(Year)
Time$=Str$(Hour)+":"+Str$(Min)+":"+Str$(Sec)

Print Time$
Print Date$


Regards
Jman
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024