ESP8266 ILI9341 display support code with printf sources, wire-frame viewer and custom fonts  1.0
ESP8266ILI9341DisplayProject
Functions | Variables
time.c File Reference

Common Linux/POSIX time functions. More...

#include "user_config.h"
#include "fatfs.h"
#include "printf/mathio.h"
#include "time.h"
#include "timer.h"
#include "posix.h"

Go to the source code of this file.

Functions

MEMSPACE char * tm_wday_to_ascii (int i)
 Get string Short name of day from day number. More...
 
MEMSPACE char * tm_mon_to_ascii (int i)
 Get string Short name of Month from month number. More...
 
static MEMSPACE int IS_Leap (int year)
 Check if a year is a leap year. More...
 
static MEMSPACE int Leap_Days_Since_1900 (int year)
 Number of leap days since 1900 to the BEGINNING of the year. More...
 
MEMSPACE int finddayofweek (int year, int month, int day)
 return day of week for givenn day, month, year More...
 
static MEMSPACE int Days_Per_Year (int year)
 Find number of days in a given year. More...
 
MEMSPACE int Days_Per_Month (int month, int year)
 days in a month More...
 
MEMSPACE time_t time_to_tm (time_t epoch, int32_t offset, tm_t *t)
 Converts epoch ( seconds from 1 Jan EPOCH_YEAR UTC), offset seconds, to UNIX tm *t. More...
 
MEMSPACE time_t timegm (tm_t *t)
 Convert tm_t structure as GMT time into GMT seconds since 1900. All calculactions are in GMT regardless of timezoe settings. More...
 
MEMSPACE char * asctime_r (tm_t *t, char *buf)
 Convert tm_t *t structure into POSIX asctime() ASCII string *buf. More...
 
MEMSPACE char * asctime (tm_t *t)
 Convert tm_t *t structure into POSIX asctime() ASCII string. More...
 
MEMSPACE char * ctime_r (time_t *t, char *buf)
 Convert local time_t *t epoch time into POSIX asctime() ASCII string *buf. More...
 
MEMSPACE char * ctime (time_t *tp)
 Convert local time_t *t epoch time into POSIX asctime() string buf[]. More...
 
MEMSPACE char * ctime_gm (time_t *tp)
 GMT version of POSIX ctime(). More...
 
MEMSPACE tm_tgmtime_r (time_t *t, tm_t *result)
 Convert epoch GMT time_t *tp into POSIX tm_t *result. More...
 
MEMSPACE tm_tgmtime (time_t *tp)
 Convert epoch GMT time_t *tp into POSIX static tm_t *t. More...
 
MEMSPACE tm_tlocaltime_r (time_t *t, tm_t *result)
 Convert POSIX epoch time_t *tp into POSIX tm_t *result expressed as local time using timezone and DST corrections. More...
 
MEMSPACE tm_tlocaltime (time_t *tp)
 Convert POSIX epoch time_t *tp into POSIX tm_t *result. More...
 
MEMSPACE time_t mktime (tm_t *t)
 convert tm_t structure to time_t local time epoch More...
 
static MEMSPACE time_t tm2epoch (tm_t *t)
 Converts tm_t structure as GMT time into GMT epoch since 1900. More...
 
MEMSPACE time_t normalize (tm_t *t, int normalize_to_timezone)
 Normalize POSIX tm_t *t struct and convert to epoch time Note: does not deal with DST - by design. More...
 
MEMSPACE int gettimezone (tz_t *tz)
 Get current timezone in struct timezone *tz - POSIX function. More...
 
MEMSPACE int settimezone (tz_t *tz)
 Set current timezone with struct timezone *tz - POSIX function. More...
 
MEMSPACE int gettimeofday (tv_t *tv, tz_t *tz)
 Get current time struct timeval *tv and struct timezone *tz - POSIX function. We assume a GMT hardware clock. More...
 
MEMSPACE time_t time (time_t *t)
 Return second from epoch - POSIX function. More...
 
MEMSPACE int settimeofday (tv_t *tv, tz_t *tz)
 Set current time struct timeval *tv and struct timezone *tz - POSIX function. We assume a GMT hardware clock. More...
 
MEMSPACE void clock_set (uint32_t seconds, uint32_t us)
 Set system clock with seconds and microseconds. More...
 
MEMSPACE int setdate (void)
 Set date and time by prompting user. More...
 
