Is it possible to increment internal time by x number of days (positive or negative) without converting it to days or seconds and then back again?
If we examine the following example of internal time format:
(apply 'encode-time (decode-time (current-time)))
We obtain a result that looks like this:
(21433 63163)
Use time-add defined in time-date.el:
(time-add my-time (days-to-time days))
Related
I want to get last working day(Weekday) of previous quarter like 2019.03.31 was Sunday, so my requirement is to get output as 2019.03.29.
I have written below code which works perfectly but seems untidy and to me it looks like kdb is eligible of providing much elegant solution than this.
{$[1<mod[dt:("d"$3 xbar "m"$.z.d)-x;7];dt;.z.s x+1]}[1]
Edit - Similarly for first week day of previous quarter, How can below code be improved:
{$[1<mod[dt:x+"d"$ -3+3 xbar "m"$.z.d;7];dt;.z.s x+1]}[0]
OR
{d:"d"$ -3+3 xbar "m"$x;$[2>r:d mod 7;d+$[0=r;2;1];d]}.z.d / Based on solution below
You could use the following:
{d:-1+"d"$3 xbar "m"$x;$[2>r:d mod 7;d-1+r;d]}.z.d
It runs slightly faster than your method
q)\ts:100000 {$[not((dt:("d"$3 xbar "m"$ .z.d)-x) mod 7) in 0 1; dt; .z.s x+1]}[1]
1031 4752
q)\ts:100000 {d:-1+"d"$3 xbar "m"$x;$[2>r:d mod 7;d-1+r;d]}.z.d
399 4976
I am working with hospital admission data, where information on admission date and discharge date is stored in clock format %tcCCYY-NN-DD_hh:MM_AM, i.e. for example
discharge date
2009-04-21 9:00 AM
So the data information is stored as milliseconds since January 1, 1960, and transforming this into a numeric double variable gives me
discharge date
1556269200000
Now, I would like to shift some of my date variables by 1 minute (just an example), and generate a new variable
gen new_discharge_date = discharge_date + 60*1000
This will only incidentally shift the discharge date by exactly one minute
In the example above this will instead give me
new_discharge_date
2009-04-25 9:00 AM
or as double
new_discharge_date
1556269236224
The difference between new_discharge_date and discharge_date is only 36224 milliseconds instead of 60000.
The problem occurs systematically, sometimes the number of milliseconds since January 1, 1960, will even be lower than before.
Any idea what I am doing wrong?
Executive summary: Adding a constant to a date-time variable with units milliseconds creates another date-time variable. Both variables should be type double.
First note that clock is not a storage format in Stata. Clock date-time variables are stored as integers; clock format is a numeric display format, which is quite different. In fact the description in the original question is backwards: the date-time data arrive as strings, which are then converted to milliseconds with the clock() function.
You are correct that clock date-times should be stored as doubles, as they are often very large integers, but for precisely that reason your shifted date-time (1 minute more than the original values) should not be stored in a float, which is what your generate does by default. You need to specify double in the generate statement. Using float instead just gives a crude approximation, which is why you observe errors. This is easy to check using your example as sandbox.
. clear
. set obs 1
number of observations (_N) was 0, now 1
. gen s_discharge_date = "2009-04-21 9:00 AM"
. gen double discharge_date = clock(s_discharge_date, "YMD hm")
. format discharge_date %tc
. gen double new_discharge_date = discharge_date + 60*1000
. format new %tc
. gen long new_discharge_date2 = discharge_date + 60*1000
. format new_discharge_date2 %tc
. list
+--------------------------------------------------------------+
1. | s_discharge_date | discharge_date | new_discharge_date |
| 2009-04-21 9:00 AM | 21apr2009 09:00:00 | 21apr2009 09:01:00 |
|--------------------------------------------------------------|
| new_di~2 |
| . |
+--------------------------------------------------------------+
The advice given in a comment to use long is wrong, as the last experiment shows immediately. Fairly recent date-times have values in trillions, some orders of magnitude larger than be could held in a long. help data types shows the limits on values in various types.
I need to create a list of days between a date interval.
Say for example from 2001-01-01 to 2009-12-31:
2001-01-01
2001-01-02
2001-01-03
..
2009-12-29
2009-12-30
2009-12-31
I know how to do it but maybe someone has a script already made?
If not, I will make such a script and upload it so others won't waste time on this when they need it.
I do not know awk from GnuWin32, but if the functions "mktime" and "strftime" are available, you can try the following code:
BEGIN {
START_DATE="2001-02-01"
END_DATE="2001-03-05"
S2=START_DATE
gsub("-"," ",S2)
T=mktime(S2 " 01 00 00")
if (T<0)
printf("%s is invalid.\n",START_DATE) >> "/dev/stderr"
else
{
for(S=START_DATE; END_DATE>S ;T+=86440) print S=strftime("%F",T)
}
}
The key is to convert the start date to a number meaning the seconds since the Epoch, add 86400 seconds (one day or 24 x 60 x 60) and convert back to the ISO date format.
After some trials I realized the mktime() function admits wrong dates as good (for instance, 2000-14-03).
Best regards
I am trying to get the current system date in Common Lisp through following function
(defun current-date-string ()
"Returns current date as a string."
(multiple-value-bind (sec min hr day mon yr dow dst-p tz)
(get-decoded-time)
(declare (ignore sec min hr dow dst-p tz))
(format nil "~A-~A-~A" yr mon day)))
Unfortunately I am getting the current date in this format "2014-1-2". However actually I need this format "2014-01-02". Is any way we can change the format? I tried replacing nil with yyyy-mm-dd but no luck. However my machine clock shows the date format is "2014-01-02".
What you need is
(format nil "~4,'0d-~2,'0d-~2,'0d" yr mon day)
~2,'0d means:
d: decimal output (instead of your generic a)
2: 1st argument: width
'0: 2nd argument: pad char 0
I suggest that you read up on Formatted Output; format is a very powerful tool.
simple-date-time is what you need, it contains some convenient function to format date and time.
I'm trying to add a date and time for an event in org-mode that will repeat on the first Wednesday of each month.
I know I can use the diary-sexp format to identify the first Wednesday of each month, like so:
* My Special Event
<%%(diary-float t 3 1)>
This works, but I also need to specify the time when that event occurs (ideally both the start and end times). I've tried both of the following:
<%%(diary-float t 3 1) 19:30>
<%%(diary-float t 3 1) 19:30-20:30>
...but neither works.
Is this even possible, and if so, what am I doing wrong?
You can use the following to get the desired effect
* My Special Event 19:30-20:30
<%%(diary-float t 3 1)>
It will output in your agenda as (Agenda trimmed):
18:00...... ----------------
filename: 19:30-20:30 My Special Event
20:00...... ----------------