change ctime output

Team,

Working with ctime - and the output is this.

Www Mmm dd hh:mm:ss yyyy

however - excel and libreoffice have difficulties identifying it as time -
is there anyway to change the way ctime outputs -

like mm/dd/yy hh:mm:ss ?

1
2
3
4
5
6
7
8
9
10
11
12
13
  /* ctime example */
#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, time, ctime */

int main ()
{
  time_t rawtime;

  time (&rawtime);
  printf ("The current local time is: %s", ctime (&rawtime));

  return 0;
}
and the output is this.

Www Mmm dd hh:mm:ss yyyy


it is the std::string resulting from std::ctime that has the above format:
http://en.cppreference.com/w/cpp/chrono/c/ctime

so your problem is now simply transforming this std::string into any other format that you wish:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# include <iostream>
# include <string>
# include <sstream>

int main()
{
   std::string ctimeString = "Www Mmm dd hh:mm:ss yyyy";
   std::istringstream stream{ctimeString};
   std::string dayOfWeek{}, month{}, day{}, hrsMinSec{}, year{};
   std::string excelString{};
   while (stream)
   {
       stream >> dayOfWeek >> month >> day >> hrsMinSec >> year;
       if(stream)
       {
           excelString = month + "/" + day + "/" + year + " " + hrsMinSec;
       }
   }
   std::cout << excelString << "\n";
}
POSIX marks this function obsolete and recommends std::strftime instead.

this is from the std::ctime() link sent earlier and going through that link it does appear that std::strftime() offers additional functionalities that allow the user to determine the format of the output right at the stage of the function call rather than manipulating the function's return values as was the case with std::ctime().
for e.g. you could get your desired output format straight-off in the following manner:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <chrono>
#include <ctime>
#include <string>

inline
std::string asString (const std::chrono::system_clock::time_point& tp)
{
    // convert to system time:
    std::time_t t = std::chrono::system_clock::to_time_t(tp);
    std::string ts{};
    char mbstr[100];
    if(std::strftime(mbstr, sizeof(mbstr), "%c", std::localtime(&t)))
    {
        ts = mbstr;
    }
    return ts;
}

// convert calendar time to timepoint of system clock
inline
std::chrono::system_clock::time_point
makeTimePoint (int year, int mon, int day,
               int hour, int min, int sec=0)
{
    struct std::tm t;
    t.tm_sec = sec;        // second of minute (0 .. 59 and 60 for leap seconds)
    t.tm_min = min;        // minute of hour (0 .. 59)
    t.tm_hour = hour;      // hour of day (0 .. 23)
    t.tm_mday = day;       // day of month (1 .. 31)
    t.tm_mon = mon-1;      // month of year (0 .. 11)
    t.tm_year = year-1900; // year since 1900
    t.tm_isdst = -1;       // determine whether daylight saving time
    std::time_t tt = std::mktime(&t);
    if (tt == -1) {
        throw "no valid system time";
    }
    return std::chrono::system_clock::from_time_t(tt);
}

int main()
{
    auto my_tp = makeTimePoint(2012, 5, 4, 7, 42);//2012 4th May 7:42

    std::cout << asString(my_tp);
}

//http://en.cppreference.com/w/cpp/chrono/c/strftime
gunnerfunner - you are the MAN!
Thanks again!
Topic archived. No new replies allowed.