MEMSPACE int setdate_r (char *buf)
 Set date and time from string in this format "YYYY MM DD HH:MM:SS". More...
 
MEMSPACE time_t find_dst (int dst, time_t epoch, int year, int month, int weekno, int dayno, int hour)
 Calculate GMT seconds of DST transition given LOCAL time start / end time and DST flag. More...
 
MEMSPACE void set_dst (time_t epoch)
 Set DST start and end time for the given epoch year. More...
 
MEMSPACE int is_dst (time_t epoch)
 Test GMT epoch time to see if DST applies in a local timezone. More...
 
MEMSPACE void print_dst ()
 print start/stop for DST as localtime for this year More...
 
MEMSPACE void print_dst_gmt ()
 print start/stop for DST as GMT for this year More...
 
MEMSPACE void initialize_clock (int minwest)
 initialize system time - if we have an RTC use it More...
 
MEMSPACE void display_clock ()
 Display system time and optionally RTC time. More...
 

Variables

volatile ts_t __clock
 System Clock Time. More...
 
tz_t __tzone
 System Time Zone. More...
 
dst_t dst
 DST start and stop in GMT epoch. More...
 
static const uint16_t __days_sum []
 accumulated days to the start of a month in a year. More...
 
static const uint16_t __days []
 days in each month. More...
 
const char * __WDay [] = { "Sun","Mon","Tue","Wed","Thu","Fri","Sat","BAD"}
 Short Name of each Day in a week. More...
 
const char * __Month []
 Short Name or each Month in a year. More...
 

Detailed Description

Common Linux/POSIX time functions.

Copyright © 2015 Mike Gore, GPL License
You are free to use this code under the terms of GPL
please retain a copy of this notice in any code you use it in.

This is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file time.c.

Function Documentation

MEMSPACE char* asctime ( tm_t t)

Convert tm_t *t structure into POSIX asctime() ASCII string.

Parameters
[in]tstruct tm * time input.
Returns
buf[] string pointer in POSIX asctime() format.
  • Example output: "Thu Dec 8 21:45:05 EST 2011".
Warning
result is overwritten on each call.

Definition at line 394 of file time.c.

Referenced by display_clock(), get_date(), and ntp_setup().

MEMSPACE char* asctime_r ( tm_t t,
char *  buf 
)

Convert tm_t *t structure into POSIX asctime() ASCII string *buf.

Todo:
implement strftime() and strptime()
Parameters
[in]ttm_t structure pointer.
[out]bufuser buffer for POSIX asctime() string result.
  • Example output: "Thu Dec 8 21:45:05 EST 2011".
Returns
buf string pointer.

Definition at line 367 of file time.c.

Referenced by asctime(), ctime(), ctime_gm(), ctime_r(), and timetests().

MEMSPACE void clock_set ( uint32_t  seconds,
uint32_t  us 
)

Set system clock with seconds and microseconds.

Definition at line 882 of file time.c.

MEMSPACE char* ctime ( time_t tp)

Convert local time_t *t epoch time into POSIX asctime() string buf[].

Parameters
[in]tptime_t * time input.
Returns
buf[].
  • Example: "Thu Dec 8 21:45:05 EST 2011".

Definition at line 424 of file time.c.

Referenced by ntp_setup(), print_dst(), user_loop(), and user_tests().

MEMSPACE char* ctime_gm ( time_t tp)

GMT version of POSIX ctime().

Parameters
[in]tptime_t * time input.
Returns
buf[].
  • Example: "Thu Dec 8 21:45:05 EST 2011".
See also
ctime()
Warning
result is overwritten on each call.

Definition at line 441 of file time.c.

Referenced by mctime(), ntp_setup(), and print_dst_gmt().

MEMSPACE char* ctime_r ( time_t t,
char *  buf 
)

Convert local time_t *t epoch time into POSIX asctime() ASCII string *buf.

Parameters
[in]ttime_t * time input.
[out]bufstring output.
  • Example output: "Thu Dec 8 21:45:05 EST 2011"
Returns
buf string pointer.

Definition at line 410 of file time.c.

MEMSPACE int Days_Per_Month ( int  month,
int  year 
)

days in a month

Parameters
[in]month;month of year
[in]year;year
Returns
days in month including yeapyears

Definition at line 223 of file time.c.

Referenced by normalize(), and tm2epoch().

static MEMSPACE int Days_Per_Year ( int  year)
static

Find number of days in a given year.

