function UNIX_TIMESTAMP does not exist - django

I am trying to convert my datetime to unix timestamp. I've tried doing several ways and the error is all the same. I am not very sure what I am suppose to do.
date_joined is like this 2017-09-30 10:24:44.954981+00:00
function unix_timestamp(timestamp with time zone) does not exist
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
User.objects.annotate(photo_time=Func(F('date_joined'),function='UNIX_TIMESTAMP'))
User.objects.extra(select={'photo_time':"to_unixtime(date_joined)"})
#also tried and UNIX_TIMESTAMP

That's because Postgres won't let you do that (see here). If you really don't need the UNIX_TIMESTAMP, you need Extract
User.objects.annotate(photo_time=Extract('date_joined', 'epoch').get())
Of course, you could also define a TO_UNIXTIME stored procedure/function, but that seems a bit over the top.

Related

Django and PostgreSQL - how to store time range?

I would like to store a time range (without dates) like "HH:MM-HH:MM".
Can you give me a hint, how can I implement it most easily? Maybe there is a way to use DateRangeField for this aim or something else.
Thanks for your spend time!
Time without date doesn't make much since if you ever need a range that span mid-night (days) You could always convert to text using to_char(<yourtimestamp>,'hh24.mi:ss') or extract the individual parts. Unfortunately Postgres does not provide an extract(time from <yourtimestamp>) function. The following function provides essentially that.
create or replace
function extract_tod(date_time_in timestamp without time zone)
returns time
language sql
as $$
select to_char(date_time_in, 'hh24:mi:ss')::time;
$$;
See here for timestatamp ranges and here for their associated functions. As for how to store then just store with the date
as a standard TIMESTAMP (date + time).

Comparing local csvs to write updates, list(set()) issue

I have a funny problem. I can get the unique rows in the two comparison csv files by using:
update = list(set(compareNew) - set(compareOld))
However, when I include this line in my function I get TypeError:'list' object is not callable.
I need to do this for a few hundred csv's so I am calling the above line in a loop. Does that change anything for the function?
Using python 3.4
I was able to fix this by subtracting the two sets, then appending the difference to a list in a subloop.
Makes it a two-step process, but it appears to work

Stata : generate/replace alternatives?

I use Stata since several years now, along with other languages like R.
Stata is great, but there is one thing that annoys me : the generate/replace behaviour, and especially the "... already defined" error.
It means that if we want to run a piece of code twice, if this piece of code contains the definition of a variable, this definition needs 2 lines :
capture drop foo
generate foo = ...
While it takes just one line in other languages such as R.
So is there another way to define variables that combines "generate" and "replace" in one command ?
I am unaware of any way to do this directly. Further, as #Roberto's comment implies, there are reasons simply issuing a generate command will not overwrite (see: replace) the contents of a variable.
To be able to do this while maintaining data integrity, you would need to issue two separate commands as your question points out (explicitly dropping the existing variable before generating the new one) - I see this as method in which Stata forces the user to be clear about his/her intentions.
It might be noted that Stata is not alone in this regard. SQL Server, for example, requires the user drop an existing table before creating a table with the same name (in the same database), does not allow multiple columns with the same name in a table, etc. and all for good reason.
However, if you are really set on being able to issue a one-liner in Stata to do what you desire, you could write a very simple program. The following should get you started:
program mkvar
version 13
syntax anything=exp [if] [in]
capture confirm variable `anything'
if !_rc {
drop `anything'
}
generate `anything' `exp' `if' `in'
end
You then would naturally save the program to mkvar.ado in a directory that Stata would find (i.e., C:\ado\personal\ on Windows. If you are unsure, type sysdir), and call it using:
mkvar newvar=expression [if] [in]
Now, I haven't tested the above code much so you may have to do a bit of de-bugging, but it has worked fine in the examples I've tried.
On a closing note, I'd advise you to exercise caution when doing this - certainly you will want to be vigilant with regard to altering your data, retain a copy of your raw data while a do file manipulates the data in memory, etc.

User written formats and comparison/logical operators in SAS

I am wondering if there is a way to perform operations on formatted values of variables with user written formats. For example, I want to compare the variable food1 with the variable food2 (both have user written formats). I want to do something like this:
if food1='ice cream' and food2='pie' then ...;
This is easy enough, though I am not sure the proper way to compare these variables if 'ice cream' and 'food' are the user written format values. Lets say 'ice cream' is actually 'A' and 'pie' is actually 'B'. Is there a way to do this comparison without removing the format or making new variables or using the actual values?
If you're using the data step (and not PROC SQL or similar), you can use VVALUE:
if vvalue(food1)='ice cream' and vvalue(food2)='pie' then ...
This accesses the currently defined formatted value (based on the format currently defined for the variable, which could change during the data step!). This does not require knowing what that format is.
VVALUEX is similar, but takes a character argument for the variable name (so if you don't know the variable name you want to evaluate, that's the right way to go).
This can be done the put() function. Replace "format1" and "format2" below with the name(s) of your user-written format(s).
if put(food1,format1.) ='ice cream' and put(food2,format2.) ='pie' then ...;

understanding print option in rrdtool

Instead of creating a graph, I need to simply output a number that is the average, max or min of some date range supplied. I have had good success with the following code:
rrdtool graph a.png --start=1325484000 --end=1364472365 DEF:power=/data1/bpoll/rrd/ws3/pdu/pdu316/a.rrd:ct12:AVERAGE 'PRINT:power:AVERAGE:%2.1lf'
However, looking at the docu, it says specifying the CF (in this case AVERAGE) is deprecated. Yet I am completely lost as to the new format. At least I can't seem to wrap my head around it. If I leave out the CF, it errors. Where exactly am I going wrong here?
PRINT:power:AVERAGE:%2.1lf
This is the 'old style' syntax, where you pass a dataset and the consolodation function to the PRINT directive.
With the new format, you use a VDEF and so do not need a function as a VDEF is single-valued. However, you need to define the VDEF beforehand.
This is the new format:
VDEF:avgpower:power,AVERAGE
PRINT:avgpower:%2.1lf
In this example, we define a new VDEF value avgpower and print that. It has the same effect as the previous old syntax code, but is in the new sytax, which allows us to also add modifiers to the PRINT statement such as :strftime to print the point in time of maxima, and so on.