source: box/trunk/lib/common/BoxTimeToText.cpp @ 1036

Revision 1036, 1.6 KB checked in by chris, 6 years ago (diff)
  • Add option to bbackupquery list command to show times in UTC or local time (refs #3)
  • Property svn:eol-style set to native
Line 
1// --------------------------------------------------------------------------
2//
3// File
4//              Name:    BoxTimeToText.cpp
5//              Purpose: Convert box time to text
6//              Created: 2003/10/10
7//
8// --------------------------------------------------------------------------
9
10#include "Box.h"
11
12#include <sys/types.h>
13#include <time.h>
14#include <stdio.h>
15
16#include "BoxTimeToText.h"
17
18#include "MemLeakFindOn.h"
19
20// --------------------------------------------------------------------------
21//
22// Function
23//              Name:    BoxTimeToISO8601String(box_time_t, bool)
24//              Purpose: Convert a 64 bit box time to a ISO 8601 compliant
25//                      string, either in local or UTC time
26//              Created: 2003/10/10
27//
28// --------------------------------------------------------------------------
29std::string BoxTimeToISO8601String(box_time_t Time, bool localTime)
30{
31        time_t timeInSecs = BoxTimeToSeconds(Time);
32        char str[128];  // more than enough space
33
34#ifdef WIN32
35        struct tm *time;
36        __time64_t winTime = timeInSecs;
37
38        if(localTime)
39        {
40                time = _localtime64(&winTime);
41        }
42        else
43        {
44                time = _gmtime64(&winTime);
45        }
46
47        if(time == NULL)
48        {
49                // ::sprintf(str, "%016I64x ", bob);
50                return std::string("unable to convert time");
51        }
52       
53        sprintf(str, "%04d-%02d-%02dT%02d:%02d:%02d", time->tm_year + 1900,
54                time->tm_mon + 1, time->tm_mday, time->tm_hour, 
55                time->tm_min, time->tm_sec);
56#else // ! WIN32
57        struct tm time;
58
59        if(localTime)
60        {
61                localtime_r(&timeInSecs, &time);
62        }
63        else
64        {
65                gmtime_r(&timeInSecs, &time);
66        }
67       
68        sprintf(str, "%04d-%02d-%02dT%02d:%02d:%02d", time.tm_year + 1900,
69                time.tm_mon + 1, time.tm_mday, time.tm_hour, 
70                time.tm_min, time.tm_sec);
71#endif // WIN32
72       
73        return std::string(str);
74}
75
76
Note: See TracBrowser for help on using the repository browser.