Parameters
[in]yearvalid over 1900..2199.
Warning
No limit checking
Returns
days in a given year.
See also
time_to_tm()
ctime_gm()
gmtime_r()
localtime_r()

Definition at line 211 of file time.c.

Referenced by time_to_tm().

MEMSPACE void display_clock ( void  )

Display system time and optionally RTC time.

Returns
void
See also
: rtc_read
: timegm()
: ascitime()

Definition at line 1227 of file time.c.

Referenced by user_tests().

MEMSPACE time_t find_dst ( int  dst,
time_t  epoch,
int  year,
int  month,
int  weekno,
int  dayno,
int  hour 
)

Calculate GMT seconds of DST transition given LOCAL time start / end time and DST flag.

Parameters
[in]dst0 .. 1 DST needs to be applied to the arguments for DST caluclulations
[in]epoch0 | epoch if non-zero - UTC epoch time used to obtain year of DST calculations
[in]year0 | year if non-zero - UTC year of DST calcululation, if year and epoch are used - ignore epoch
[in]month0 .. 11, local time month DST transition
[in]weekno1 .. 4 localtime dayno count in this month
[in]dayno0 .. 6, localtime day of DST transition, 0 = Sunday
[in]hour0 .. 23 local time hour of DST transition result is in epoch GMT seconds Example: Eastern Time and Daylight Savings time time_t epoch,start,end; tv_t tv; tv_t tz; gettimeofday(&tv, &tz); epoch = tv.tv_sec; // DST start for this year, 2nd Sunday of Mar at 2:00am EST start = find_dst(0, epoch, 0, 3, 2, 0, 2); // DST start on 2016, 2nd Sunday of Mar at 2:00am EST start = find_dst(0, 0, 2016, 3, 2, 0, 2); // DST ends on for this year, 1st Sunday of Nov at 2:00am DST end = find_dst(1, epoch, 0, 11, 1, 0, 2); // DST ends on 2016, 1st Sunday of Nov at 2:00am DST end = find_dst(1, 0, 2016, 11, 1, 0, 2);

Definition at line 1038 of file time.c.

Referenced by set_dst().

MEMSPACE int finddayofweek ( int  year,
int  month,
int  day 
)

return day of week for givenn day, month, year

Parameters
[in]yearyear such as 2016
[in]monthmonth 0 .. 11
[in]dayday 1 .. 28|29|30|31 result is day of week 0 .. 6, 0 = Sunday

Definition at line 180 of file time.c.

MEMSPACE int gettimeofday ( tv_t tv,
tz_t tz 
)

Get current time struct timeval *tv and struct timezone *tz - POSIX function. We assume a GMT hardware clock.

Parameters
[in]tvtime.
[in]tztimezone.
Returns
0

Definition at line 822 of file time.c.

Referenced by find_dst(), set_dst(), and timetests().

MEMSPACE int gettimezone ( tz_t tz)

Get current timezone in struct timezone *tz - POSIX function.

Parameters
[out]tztimezone result.
Returns
0

Definition at line 792 of file time.c.

Referenced by gettimeofday(), localtime_r(), and normalize().

MEMSPACE tm_t* gmtime ( time_t tp)

Convert epoch GMT time_t *tp into POSIX static tm_t *t.

Parameters
[in]tptime_t * time input.
Returns
tm_t t.
Warning
result is overwritten on each call.

Definition at line 471 of file time.c.

Referenced by display_clock(), ntp_setup(), rtc_init(), and unix_time_to_fat().

MEMSPACE tm_t* gmtime_r ( time_t t,
tm_t result 
)

Convert epoch GMT time_t *tp into POSIX tm_t *result.

Parameters
[in]tptime_t * time input.
[out]resulttm_t *result.
Returns
tm_t *result.

Definition at line 456 of file time.c.

Referenced by ctime_gm(), and gmtime().

MEMSPACE void initialize_clock ( int  minwest)

initialize system time - if we have an RTC use it

Parameters
[in]minwestyour time zone as minute west
Returns
void
See also
: clock_settime()

Definition at line 1178 of file time.c.

Referenced by setup().

MEMSPACE int is_dst ( time_t  epoch)

Test GMT epoch time to see if DST applies in a local timezone.

Parameters
[in]epochseconds in GMT normalize() return 1 if yes, 0 if not

Definition at line 1147 of file time.c.

Referenced by localtime_r(), and normalize().

static MEMSPACE int IS_Leap ( int  year)
static

Check if a year is a leap year.

