create variable to hold time created - c++

Im creating a contact list program and need to beable to record when the contact was created and list the contacts according to their creation date.
what can i use to give a variable a date? i know the time.h file has something inside, but i dont know how to use it with less code as possible.
perhaps
time.h seconds = timeStamp;
?
if this is the way, then what would the output be? and whats the best way to output it in order? this variable will be part of a class.

I know you can use time_t for dates. If you need higher precision, use clock_t
You can get the current time as follows. Note that what is stored is an integer value of the number of seconds since January 1st, 1970.
#include <time.h>
....
time_t s = time(NULL);
See this for further details. Hope that helps!

#include <time.h>
...
time_t seconds = time(NULL);
The seconds variable will contain the number of seconds since 1970, which is enough information to store both date and time.
You can use the asctime function to convert this value into a human-readable string.

Related

How to convert UDate from one timezone to another using ICU

After spending some significant amount of time, I could not figure out how to convert UDate from one timezone to another. Here is the problem I am trying to solve :
I have a timestamp which is number of milliseconds since epoch. This is a timestamp in UTC. I want to convert this timestamp to a timestamp at some local time zone (e.g. US/Eastern). I want to extract number of days since epoch and number of milliseconds since from converted timestamp. I want to use icu library to do this.
I tried to create a UDate from number of milliseconds since epoch in UTC. I can create a timezone instance for the given timezone.
TimeZone *tz = TimeZone::createTimeZone("US/Eastern");
How do I convert UDate from UTC to the given timezone and extract the answers I want? Could it be done using icu?
Any help will be greatly appreciated.
I want … number of days since epoch and number of milliseconds since from converted timestamp.
I think these will be the same regardless of time zone, won't they?
Or can you give an example of the result you would like to see (before vs. after)

Print current month as calendar using current system date C++

First time poster so excuse me if I make any sort of mistake.
I am fairly new to the whole programming in C++ but I was wondering if it is possible to print out a calendar of a certain month (the current one) e.g. today it is June 2015 so I want to print out the monthly calendar of June 2015 in C++
If anyone has any suggestion how to make this possible that would be extremely helpful. I know how to post the current month with user input yet I want my program to look at the system date.
Thanks in advance.
Use time(0) to get a time_t that is also somehow the number of seconds since xxxoopsxxxxx => epoch: Wed Dec 31 19:00:00 1969, so 1/1/1970
time_t linearTime = time(0);
Use localtime_r to convert the linearTime to
struct tm timeinfo = *localtime_r(&linearTime, &timeinfo);
Use struct tm to decode broken-down-time (in Linux, at /usr/include/time.h) and produce your calendar as you want.
Have fun.
Edit 2
Looked into chrono. Figured out how to measure duration in us.
BUT, the following chrono stuff is suspiciously similar to previous stuff.
std::time_t t0 = std::time(nullptr);
std::tm* localtime(const std::time_t* t0); // std::tm has the fields you expect
// convenient to text
std::cout << std::asctime(std::localtime(&t0)); // prints the calendar info for 'now'
I suspect not much different at all.
edit 3
I have doubts. I think the previous suspiciously familiar bit came from ctime, not chrono. So I plan digging around in chrono some more.

date and time manipulation in c++

So, I want to do a class Delivery like this:
class Delivery{
private:
string recipient;
time_t date;
}
So, the date is time_t. The thing I want to do is let the user type the delivery date. Maybe the delivery is made today, maybe tomorrow, maybe next month. I could've made the date attribute string date instead of time_t. Why I didn't do that? Because, I have a list of deliveries and I want to sort the deliveries and then to print the deliveries that were made in a certain period. For example, print the deliveries made from 12.03.2013 until 25.08.2013.
The question is: how can I let the user set the date? I searched the Internet but I didn't found any useful functions. Is there a way to solve this problem?
Assuming you read the input into a string named time_string in the format 01/01/13:
struct tm tm;
strptime(time_string, "%D", &tm);
time_t t = mktime(&tm);
If you include the full year, e.g. 01/01/2013, replace strptime(time_string, "%D", &tm); with strptime(time_string, "%m/%d/%Y", &tm);. %m is the month, %d the day, and %Y the full year, e.g. 2013 instead of 13. Also note that if time_string is an std::string instead of a C-style string, you need to replace time_string with time_string.c_str() in the call to strptime.
Sources: https://stackoverflow.com/a/11213640/2097780 and http://publib.boulder.ibm.com/infocenter/zos/v1r12/index.jsp?topic=%2Fcom.ibm.zos.r12.bpxbd00%2Fstrptip.htm.
Working on date/time involves using the struct tm and time_t data structures.
To convert time_t to struct tm, there are a few differnt functions, such as localtime(), gmtime(), etc.
To convert from struct tm to time_t, you use mktime().
Obviously, you'll also need to write some code that reads year, month, day and perhaps hours and minutes from the user as integer values, then fill in a struct tm with the relevant values, and call mktime() to convert it to "seconds since 1 jan 1970" in a time_t value.
All functions to do this are declared in <ctime>
Given that you are using C++, you might want to consider using: Boost DateTime.

