I have a unix time stamp as follows
char timestamp[100];
strcpy(timestamp,"701729943");
time_t timeval=ctime(timestamp);
printf("Time %s",timeval);
If the check the value of the timestamp in the online unix time convertor it shows 27th march 1992, but if the check the program's output it shows feb 25,1996. How to rectify this?
You're using ctime the wrong way around: it expects a pointer to a time_t and returns a string, whereas you're passing it a string and expect it to return a time_t. Does your compiler not warn you about that?
Anyway, it is meant to be used this way:
time_t timeval = 701729943;
printf("Time %s", ctime(&timeval));
If you only have the UNIX timestamp as a string, use strtoul or atoi to so to make a time_t from it, then do this.
Related
I was trying to change the access time of a file, but i didn't get the result i wanted.
this is what i tried:
struct tm time;
time.tm_sec=56;
time.tm_min=48;
time.tm_hour=20;
time.tm_mday=12;
time.tm_mon=8;
time.tm_year=1905;
struct utimbuf utime_par;
utime_par.actime=mktime(&time);
if(utime("file_name",&utime_par)!=0)
{
perror("smash error: utime failed");
std::cout<<"entered";
return;
}
when I run on linux terminal
ls -l file_name
I get
-rwxrw-rw- 1 student student 3133 Jun 20 4461763 README.txt
Does anyone know what I did wrong?
According to the documentation of the function utime, struct utimbuf (which you pass to the function utime) is defined in the following way:
struct utimbuf {
time_t actime; /* access time */
time_t modtime; /* modification time */
};
However, you are only setting the actime field of this struct, which means that the field modtime has an indeterminate value when you pass it to the function utime.
In your case, you probably want to set both fields to the same time value:
utime_par.actime = mktime( &time );
utime_par.modtime = mktime( &time );
Or, if you don't want to call the function mktime twice (which is a bit inefficient), you can also write:
utime_par.actime = mktime( &time );
utime_par.modtime = time_par.actime;
Also, as already pointed out in the comments section, the field tm_year in a struct tm should not be the absolute year, but rather the number of years since the year 1900. Therefore, it is probably wrong that you write 1905 into this field, as that corresponds to the year 3805.
Another issue is that you should set the field tm_isdst in the struct tm, to indicate whether daylight savings was in effect. You can simply set this field to a negative value, which will tell mktime that you are not providing this information, and that it should therefore determine this itself. By not setting this field, the value of this field will be indeterminate, which means that you may be providing mktime false information about whether daylight savings was in effect. This could lead to the timestamp on the file being wrong by one hour.
I think you have successfully updated the access time of your file_name, but you failed to check the result with ls.
I just copy your code, and change the access time of my file successfully.
The problem is:
ls -l file_name
ls shows the mtime — updated when the file contents change. This is the "default" file time in most cases. But I guess you have not changed the file contents of your file
What you should do is:
ls -l --time=atime your-file-name
What does --time mean? You could check its meaning by man ls
--time=WORD
change the default of using modification times; access time (-u): atime, access, use; change time (-c): ctime, status; birth time: birth, creation;
with -l, WORD determines which time to show; with --sort=time, sort by WORD (newest first)
I need to get the current system time in Unix UTC format and convert it into a CString. I am trying to do this like this
CTime time = CTime::GetCurrentTime();
CString string = time.FormatGmt(L"%d");
code is running, but 'string' variable contains a wrong value, it should contain something like '1011173512', i.e. time in seconds since 1970. Does anyone has any clue why???
The function CTime::FormatGmt uses the same formatting rules as strftime.
https://msdn.microsoft.com/en-us/library/fe06s4ak.aspx
In particular, %d means "Day of month as decimal number (01 – 31)".
I suspect that you want a time_t or similar object. I suggest you want the GetTime method, which returns a _time64_t.
https://msdn.microsoft.com/en-us/library/3s7y67z7.aspx
So:
CTime time = CTime::GetCurrentTime();
CString string; string.Format(L"%lld", time.GetTime());
I have a string like 2013-05-29T21:19:48Z. I'd like to convert it to the number of seconds since 1 January 1970 (the UNIX epoch), so that I can save it using just 4 bytes (or maybe 5 bytes, to avoid the year 2038 problem). How can I do that in a portable way? (My code has to run both on Linux and Windows.)
I can get the date parts out of the string, but I don't know how to figure out the number of seconds. I tried looking at the documentation of date and time utilities in C++, but I didn't find anything.
Here is the working code
string s{"2019-08-22T10:55:23.000Z"};
std::tm t{};
std::istringstream ss(s);
ss >> std::get_time(&t, "%Y-%m-%dT%H:%M:%S");
if (ss.fail()) {
throw std::runtime_error{"failed to parse time string"};
}
std::time_t time_stamp = mktime(&t);
use std::get_time if you want the c++ way - but both other options are also valid. strptime will ignore the Z at the end - and the T can be accomodated by format string %Y-%m-%dT%H:%M:%s - but you could also just put the Z at the end.
Take a look at strptime(). For a Windows alternative, see this question.
You could use boost date_time ore more specific ptime.
Use ptime time_from_string(std::string) to init your time and long total_seconds() to get the seconds of the duration.
I want to format a date and time as a string using the format:
20130630-03:11:45.862
I can do most of this by using strftime, however there is no clear way to achieve fractional seconds on the end.
My current code is:
time_t rawtime;
time(&rawtime);
tm* timeinfo = localtime(&rawtime);
char buffer[80];
strftime(buffer, 80, "%G%m%d-%I:%M:%S", timeinfo);
This produces the value without the fractional seconds part.
However ultimately I just want to have a string version of the date in this format, and don't care what API it takes.
I'm using g++ on Linux in case it's relevant.
If you don't care about the API, you could use boost::date_time and it's time_facet.
Short example so far:
// setup facet and zone
// this facet should result like your desired format
std::string facet="%Y%m%d-%H:%M:%s";
std::string zone="UTC+00";
// create a facet
boost::local_time::local_time_facet *time_facet;
time_facet = new boost::local_time::local_time_facet;
// create a stream and imbue the facet
std::stringstream stream(std::stringstream::in | std::stringstream::out);
stream.imbue(std::locale(stream.getloc(), time_facet));
// create zone
boost::local_time::time_zone_ptr time_zone;
time_zone.reset(new boost::local_time::posix_time_zone(zone));
// write local from calculated zone in the given facet to stream
stream << boost::local_time::local_microsec_clock::local_time(time_zone);
// now you can get the string from stream
std::string my_time = stream.str();
This example is maybe incomplete, because I copied some code out of mine, but I hope you got the point.
With the facet, you can setup your format. The %s (small s with, big S without fractial) setup seconds with fractial. You can read this in the documentation facet format.
The timezone is for calculating your local machine time to the right zone.
c++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
I am converting some source code from windows to running on ubuntu.
However, the windows is using the code below to log messages and get the current time the message is logged. However, this is windows. What is the best way to convert this to running on linux?
struct _timeb timebuffer;
char *timeline;
_ftime(&timebuffer);
timeline = ctime(&(timebuffer.time));
Many thanks for any advice,
in linux a similar function ftime is used which gives you the time do
#include <sys/timeb.h>
/* .... */
struct timeb tp;
ftime(&tp);
printf("time = %ld.%d\n",tp.time,tp.millitm);
this will give you the time in second and milliseconds.
The windows code is calling ctime, which is available on Linux too and prepares a date-time string such as:
"Wed Jun 30 21:49:08 1993\n"
The man page for ctime() on Linux documents:
char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);
So, you don't want to use ftime() on linux... that populates a struct timeb buffer which isn't accepted by ctime().
Instead, you can get/use a time_t value using the time() function as in:
#include <time.h>
time_t my_time_t;
time(&my_time_t);
if (my_time_t == -1)
// error...
char buffer[26]; // yes - a magic number - see the man page
if (ctime_r(&my_time_t, buffer) == NULL)
// error...
// textual representation of current date/time in buffer...
Use Boost libraries (datetime) as your questions look to be more about date/time. Most of the boost lib is portable. If you look for GUI libraries QT is the best one which is again portable b/w windows/*nix.