Parameters
[in]yearvalid over 1900..2199.
Warning
: No limit checking.
Returns
1 (leap year), 0 (not a leap year(
See also
time_to_tm()
ctime_gm()
gmtime_r()
localtime_r()

Definition at line 135 of file time.c.

Referenced by Days_Per_Month(), Days_Per_Year(), time_to_tm(), and tm2epoch().

static MEMSPACE int Leap_Days_Since_1900 ( int  year)
static

Number of leap days since 1900 to the BEGINNING of the year.

Parameters
[in]yearvalid over 1900..2199.
Warning
No limit checking
Returns
days.
See also
time_to_tm()
ctime_gm()
gmtime_r()
localtime_r()

Definition at line 156 of file time.c.

Referenced by tm2epoch().

MEMSPACE tm_t* localtime ( time_t tp)

Convert POSIX epoch time_t *tp into POSIX tm_t *result.

Parameters
[in]tptime_t * epoch time input.
Returns
struct tm result.
Warning
result is overwritten on each call.

Definition at line 522 of file time.c.

Referenced by ctime(), ctime_r(), get_date(), and get_fattime().

MEMSPACE tm_t* localtime_r ( time_t t,
tm_t result 
)

Convert POSIX epoch time_t *tp into POSIX tm_t *result expressed as local time using timezone and DST corrections.

Parameters
[in]ttime_t * epoch time input.
[out]resulttm_t *result.
Returns
result.

Definition at line 499 of file time.c.

Referenced by localtime(), and timetests().

MEMSPACE time_t mktime ( tm_t t)

convert tm_t structure to time_t local time epoch

Parameters
[in]ttm_t time input.
Returns
local epoch or -1 on failure out of range

Definition at line 534 of file time.c.

Referenced by ntp_setup(), and timetests().

MEMSPACE time_t normalize ( tm_t t,
int  normalize_to_timezone 
)

Normalize POSIX tm_t *t struct and convert to epoch time Note: does not deal with DST - by design.

Parameters
[in]ttm_t time input.
[in]normalize_to_timezonenonzero = adjust to local timezone and DST
Returns
epoch since 1900 on sucess, -1 on failure out of range

Definition at line 654 of file time.c.

Referenced by asctime_r(), mktime(), and timegm().

MEMSPACE void print_dst ( void  )

print start/stop for DST as localtime for this year

Definition at line 1158 of file time.c.

Referenced by ntp_setup().

MEMSPACE void print_dst_gmt ( void  )

print start/stop for DST as GMT for this year

Definition at line 1166 of file time.c.

Referenced by ntp_setup().

MEMSPACE void set_dst ( time_t  epoch)

Set DST start and end time for the given epoch year.

Parameters
[in]0- or epoch seconds in GMT used to determin the year to aply DST in If 0 we get local GMT epoch time in seconds

Definition at line 1113 of file time.c.

Referenced by is_dst(), and ntp_setup().

MEMSPACE int setdate ( void  )

Set date and time by prompting user.

  • Prompt use for Date Time input with "YYYY MM DD HH:MM:SS>"
  • Input format is: "YYYY MM DD HH:MM:SS"
Returns
0 on success.
-1 on error>

Definition at line 901 of file time.c.

MEMSPACE int setdate_r ( char *  buf)

Set date and time from string in this format "YYYY MM DD HH:MM:SS".

Parameters
[in]bufDate string in this format "YYYY MM DD HH:MM:SS".
Returns
0 on success.
(-1) on error.

Definition at line 919 of file time.c.

Referenced by setdate(), and user_tests().

MEMSPACE int settimeofday ( tv_t tv,
tz_t tz 
)

Set current time struct timeval *tv and struct timezone *tz - POSIX function. We assume a GMT hardware clock.

Parameters
[in]tvtime.
[in]tztimezone.
Returns
0

Definition at line 861 of file time.c.

Referenced by ntp_setup().

MEMSPACE int settimezone ( tz_t tz)

Set current timezone with struct timezone *tz - POSIX function.

Parameters
[in]tztimezone result.
Returns
0.

Definition at line 806 of file time.c.

Referenced by initialize_clock(), and settimeofday().

MEMSPACE time_t time ( time_t t)

Return second from epoch - POSIX function.

Parameters
[in,out]tpointer to store time in.
  • Notes: If t is non-NULL, store the return value there also.
Returns
time_t seconds from epoch.
See also
clock_gettime().

Definition at line 843 of file time.c.

Referenced by get_date(), get_fattime(), setup(), user_loop(), user_tests(), and utime().

MEMSPACE time_t time_to_tm ( time_t  epoch,
int32_t  offset,
tm_t t 
)

Converts epoch ( seconds from 1 Jan EPOCH_YEAR UTC), offset seconds, to UNIX tm *t.

Parameters
[in]epochSeconds elapsed since January 1, EPOCH_YEAR.
  • unsigned long, range limited to: 0 .. 0xFFFD5D00>
  • The range 0xFFFEAE80 .. 0xFFFFFFFF is reserverd for Dec 31, 1969.
  • The range 0xFFFD5D00 .. 0xFFFEAE7F is reserverd of offset overflow.
[in]offsetOffset in seconds to localtime.
  • long int, range limited to +/- 86400.
  • (Number of seconds that we add to UTC to get local time).
[out]tUnix tm struct pointer output.
Returns
epoch: time in seconds, *t has result.
-1: error.
See also
ctime_gm()
gmtime_r()
localtime_r()

Definition at line 262 of file time.c.

Referenced by find_dst(), gmtime_r(), localtime_r(), and normalize().

MEMSPACE time_t timegm ( tm_t t)

Convert tm_t structure as GMT time into GMT seconds since 1900. All calculactions are in GMT regardless of timezoe settings.

  • Standards: GNU and BSD.
  • Limits: year(1900..2199).
  • Assume: epoch size is time_t;
See also
mktime() POSIX function.
timegm() POSIX function.
normalize() and tm2epoch non POSIX functions
Returns
Seconds since EPOCH_YEAR Jan 1st.
-1 on error.

Definition at line 347 of file time.c.

Referenced by display_clock(), fat_time_to_unix(), find_dst(), initialize_clock(), and setdate_r().

static MEMSPACE time_t tm2epoch ( tm_t t)
static

Converts tm_t structure as GMT time into GMT epoch since 1900.

  • used internally by normalize() function after normalization
  • DO NOT use in user code , use normaize or any function that uses it
  • Standards: none
  • Limits: year(1900..2199).
  • Assume: epoch size is time_t;
Returns
Seconds since GMT EPOCH_YEAR Jan 1st.
-1 on error.

Note: To simplify we caculate Leap Day contributions in stages

Definition at line 552 of file time.c.

Referenced by normalize().

MEMSPACE char* tm_mon_to_ascii ( int  i)

Get string Short name of Month from month number.

Parameters
[in]iMonth 0 .. 11 to string.
Returns
string pointer to month.
"BAD" on error.
See also
asctime_r()

Definition at line 114 of file time.c.

MEMSPACE char* tm_wday_to_ascii ( int  i)

Get string Short name of day from day number.

Parameters
[in]iDay 0 .. 6.
Returns
string pointer to day.
"BAD" on error.

Definition at line 87 of file time.c.

Variable Documentation

volatile ts_t __clock

System Clock Time.

Definition at line 38 of file timer.c.

const uint16_t __days[]
static
Initial value:
=
{
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
}

days in each month.

  • without leap days.
  • Index: Month 00 .. 11.
    See also
    timegm().

Definition at line 68 of file time.c.

Referenced by Days_Per_Month(), and time_to_tm().

const uint16_t __days_sum[]
static
Initial value:
=
{
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
}

accumulated days to the start of a month in a year.

  • without Leap days.
  • Index: Month 00 .. 11 (12 is a year).
    See also
    timegm().

Definition at line 59 of file time.c.

Referenced by tm2epoch().

const char* __Month[]
Initial value:
=
{
"Jan","Feb","Mar","Apr","May","Jun","Jul",
"Aug","Sep","Oct","Nov","Dec","BAD"
}

Short Name or each Month in a year.

  • Month 0 .. 11 to string.

Definition at line 100 of file time.c.

Referenced by asctime_r(), and tm_mon_to_ascii().

tz_t __tzone

System Time Zone.

Definition at line 49 of file time.c.

const char* __WDay[] = { "Sun","Mon","Tue","Wed","Thu","Fri","Sat","BAD"}

Short Name of each Day in a week.

  • Day 0 .. 6 to string.
See also
asctime_r()

Definition at line 78 of file time.c.

Referenced by asctime_r(), and tm_wday_to_ascii().

dst_t dst

DST start and stop in GMT epoch.

Definition at line 52 of file time.c.