This library takes the current time (which must be set to UTC, e.g. via an NTP call), and applies a timezone definition (loaded at startup) to calculate the local time. This includes the handling of daylight saving. See http://mbed.org/users/hlipka/notebook/time-zone-handling/ for more information (esp. how to get a time zone definition file).

Dependents:   CubiScan 000-FIN_youcef 005_ESSAI_youcef

Revision:
4:c84afcfbac84
Parent:
3:2ad51ec5ef6e
Child:
5:fde01b92a384
diff -r 2ad51ec5ef6e -r c84afcfbac84 Time.h
--- a/Time.h	Tue Jan 04 23:19:50 2011 +0000
+++ b/Time.h	Fri Jan 21 23:05:05 2011 +0000
@@ -1,154 +1,188 @@
-#ifndef __TIME_H_
-#define __TIME_H_
-
-#include <time.h>
-#include "stdio.h"
-#include "string.h"
-#include <list>
-
-class Time;
-
-/**
-    This class encapsulates a point in time - which means it will not change after it has been created.
-*/
-class TimeStamp {
-    friend class Time;
-public:
-    /**
-        @returns the year of the time stamp
-    */
-    int getYear() {
-        return _tm.tm_year+1900;
-    };
-
-    /**
-        @returns the month of the time stamp (January is 1)
-    */
-    int getMonth() {
-        return _tm.tm_mon+1;
-    };
-
-    int getDay() {
-    /**
-        @returns the day-of-the-month of the time stamp (starting with 1)
-    */
-        return _tm.tm_mday;
-    };
-
-    /**
-        @returns the hour of the time stamp (0-23)
-    */
-    int getHour() {
-        return _tm.tm_hour;
-    };
-
-    /**
-        @returns the minute of the time stamp (0-59)
-    */
-    int getMinute() {
-        return _tm.tm_min;
-    };
-
-    /**
-        @returns the second of the time stamp (0-59)
-    */
-    int getSecond() {
-        return _tm.tm_sec;
-    };
-
-    /**
-        get the day of the week - monday is 0
-        @returns the day-of-the-week of the time stamp
-    */
-    int getDayOfWeek() {
-        int dow=_tm.tm_wday;
-        dow--;
-        if (dow==-1)
-            dow=6;
-        return dow;
-    };
-
-    /**
-        @returns the day-of-the-year of the time stamp
-    */
-    int getDayOfYear() {
-        return _tm.tm_yday;
-    };
-
-    /**
-        creates a new time stamp based on the UNIX time stamp (seconds since 1970)
-    */
-    TimeStamp(time_t unixTime) {
-        updateTime(unixTime);
-    };
-
-    ~TimeStamp() {
-    };
-    
-    /**
-        @param ts the time stamp to compare to
-        @returns true when both timestamp are the same point in time
-    */
-    bool isSame(TimeStamp* ts);
-    
-    /**
-        @param ts the time stamp to compare to
-        @return true when the current time stamp is before the given time stamp
-    */
-    bool isBefore(TimeStamp* ts);
-    /**
-        @param ts the time stamp to compare to
-        @return true when the current time stamp is after the given time stamp
-    */
-    bool isAfter(TimeStamp* ts);
-private:
-    void updateTime(time_t unixTime) {
-        struct tm *time;
-        time=localtime(&unixTime);
-        memcpy(&_tm, time, sizeof(_tm));
-    }
-    TimeStamp(int year, int mon, int day, int hour, int min, int sec)
-    {
-        _tm.tm_year=year-1900;
-        _tm.tm_mon=mon-1;
-        _tm.tm_mday=day;
-        _tm.tm_hour=hour;
-        _tm.tm_min=min;
-        _tm.tm_sec=sec;
-    }
-    
-    void print()
-    {
-        printf("ts=%i.%i.%i %i:%i.%i\n",_tm.tm_mday,_tm.tm_mon+1,_tm.tm_year+1900,_tm.tm_hour,_tm.tm_min,_tm.tm_sec);
-    }
-
-    struct tm _tm;
-};
-
-class TimeZoneEntry;
-
-/**
-    This class handles the time zone calculation, and is used for creating time stamp objects.
-*/
-class Time {
-public:
-    /**
-        creates a new Time instance. On the first call, it reads the file 'timezone.csv' from the USB disk (local file system).
-    */
-    Time();
-    ~Time();
-    /**
-        creates a new TimeStamp instance. The caller is responsible for deleting it afterwards!
-        @returns the time stamp
-    */
-    TimeStamp* getTime();
-    /**
-        @returns the current time stamp (UTC) in UNIX format, without time zone correction.
-    */
-    long getUnixTime();
-private:
-    static std::list<TimeZoneEntry*> *_timeZoneEntries;
-    void readTimeZones();
-};
-
+#ifndef __TIME_H_
+#define __TIME_H_
+
+#include <time.h>
+#include "stdio.h"
+#include "string.h"
+#include <list>
+
+class Time;
+
+/**
+    This class encapsulates a point in time - which means it will not change after it has been created.
+*/
+class TimeStamp {
+    friend class Time;
+public:
+    /**
+        @returns the year of the time stamp
+    */
+    int getYear() {
+        return _year+1900;
+    };
+
+    /**
+        @returns the month of the time stamp (January is 1)
+    */
+    int getMonth() {
+        return _mon+1;
+    };
+
+    int getDay() {
+    /**
+        @returns the day-of-the-month of the time stamp (starting with 1)
+    */
+        return _mday;
+    };
+
+    /**
+        @returns the hour of the time stamp (0-23)
+    */
+    int getHour() {
+        return _hour;
+    };
+
+    /**
+        @returns the minute of the time stamp (0-59)
+    */
+    int getMinute() {
+        return _min;
+    };
+
+    /**
+        @returns the second of the time stamp (0-59)
+    */
+    int getSecond() {
+        return _sec;
+    };
+
+    /**
+        get the day of the week - monday is 0
+        @returns the day-of-the-week of the time stamp
+    */
+    int getDayOfWeek() {
+        int dow=_wday;
+        dow--;
+        if (dow==-1)
+            dow=6;
+        return dow;
+    };
+
+    /**
+        @returns the day-of-the-year of the time stamp
+    */
+    /*
+    int getDayOfYear() {
+        return _yday;
+    };
+*/
+    /**
+        creates a new time stamp based on the UNIX time stamp (seconds since 1970)
+    */
+    TimeStamp(time_t unixTime) {
+        updateTime(unixTime);
+    };
+
+    ~TimeStamp() {
+    };
+    
+    /**
+        @param ts the time stamp to compare to
+        @returns true when both timestamp are the same point in time
+    */
+    bool isSame(TimeStamp* ts);
+    
+    /**
+        @param ts the time stamp to compare to
+        @return true when the current time stamp is before the given time stamp
+    */
+    bool isBefore(TimeStamp* ts);
+    
+    /**
+        @returns time stamp transformed to ASCII (done by asctime() - but transformed to local time)
+    */ 
+    char *asChar() { struct tm * time=getTm();char *s=asctime(time); delete time;return s;}; 
+    
+    /**
+        @param ts the time stamp to compare to
+        @return true when the current time stamp is after the given time stamp
+    */
+    bool isAfter(TimeStamp* ts);
+private:
+    tm *getTm()
+    {
+        tm *time=new tm();
+        
+        time->tm_year=_year;
+        time->tm_mon=_mon;
+        time->tm_mday=_mday;
+        time->tm_hour=_hour;
+        time->tm_min=_min;
+        time->tm_sec=_sec;
+        time->tm_wday=_wday;
+        
+        return time;
+    }
+    void updateTime(time_t unixTime) {
+        struct tm *time;
+        time=localtime(&unixTime);
+        _year=time->tm_year;
+        _mon=time->tm_mon;
+        _mday=time->tm_mday;
+        _hour=time->tm_hour;
+        _min=time->tm_min;
+        _sec=time->tm_sec;
+        _wday=time->tm_wday;
+    }
+    TimeStamp(int year, int mon, int day, int hour, int min, int sec, int wday)
+    {
+        _year=(char)(year-1900);
+        _mon=(char)(mon-1);
+        _mday=(char)day;
+        _hour=(char)hour;
+        _min=(char)min;
+        _sec=(char)sec;
+        _wday=(char)wday;
+    }
+    
+    void print()
+    {
+        printf("ts=%i.%i.%i %i:%i.%i\n",_mday,_mon+1,_year+1900,_hour,_min,_sec);
+    }
+    // make sure this structure needs only 7 bytes - otherwise it's 48 bytes...
+    char __packed _year;
+    char __packed _mon;
+    char __packed _mday;
+    char __packed _hour;
+    char __packed _min;
+    char __packed _sec;
+    char __packed _wday;
+};
+
+class TimeZoneEntry;
+
+/**
+    This class handles the time zone calculation, and is used for creating time stamp objects.
+*/
+class Time {
+public:
+    /**
+        creates a new Time instance. On the first call, it reads the file 'timezone.csv' from the USB disk (local file system).
+    */
+    Time();
+    ~Time();
+    /**
+        creates a new TimeStamp instance. The caller is responsible for deleting it afterwards!
+        @returns the time stamp
+    */
+    TimeStamp* getTime();
+    /**
+        @returns the current time stamp (UTC) in UNIX format, without time zone correction.
+    */
+    long getUnixTime();
+private:
+    static TimeZoneEntry* _timeZoneEntries;
+    void readTimeZones();
+};
+
 #endif
\ No newline at end of file