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

Common Linux/POSIX time functions. More...

Go to the source code of this file.

Data Structures

struct  tm
 POSIX struct tm. More...
 
struct  timeval
 POSIX timeval. More...
 
struct  timezone
 POSIX timezone. More...
 
struct  timespec
 POSIX timespec. More...
 
struct  dst_t
 DST structure. More...
 

Macros

#define EPOCH_YEAR   1970 /*< Thursday Jan 1 1970 */
 
#define EPOCH_DAY   4 /*< Sunday = 0 ... Saturday = 6 */
 
#define EPOCH   0 /*< Zero seconds */
 
#define EPOCH_2000   946684800 /*< Sat, 01 Jan 2000 00:00:00 GMT */
 

Typedefs

typedef uint32_t time_t
 type of EPOCH result. More...
 
typedef uint16_t clockid_t
 type of clockid_t. More...
 
typedef struct tm tm_t
 POSIX struct tm typedef. More...
 
typedef struct timeval tv_t
 POSIX timeval typedef. More...
 
typedef struct timezone tz_t
 POSIX timezone typedef. More...
 
typedef struct timespec ts_t
 POSIX timespec typedef. More...
 

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...
 
MEMSPACE int finddayofweek (int year, int month, int day)
 return day of week for givenn day, month, 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...
 
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 (void)
 print start/stop for DST as localtime for this year More...
 
MEMSPACE void print_dst_gmt (void)
 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 (void)
 Display system time and optionally RTC time. More...
 

Variables

volatile ts_t __clock
 System Clock Time. More...
 
tz_t __tzone
 System Time Zone. 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.h.

Macro Definition Documentation

#define EPOCH   0 /*< Zero seconds */

Definition at line 31 of file time.h.

#define EPOCH_2000   946684800 /*< Sat, 01 Jan 2000 00:00:00 GMT */

Definition at line 32 of file time.h.

#define EPOCH_DAY   4 /*< Sunday = 0 ... Saturday = 6 */

Definition at line 30 of file time.h.

Referenced by time_to_tm().

#define EPOCH_YEAR   1970 /*< Thursday Jan 1 1970 */

Definition at line 29 of file time.h.

Referenced by time_to_tm(), timetests(), and tm2epoch().

Typedef Documentation

type of clockid_t.

Definition at line 38 of file time.h.

typedef uint32_t time_t

type of EPOCH result.

Definition at line 35 of file time.h.

typedef struct tm tm_t

POSIX struct tm typedef.

tm_isdst value notes 1 DST in effect - add an hour 0 DST not in effect -1 DST Unknown status

Definition at line 64 of file time.h.

typedef struct timespec ts_t

POSIX timespec typedef.

Definition at line 95 of file time.h.

typedef struct timeval tv_t

POSIX timeval typedef.

Definition at line 75 of file time.h.

typedef struct timezone tz_t

POSIX timezone typedef.

Definition at line 85 of file time.h.

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().

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().

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().

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.

tz_t __tzone

System Time Zone.

Definition at line 49 of file time.c.