Sqlite couldn't show the correct current time - iphone

I have set my time as US Locale.I have made database in Sqlite.In database i have fields like Entry_Date which set as CURRENT_TIMESTAMP..Now i run query like
SELECT time('now')
o/p: 06:22:09
It display wrong time.so my question is How do i set the Sqlite time?

You need to pass the localtime modifier to the time function.
sqlite> select time('now','localtime');
06:38:51
See Date and Time Functions
Update: For the record, here's how to output a unix epoch date:
sqlite> select time(1316638370,'unixepoch','localtime');
06:52:50

Related

Estimate elapsed time in Db2

I wanted to calculate the elapsed time of a script in DB2 LUW .
I need to wright the code to get start time and end time then return the difference.
select current timestamp as startdate from sysibm.sysdummy1;
-- my querys
select current timestamp as enddate from sysibm.sysdummy1;
select timestampdiff (enddate , startdate);
Firstly, you are using timestampdiff() incorrectly -- please check the manual.
Secondly, you cannot select from nothing in Db2; you seem to know how to use sysibm.sysdummy1, so apply the same technique to your elapsed time calculation. Alternatively, you could use the values statement.
But worst of all, you don't save the result of your select current timestamp... queries anywhere, so you can't reference them later.
You could do something like this if you don't want to write SQL/PL code:
create table t (starttime timestamp, endtime timestamp);
-- you could also declare a global temporary table instead
insert into t (starttime) values (current timestamp);
-- your statements
update t set endtime=current timestamp;
select timestampdiff(1, char(endtime-starttime)) as elapsed_microseconds from t;
drop table t; -- if it's not a temp table

PG SQL 10 set default time zone to UTC

I need to change the database config to use UTC as the default for a PostgreSQL10 instance hosted on AWS RDS. I want it permanently changed at the database level and to never revert back to any other timezone no matter what.
I've tried running this, but it shows 0 updated rows:
ALTER DATABASE <my-db> SET timezone='UTC';
I've tried attaching a custom param group to the DB in RDS and modifying the entries like so (also rebooted after):
No matter what I do, when I run select * from pg_settings where name = 'TimeZone'; or SHOW timezone it shows 'America/Chicago'.
It seems like this should be easy to do, but it is proving to be a challenge.
If you want to store your timestamps in UTC and always want the database to send the data to the client in UTC as well, you should use the data type timestamp without time zone, which will not perform any time zone handling for you. That would be the simplest solution.
To convert the data, you could proceed like this:
SET timezone = 'UTC';
ALTER TABLE mytable ALTER timestampcol TYPE timestamp without time zone;
Based on this dba.stackexchange.com question. Apparently PG stores the timestamp in UTC time but then converts it to the session time zone. From what I've gathered, since my timestamps don't include time zone information, I need to tell PG that the timezone being stored is UTC, and then convert that to whatever local time is needed, so something like this:
SELECT my_timestamp_in_utc AT TIME ZONE 'UTC' AT TIME ZONE 'America/Denver' as my_local_time
FROM my_table;
This is a little verbose, but I'll go with it for now.

PostgreSQL: Insert Date and Time of day in the same field/box?

After performing the following:
INSERT INTO times_table (start_time, end_time) VALUES (to_date('2/3/2016 12:05',
'MM/DD/YYYY HH24:MI'), to_date('2/3/2016 15:05', 'MM/DD/YYYY HH24:MI'));
PostgreSQL only displays the date.
If possible, would I have to run a separate select statement to extract the time (i.e. 12:05 and 15:05), stored in that field? Or are the times completey discard when the query gets executed.
I don't want to use timestamp, since I'd like to execute this in Oracle SQL as well.
to_date returns... a date. Surprise! So yeah, it's not going to give you the time.
You should be using the timestamp data type to store times and functions which return timestamps. So use to_timestamp.
Oracle also has a timestamp data type and to_timestamp function.
In general, trying to write one set of SQL that works with multiple databases results in either having to write very simple SQL that doesn't take advantage of any of the database's features, or madness.
Instead, use a SQL query builder to write your SQL for you, take care of compatibility issues, and allow you to add clauses to existing statements. For example, Javascript has Knex.js and Perl has SQL::Abstract.

Is there any equivallent function in postgres like systimestamp() of oracle

I'm working on a oracle to postgresql database migration project. I need to read operating system date and time from postgres. In oracle sysdate() returns system date time in date type data and systimestamp() returns timestamp type data whatever the time_zone variable set. But in postgres current_date() and current_timestamp() always give the result relative to the timezone variable set for that database.
Synchronizing the timezone variable (i.e. set timezone='utc') is one way, but I don't want my timezone variable to be changed.
All I want to get is the current date and time of my system (time zone may include or not) like in oracle. Any pl/pgsql would be helpful. Thanks
The data type TIMESTAMP WITH TIME ZONE is different in Oracle and PostgreSQL.
While Oracle stores the timestamp information along with the timestamp, PostgreSQL stores the timestamp in UTC and displays it in the currently set time zone (available with the SQL statement SHOW TimeZone).
So the functions return the same time in PostgreSQL and Oracle, but it is displayed in a different fashion. That should normally be no problem.
If you really need to store time zone information along with a timestamp, you'll have to use a separate field to store the time zone information. You can then use AT TIME ZONE to convert the timestamp to that time zone for display.

What's the cleanest way to get the current local time in Postgres?

Is there a more succinct way to write the following in Postgres:
DATE(NOW() AT TIME ZONE time_zone)
i.e. to get the local time for a record with time zone time_zone?
Ideally I'd like to do something like this:
NOW(time_zone)
That doesn't work but is there anything similar and clean I can use?
=> SELECT DATE(NOW() AT TIME ZONE 'UTC');
2013-11-19
=> SELECT DATE(NOW() AT TIME ZONE 'PST');
2013-11-18
UPDATE:
If you're looking to abstract this query into a custom function, you can create one as follows:
=> CREATE FUNCTION date_with_time_zone() RETURNS date AS '
SELECT DATE(NOW() AT TIME ZONE ''PST'');
' LANGUAGE SQL;
CREATE FUNCTION
=> SELECT date_with_time_zone();
2013-11-18
While you still need to issue the SELECT command, the current date with time zone will be returned simply by invoking the function, rather than the entire query.