2016-09-15 45 views
1

我使用Matlab datainsert函數將格式爲'yyyy-mm-dd hh:mm:ss'的字符串插入到PostgreSQL數據庫中的帶時區字段的時間戳中,沒有任何問題。但是,我似乎無法確定如何在字符串本身中包含時區,所以Postgres假定它具有我的區域設置的時區。應如何格式化時區在Matlab中,將字符串插入帶時區的postgresql字段時間戳時?

例如

create table tmp_test (
id serial primary key, 
time timestamp with time zone NOT NULL 
); 

然後,這個工程:

datainsert(conn, 'tmp_test', {'time'}, {'2016-09-15 03:45:49.326'}) 

但這並不:(?)

>> datainsert(conn, 'tmp_test', {'time'}, {'2016-09-15 03:45:49.326 +00:00'}) 
Error using database/datainsert (line 301) 
Unable to insert element in row 1 column 1, 2016-09-15 03:45:49.326 +00:00. Timestamp format must be yyyy-mm-dd 
hh:mm:ss[.fffffffff] 
+0

可能重複的[我如何插入timezone與Postgresql與準備好的語句時間戳?](http://stackoverflow.com/questions/13676168/how-can-i-insert-timestamp-with-timezone- in-postgresql-with-prepared-statement) – Alex

回答

2

我假設是unix環境,但date()命令可以返回包含時區的字符串:即東部時間EDT

$> TZ='America/New_York' date +%Y-%m-%d_%T_%Z 
2016-09-15_01:34:10_EDT 

這是用於PostgreSQL的特定語言,如PHP等?

+0

謝謝。環境實際上是Windows 7.我通過如下命令插入:'insert into tmp_test(time)values('2016-08-01 14:22:50.333')'。 – Alex

+0

然後正式,我提到雅日期格式的具體postgres文檔:https://www.postgresql.org/docs/current/static/functions-formatting.html – SomeDude

+0

謝謝,事實證明,這是特定於Matlab。我可以在pgadmin中插入帶時區的字符串,但不能在Matlab中使用 – Alex

相關問題