C++ strptime not filling in year - c++

I'm trying to parse a string into a struct tm using strptime. Looks easy enough, but it is not filling in the year correctly (yes, I know about the 1900 offset) - after the call, the year is set to 0. Other fields look ok. See below:
struct tm tm;
memset(&tm, 0, sizeof(struct tm));
strptime("Fri Jan 17 09:44:33 UTC 2014", "%a %b %d %H:%M:%S %Z %Y", &tm);
After the strptime call, the tm struct is as follows - note that tm_year=0, where I am expecting it to be 114:
{tm_sec = 33, tm_min = 44, tm_hour = 9, tm_mday = 17, tm_mon = 0, tm_year = 0,
tm_wday = 5, tm_yday = 0, tm_isdst = 0, tm_gmtoff = 0, tm_zone = 0x0}
I'm sure I'm missing something simple - can anyone point it out to me?

On my system (Mac OS X), man strptime has this note about %Z:
The %Z format specifier only accepts time zone abbreviations of the
local time zone, or the value
"GMT". This limitation is because of ambiguity due to of the over loading of time zone abbreviations.
One such example is EST which is both Eastern Standard Time and Eastern Australia Summer Time.
And indeed, if I change your "UTC" to "GMT", the code suddenly works. Note that Python for whatever reason is smarter here, and supports "UTC" as well.

Related

How to decode a week code (ISO week date)

Is there a way on how to decode a week code or ISO week date using CTime ? I am currently creating an expiration date that is based on week code. I'm having a hard time adding the CTime and CTimeSpan (shelf_life) because the output is not accurate.
If the shelf_life is 6 months, my output is accurate. But when the shelf_life is more than a year, the output is 4 months overdue.
This is my code:
CTime tt, t11;
CString Cweek, Cyear;
int yy, mm, wknum, yrnum;
tt = CTime::GetCurrentTime();
yy = tt.GetYear();
mm = tt.GetMonth();
wknum = atoi(tt.Format("%U"));
yrnum = atoi(tt.Format("%y"));
t11 = CTime(yy, mm, 1, 0, 0, 0);
if (t11.GetDayOfWeek() == 4)
wknum = wknum ++;
Cweek.Format("%02d", wknum);
Cyear.Format("%02d", yrnum);
CTimeSpan shelf_no = CTimeSpan(atoi(view->m_pODBCPartsNo->m_shelf_life), 0, 0, 0);
CTime expiration_date = tt + shelf_no;

Expiration Date Based on Shelf_Life

I don't know what is wrong with my code. My code works if the shelf_life is over 1 year. But if the shelf_life is 6 months. It expired very short. It takes only 3 months and it is already expired. But the shelf_life is 6 months. What am I going to do ? What is lacking in my code ?
Week Code: 2138 (2021 week 38)
Shelf_life : 6 months
CTime tt, t11;
CString Cweek, Cyear;
int yy, mm, wknum, yrnum;
tt = CTime::GetCurrentTime();
yy = tt.GetYear();
mm = tt.GetMonth();
wknum = atoi(tt.Format("%U"));
yrnum = atoi(tt.Format("%y"));
t11 = CTime(yy, 1, 1, 0, 0, 0);
if (t11.GetDayOfWeek() != 1)
wknum = wknum + 1;
Cweek.Format("%01d", wknum);
Cyear.Format("%02d", yrnum);
CTimeSpan shelf_no = CTimeSpan(atoi(view->m_pODBCPartsNo->m_shelf_life), 0, 0, 0);
CTime expiration_date = t11 + shelf_no;
Week code is a date code, for example 2138, year 2021(21) week 38(38). Week 38 is around September 19, 2021 to September 25, 2021.

Conversion to UTC timezone in C++ leaves the time intact (regardless of the original timezone)

I am working with a dataset that contains the following information: date and time in the form "yyyy-mm-dd", "hh:mm". It also contains the name of the US state where the time has been measured. My goal is to unify all the time measurements so that they are all in the UTC timezone. I have written a code that doesn't work and I don't intend to go through the details of that code. However, in order to check why my code doesn't work, I have written a simple test that also doesn't work. The test is as follows:
I have tried to convert "Friday January 1 2021 9:00 AM California" to its equivalent UTC time which is "Friday January 1 2021 5:00 PM". The code snippet for conversion comes in the following:
struct tm calTM;
calTM.tm_hour = 9;
calTM.tm_min = 0;
calTM.tm_year = 2021-1900;
calTM.tm_mon = 1-1;
calTM.tm_mday = 1;
calTM.tm_isdst = -1;
setenv("TZ", "America/California", 1);
time_t calTime = mktime(&calTM);
unsetenv("TZ");
struct tm* utcTime;
utcTime = gmtime(&calTime);
cout << "UTC hh: " << utcTime->tm_hour << ", UTC mm: " << utcTime->tm_min;
cout << ", UTC year: " << utcTime->tm_year << ", UTC month: " << utcTime->tm_mon << ", UTC day: " << utcTime->tm_mday << endl;
Instead of producing the expected result (Friday January 1 2021 5:00 PM), the code snippet above produces the following result:
UTC hh: 9, UTC mm: 0, UTC year: 121, UTC month: 0, UTC day: 1
which is "month 0, day 1, year 121, 9:00 AM". Year and month are fixed after passing utcTime to the mktime() function. After adding the lines
time_t utc_time_t = mktime(utcTime);
cout << ctime(&utc_time_t) << endl;
the output is
Fri Jan 1 09:00:00 2021
Still, the time (hh:mm:ss) is wrong (exactly the same as in the original timezone). Could you please help me figure out the problem with my code? Thank you very much!

C++ how to add "text abbreviation" in separate variables inside the strftime function?

I am very new in c++. I want to setup variables in formatting date and time in strftime function in c++ how can I do it.
date = "%d";
year = "%Y";
month = "%m";
//inside the strftime function
strftime(dateX, 80, " date + year + month ", timeinfo);

isDst in Time Zone in C language

I am trying to figure out on making a time from the values i choose (dynamically)
I tried giving a date of 31st march 03:10pm and trying to go back a month with 03:10Pm which is 28th Feb 03:10Pm. But i am getting a value as 04:10Pm.
I set the isdst = -1 to do this bit; but still it fails.
Any ideas.
Tm->tm_mon = ReportMonth;
Tm->tm_mday = ReportEndDay;
Tm->tm_hour = TimeOfDay/60;
Tm->tm_min = TimeOfDay%60;
Tm->tm_sec = 59;
Tm->tm_isdst = -1;