(Detailed) how to calculate time duration from epoch 1900 to now use boost?

As title, how to calculate time duration from epoch 1900 to now use boost?
Edit: Sorry about too short question at previous. I will describe my question again.
I have problem about save birthday to database as integer number. I have create four functions use as following:
ptime to integer (by day):
int ti=time_to_int(ptime(date(2012,1,1),0));
=> ti = 15430;//number of day from 1970 to 2012
integer to ptime:
ptime pt = int_to_time(15430);
ptime to string:
ptime pt(date(2012,1,1), 0);
string s = time_to_string(pt, "%d.%m.%Y");
string to ptime:
ptime pt = string_to_time(%d.%m.%Y);
PROBLEM:
Above, I have used epoch from 1970 and All work very well. So how can I convert 01/01/1945 to integer? I think that need use epoch from such as 1900.
However, when convert 01/01/2012 to int from 1900 it returns negative number, because "I think" tick count in miliseconds overflow (32bit).
Use some ways manual to calculate date to int is ok, but convert int back to date seems to bad. I want to use boost to do this.
Any suggestion?
Thanks!
Integer (int) is not big enough to hold the number of seconds since 1900. It can only hold about 68 years. You need to use a long (64 bit). Boost's time_duration will give you access to the number of seconds as a long. You could also use the number of days as mentioned by #jogojapan. However, day lengths are not always the same (23-25 hours), so sometimes keeping track of things in days is more prone to error (if you are careful it should be fine though).
Take a look at some examples from the documentation. You can subtract two ptime objects to get a time_duration:
time_duration td = ptime1 - ptime2;
long secElapsed = td.total_seconds();

Convert a date in a string to FILETIME

I have an imput .txt file, which contains a date. The date is in the form DDMONYY, as in 08JUN98 or 16NOV05. I need to compare this date to the creation date of the file, and see which is sooner.
FILETIME seems like the appropriate form, especially since there is a CompareFileTime function. However, I have a problem converting the date into file time.
I can extract the date from the file and convert it into numbers (days 1-31, month 1-12, year 1900-20whatever) so I don't need help with that, but feel free to comment on it anyway.
The real problem is that I can't figure out how to get these values into a FILETIME format. Right now I have them as three different strings(or int's if that helps more) one for day, month and year. If you could help me make the jump from string to FILETIME that would be great.
Or, if there is a completely better way that I missed during my research, please feel free to suggest it.
If you want, I can post what I have, but I don't think it'll help since it's only getting to the string/int's.
if it makes it any clearer, the values I have in my variables at this point, for May 3rd, 2002, would be something along the lines of
string day = "03";
string month ="5";
string year = "2002;
int dayint = 3;
int monthint = 5;
int yearint = 2002;
And I need a way to convert this to a FILETIME
Thanks a bunch.
EDIT:
I'm not sure how I would fill a SYSTEMTIME structure, or even how to do it for a different time system. So I guess that is more my question.
I should have also mentioned that I only need accuracy of days. Hours and anything smaller aren't really of concern to me.
EDIT2:
I must not have tried giving the SYSTEMTIME int values, because that seems to work. However, I am running into issues when I try to convert that systemtime to a filetime. It states "Error: no suitable converstion from "SYSTEMTIME" to "const SYSTEMTIME *" exists". I'm not sure how to fix this, since if I define the systime variable as const SYSTEMTIME I can't assign values to it.
here's the relevant code, if it helps
SYSTEMTIME systime;
LPFILETIME ftime1;
LPFILETIME ftime2;
int dayint=6;
int monthint=12;
int yearint=1989;
systime.wDay = dayint;
systime.wMonth = monthint;
//and so on for year, hour, etc.
SystemTimeToFileTime(systime,ftime1); //This is where the aforementioned error occurs
GetFileTime(filename, NULL, NULL, ftime2); //I'm also not sure about this line... feel free to critique it. I'm only interested in the last written time so I put NULL for the other times... When I check the values for this (in SYSTEMTIME) it only returns 52428, so I don't think this line is working correctly...
CompareFileTime(ftime1,ftime2);
Of course, the values for day, month, year aren't hardcoded in the actual code.
EDIT3:
I have a bad handle....
I'm going to try to fix that....
Thanks for your help Ben Voigt
If I can't get it, I guess I'll be back
You can fill in a SYSTEMTIME structure and then call SystemTimeToFileTime
You may want to use LocalFileTimeToFileTime afterwards, depending on whether you want to specify the hours in UTC or local time.