]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- same date as time_t
authorArvin Schnell <aschnell@suse.de>
Mon, 7 Feb 2011 13:37:59 +0000 (14:37 +0100)
committerArvin Schnell <aschnell@suse.de>
Mon, 7 Feb 2011 13:37:59 +0000 (14:37 +0100)
snapper/AppUtil.cc
snapper/AppUtil.h
snapper/Snapshot.cc
snapper/Snapshot.h
tools/snapper.cc

index 7dde7a3c0622c765cc3092729435acfa6dcc403b..40242081550fbc70b79cb1469f7a0fad12ce8a27 100644 (file)
@@ -392,18 +392,29 @@ readlink(const string& path, string& buf)
 
 
     string
-    datetime()
+    datetime(time_t t1, bool utc, bool classic)
     {
-       time_t t1 = time(NULL);
        struct tm t2;
-       gmtime_r(&t1, &t2);
+       utc ? gmtime_r(&t1, &t2) : localtime_r(&t1, &t2);
        char buf[64 + 1];
-       if (strftime(buf, sizeof(buf), "%F %T %Z", &t2) == 0)
+       if (strftime(buf, sizeof(buf), classic ? "%F %T" : "%c", &t2) == 0)
            return string("unknown");
        return string(buf);
     }
 
 
+    time_t
+    scan_datetime(const string& str, bool utc)
+    {
+       struct tm s;
+       memset(&s, 0, sizeof(s));
+       const char* p = strptime(str.c_str(), "%F %T", &s);
+       if (!p || *p != '\0')
+           return (time_t)(-1);
+       return utc ? timegm(&s) : timelocal(&s);
+    }
+
+
     StopWatch::StopWatch()
     {
        gettimeofday(&start_tv, NULL);
index 53b7744838a719d5c3438377e1b76323a8986216..8f079a70bcfde220600c86ab8195def1d923e350 100644 (file)
@@ -89,7 +89,9 @@ void initDefaultLogger();
 
 
     string hostname();
-    string datetime();
+
+    string datetime(time_t time, bool utc, bool classic);
+    time_t scan_datetime(const string& str, bool utc);
 
 
     class StopWatch
index db6ed8d8650ef33210eefbe852c41b5e24b38de2..8b406eebeb4618b30c6347631086720ebe8c7b44 100644 (file)
@@ -24,6 +24,7 @@
 #include <sys/types.h>
 #include <glob.h>
 #include <string.h>
+#include <boost/algorithm/string.hpp>
 
 #include "snapper/Snapshot.h"
 #include "snapper/AppUtil.h"
@@ -46,7 +47,7 @@ namespace snapper
        if (snapshot.pre_num != 0)
            s << " pre-num:" << snapshot.pre_num;
 
-       s << " date:\"" << snapshot.date << "\"";
+       s << " date:\"" << datetime(snapshot.date, true, true) << "\"";
 
        if (!snapshot.description.empty())
            s << " description:\"" << snapshot.description << "\"";
@@ -101,7 +102,16 @@ namespace snapper
            getChildValue(node, "num", snapshot.num);
            assert(num == snapshot.num);
 
-           getChildValue(node, "date", snapshot.date);
+           if (getChildValue(node, "date", tmp))
+           {
+               // TODO: remove someday
+               if (boost::ends_with(tmp, " GMT"))
+                   tmp.erase(19);
+
+               assert(tmp.size() == 19);
+
+               snapshot.date = scan_datetime(tmp, true);
+           }
 
            getChildValue(node, "description", snapshot.description);
 
@@ -130,7 +140,7 @@ namespace snapper
        Snapshot snapshot;
        snapshot.type = SINGLE;
        snapshot.num = 0;
-       snapshot.date = "now";
+       snapshot.date = (time_t)(-1);
        snapshot.description = "current";
        entries.push_back(snapshot);
 
@@ -175,7 +185,7 @@ namespace snapper
 
        setChildValue(node, "num", num);
 
-       setChildValue(node, "date", date);
+       setChildValue(node, "date", datetime(date, true, true));
 
        if (type == SINGLE || type == PRE)
            setChildValue(node, "description", description);
@@ -203,7 +213,7 @@ namespace snapper
        Snapshot snapshot;
        snapshot.type = SINGLE;
        snapshot.num = nextNumber();
-       snapshot.date = datetime();
+       snapshot.date = time(NULL);
        snapshot.description = description;
 
        snapshot.writeInfo();
@@ -219,7 +229,7 @@ namespace snapper
        Snapshot snapshot;
        snapshot.type = PRE;
        snapshot.num = nextNumber();
-       snapshot.date = datetime();
+       snapshot.date = time(NULL);
        snapshot.description = description;
 
        snapshot.writeInfo();
@@ -235,7 +245,7 @@ namespace snapper
        Snapshot snapshot;
        snapshot.type = POST;
        snapshot.num = nextNumber();
-       snapshot.date = datetime();
+       snapshot.date = time(NULL);
        snapshot.pre_num = pre->getNum();
 
        snapshot.writeInfo();
index b3943d82dd8d3f42d38cd428cdab34b75fd395d3..3b2e10e35b8c02d3153a48b8e359c1fdcd50d454 100644 (file)
@@ -24,6 +24,8 @@
 #define SNAPSHOT_H
 
 
+#include <time.h>
+
 #include <string>
 #include <list>
 
@@ -50,7 +52,7 @@ namespace snapper
        unsigned int getNum() const { return num; }
        bool isCurrent() const { return num == 0; }
 
-       string getDate() const { return date; }
+       time_t getDate() const { return date; }
 
        string getDescription() const { return description; }
 
@@ -67,7 +69,7 @@ namespace snapper
 
        unsigned int num;
 
-       string date;
+       time_t date;
 
        string description;     // empty for type=POST
 
index 43b03a1f9df7c3d1e61603549dd11864af5f36aa..096b78c6aa27f4585792d49b3c890fe14c88f901 100644 (file)
@@ -62,7 +62,7 @@ listSnap( const list<string>& args )
        row.add(toString(it->getType()));
        row.add(decString(it->getNum()));
        row.add(it->getType() == POST ? decString(it->getPreNum()) : "");
-       row.add(it->getDate());
+       row.add(it->isCurrent() ? "" : datetime(it->getDate(), false, false));
        row.add(it->getDescription());
        table.add(row);
     }
@@ -228,6 +228,8 @@ doRollback( const list<string>& args )
 int
 main(int argc, char** argv)
     {
+    setlocale (LC_ALL, "");
+
     list<string> args;
     initDefaultLogger();
     y2mil( "argc:" << argc );