How to add chrono::year_month_day to chrono::sys_seconds - c++

Am holding duration's in a year_month_day. Is there a simple way to add the year_month_day duration to a time_point like sys_seconds?
sys_seconds date1 = {};
sys_seconds dat2 = {};
year_month_day calDuration;
date1 = date2 + calDuration; //error: no match for ‘operator+’

This is chrono catching logic bugs for you at compile-time. Adding date2 + calDuration is akin to adding tomorrow + today. It just doesn't make sense. And that's why it is a compile-time error.
What you may mean is that you have durations years, months and days. This is not the same as the similarly named types year, month and day. The plural forms are chrono::durations, just like minutes and nanoseconds. days is 24h. And years and months are the average length of those units in the civil calendar.
Conversely, the singular forms year, month and day are the calendrical components that give a name to a day in the civil calendar, e.g. 2020y, December, and 18d. And it is these singular forms that make up a year_month_day, e.g. 2020y/December/18d.
See this SO answer for a deep-dive on the difference between month and months.
There are multiple ways to add the units years and months to a time_point. See this SO answer for a deep-dive on that topic.

Related

How to create a YTD using fiscal year / financial year

Apologises if this question has already been raised but I'm struggling with my data model to create a rolling total / YTD sales using fiscal years.
I know PowerBI/DAX is great using calendar Jan-Dec dates but how do you amend this if your financial year begins in March-April instead?
When I created my calendar table, I used the CALENDEARAUTO() which gave me a column of dates from Jan to Dec as expected and when I change the function to CALENDERAUTO(3), my date column begins from March to April which makes sense.
However, if I use the CALCULATE and DATESYTD functions to sum the total sales column and choose the date column as described above, the result looks like:
Actual YTD = CALCULATE(sum('spend'[Actual]),DATESYTD('Fiscal Year Table'[Date],"03-31"))
What do I need to do exactly for it work in this exact date format?
Many thanks
Hash

Output first day of year

I have various needs for time calculations, but can't get the basic syntax right. The measure
First Day of Year = STARTOFYEAR('Calendar'[Date])
returns 1/1/2019, which is the earliest date on my calendar table. Today's date being Jun 2022, I want it to return 1/1/2022.
Related question: In order to return the first day of the fiscal year (October 1 of the prior year) I would expect something like
First Day of Fiscal Year = STARTOFYEAR(DATEADD('Calendar'[Date],-3,MONTH))
But this returns the same result, 1/1/2019. Desired result is 10/1/2021
If you want it reference this (the current) year, you need the TODAY() function.
eg:
First Day of Year = date(year(TODAY()),1,1)
First Day of Fiscal Year = date(year(today())-1,10,1)
if you want to get the first Date of the year of a table containing more Years you have to filter the date Table:
CALCULATETABLE(STARTOFYEAR('Calendar'[Date]), 'Calendar'[Year]=year(today()))

Stata: How to modify some values in a string variable but keep original values?

I am working with a very large dataset (1 million obs.).
I have a string date that looks like this
key seq startdate (string)
AD07 1 August 2011
AD07 2 June 2011
AD07 3 February 2004
AD07 4 November 2004
AD07 5 2001
AD07 6 January 1998
AD5c23 1 January 2014
AD5c235 2 February 2014
AD5c235 3 2014
These are self-reported employment dates.
Some did not report the month at which they started.
But I would like to replace for AD07 the date “2001” to “January 2001”. Hence I cannot simply replace it because I would like to keep the original years but add the month in the string variable.
I started with:
levelsof start if start<="2016", local(levels)
which gives me all the years without the month from 1900 to 2016.
Now I would like to add "January" for the years without the month and keep original years.
How should I do that without using replace for every year? foreach loop?
You have a serious data quality problem if people are claiming to have started work in 1900 and every year since then! Even considering early employment starts and delayed retirement, that implies people older than the oldest established age.
Also, imputing "January" will impart bias as almost all job durations will be longer than they would have been. Real January starts will be correct, but no others: "June" or "July" or random months would make more obvious statistical sense.
That said, there is no loop needed here. You're asking for one line, say
replace startdate = "January " + startdate if length(trim(date)) == 4
or
replace startdate = "January " + startdate if real(startdate) < .
-- assuming a follow-up in converting to numeric dates. The logic there is that all year-only dates trim down to 4 characters, or (better) that feeding month names to real() will yield missings.
That said in turn, creating a new variable is better practice than over-writing one. Also, consider throwing away the month detail. Is it needed?
EDIT
You may have another problem if there are people with two or more jobs in the same year without month specifications. You don't want to impute all months in question as "January". You can check for such observations by
gen byte incomplete = real(startdate) < .
gen year = substr(trim(startdate), -4, 4)
bysort key year incomplete : gen byte multiplebad = incomplete & _N > 1

how to get the next day date in informaitca

I have a date field coming form source paid_date and i want it to convert as
trunc(next_day(sysdate-1,'MON')).. I need to get the NEXT_DAY here and the filed data type is date-time.
Please share your inputs.
Unfortunately, as of now, there is no NEXT_DAY equivalent in Informatica. So you have to calculate it like this in expression.
TRUNC(
ADD_TO_DATE(
SYSDATE,
'DD',
(9 - TO_FLOAT(TO_CHAR(SYSDATE,'D')))%7
)
)
Explanation:
(9 - TO_FLOAT(TO_CHAR(SYSDATE,'D')))%7 - Calculates the number of days till next Monday.
ADD_TO_DATE(SYSDATE,'DD',...) - Adds the above no. of days to the input date
In this case you can use Add_To_Date function. Using this function you can get your exact date or month or year.
Formats of defining date,
Date – DD, DDD, DY and DAY
Month – MM, MON and MONTH
Year – YY, YYY and YYYY
Hour – HH, HH12 and HH24
Minute – MI
Seconds - SS
Syntax : ADD_TO_DATE (date_column, format, value)
Example: ADD_TO_DATE (Date, ‘DD’, 10)
Result:
10/01/2016 - 20/01/2016
As the format is provided as ‘DD’ and value as 10, the dates are displayed by increasing 10 days. This logics stands for date, month, year, minute, hour or seconds whatever defined in the syntax. To decrease the date value just add negative number (-10).
For you to get next day, just define
ADD_TO_DATE (Your column, ‘DD’, 1).
For more details on informatica just visit my blog,
http://etlinfromatica.wordpress.com/

How to destring a date in Stata containing just the year?

I have a string variable in Stata called YEAR with format "aaaa" (e.g. 2011). I want to replace "aaaa" with "31decaaaa" and destring the obtained variable.
My feeling is that the best way to proceed could be firstly destringing the variable YEAR and then adding "31dec". To destring the variable YEAR I have tried the command date but it does not seem to work. Any suggestion?
It would be best to describe your eventual goal here, as use of destring just appears to be what you have in mind as the next step.
If your goal is, given a string variable year, to produce a daily date variable for 31 December in each year, then destring is not necessary. Here are three ways to do it:
gen date = daily("31 Dec" + year, "DMY")
gen date = date("31 Dec" + year, "DMY")
gen date = mdy(12, 31, real(year))
Incidentally, there is no likely gain for Stata use in daily dates 365 or 366 days apart, as they just create a time series that is mostly implicit gaps.
If your data are yearly, but just associated with the end of each calendar year, keep them as yearly and use a display format to show "31 Dec", or the equivalent, in output.
. di %ty!3!1_!D!e!c_CCYY 2015
31 Dec 2015
Detail. date() is a function, not a command, in Stata. We can't comment on "does not seem to work" as no details are given of what you tried or what happened. daily() is just a synonym for date().