Brandon Fictorie / Mbed 2 deprecated BF_Websocket

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Time.h Source File

Time.h

00001 #ifndef __TIME_H_
00002 #define __TIME_H_
00003 
00004 #include <time.h>
00005 #include "stdio.h"
00006 #include "string.h"
00007 #include <list>
00008 
00009 class Time;
00010 
00011 /**
00012     This class encapsulates a point in time - which means it will not change after it has been created.
00013 */
00014 class TimeStamp {
00015     friend class Time;
00016 public:
00017     /**
00018         @returns the year of the time stamp
00019     */
00020     int getYear () {
00021         return _tm.tm_year+1900;
00022     };
00023 
00024     /**
00025         @returns the month of the time stamp
00026     */
00027     int getMonth () {
00028         return _tm.tm_mon;
00029     };
00030 
00031     int getDay () {
00032     /**
00033         @returns the day-of-the-month of the time stamp
00034     */
00035         return _tm.tm_mday;
00036     };
00037 
00038     /**
00039         @returns the hour of the time stamp
00040     */
00041     int getHour () {
00042         return _tm.tm_hour;
00043     };
00044 
00045     /**
00046         @returns the minute of the time stamp
00047     */
00048     int getMinute () {
00049         return _tm.tm_min;
00050     };
00051 
00052     /**
00053         @returns the second of the time stamp
00054     */
00055     int getSecond () {
00056         return _tm.tm_sec;
00057     };
00058 
00059     /**
00060         get the day of the week - monday is 0
00061         @returns the day-of-the-week of the time stamp
00062     */
00063     int getDayOfWeek() {
00064         int dow=_tm.tm_wday;
00065         dow--;
00066         if (dow==-1)
00067             dow=6;
00068         return dow;
00069     };
00070 
00071     /**
00072         @returns the day-of-the-year of the time stamp
00073     */
00074     int getDayOfYear () {
00075         return _tm.tm_yday;
00076     };
00077 
00078     /**
00079         creates a new time stamp based on the UNIX time stamp (seconds since 1970)
00080     */
00081     TimeStamp(time_t unixTime) {
00082         updateTime(unixTime);
00083     };
00084 
00085     ~TimeStamp() {
00086     };
00087     
00088     /**
00089         @param ts the time stamp to compare to
00090         @returns true when both timestamp are the same point in time
00091     */
00092     bool isSame (TimeStamp* ts);
00093     
00094     /**
00095         @param ts the time stamp to compare to
00096         @return true when the current time stamp is before the given time stamp
00097     */
00098     bool isBefore (TimeStamp* ts);
00099     /**
00100         @param ts the time stamp to compare to
00101         @return true when the current time stamp is after the given time stamp
00102     */
00103     bool isAfter (TimeStamp* ts);
00104 private:
00105     void updateTime(time_t unixTime) {
00106         struct tm *time;
00107         time=localtime(&unixTime);
00108         memcpy(&_tm, time, sizeof(_tm));
00109     }
00110     TimeStamp(int year, int mon, int day, int hour, int min, int sec)
00111     {
00112         _tm.tm_year=year-1900;
00113         _tm.tm_mon=mon;
00114         _tm.tm_mday=day;
00115         _tm.tm_hour=hour;
00116         _tm.tm_min=min;
00117         _tm.tm_sec=sec;
00118     }
00119 
00120     struct tm _tm;
00121 };
00122 
00123 class TimeZoneEntry;
00124 
00125 /**
00126     This class handles the time zone calculation, and is used for creating time stamp objects.
00127 */
00128 class Time {
00129 public:
00130     /**
00131         creates a new Time instance. On the first call, it reads the file 'timezone.csv' from the USB disk (local file system).
00132     */
00133     Time();
00134     ~Time();
00135     /**
00136         creates a new TimeStamp instance. The caller is responsible for deleting it afterwards!
00137         @returns the time stamp
00138     */
00139     TimeStamp* getTime();
00140     /**
00141         @returns the current time stamp (UTC) in UNIX format, without time zone correction.
00142     */
00143     long getUnixTime ();
00144 private:
00145     static std::list<TimeZoneEntry*> *_timeZoneEntries;
00146     void readTimeZones();
00147 };
00148 
00149 #endif