Postgres相当于Timezonedb Mysql查询

时间:2017-12-21 12:08:40

标签: mysql postgresql timezonedb

我试图在MySql中创建的psql中使用Timezone DB。我通过手动查询并通过节点脚本将其插入psql来移植数据。现在我需要的是以下查询的等效psql查询。由于在mysql中找不到某些函数,因此在psql下面查询将无法正常工作。任何帮助将不胜感激。

使用的三个表的定义是

<tr *ngFor="let vehicle of vehicles;let i = index;trackBy :trackByVehicles">
  <td>
  <kendo-autocomplete [data]="bookingItemRelations" 
  (focus)="relationsFocusAutoComplete({{i}})"  //Its not working.
   #relationAutocomplete{{i}} required id="relation--{{i}}">
  </kendo-autocomplete>
</td>

查询在Mysql中显示本地时区:

DROP TABLE IF EXISTS `country`;
CREATE TABLE `country` (
    `country_code` CHAR(2) NULL,
    `country_name` VARCHAR(45) NULL,
    INDEX `idx_country_code` (`country_code`)
) COLLATE='utf8_bin' ENGINE=MyISAM;

LOAD DATA LOCAL INFILE 'country.csv' INTO TABLE `country` FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';


DROP TABLE IF EXISTS `timezone`;
CREATE TABLE `timezone` (
    `zone_id` INT(10) NOT NULL,
    `abbreviation` VARCHAR(6) NOT NULL,
    `time_start` INT NOT NULL,
    `gmt_offset` INT NOT NULL,
    `dst` CHAR(1) NOT NULL,
    INDEX `idx_zone_id` (`zone_id`),
    INDEX `idx_time_start` (`time_start`)
) COLLATE='utf8_bin' ENGINE=MyISAM;

LOAD DATA LOCAL INFILE 'timezone.csv' INTO TABLE `timezone` FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';


DROP TABLE IF EXISTS `zone`;
CREATE TABLE `zone` (
    `zone_id` INT(10) NOT NULL AUTO_INCREMENT,
    `country_code` CHAR(2) NOT NULL,
    `zone_name` VARCHAR(35) NOT NULL,
    PRIMARY KEY (`zone_id`),
    INDEX `idx_zone_name` (`zone_name`)
) COLLATE='utf8_bin' ENGINE=MyISAM;

LOAD DATA LOCAL INFILE 'zone.csv' INTO TABLE `zone` FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';

Psql中的等价物是什么?

这是我到目前为止所尝试过的。但这不正确。它显示了+5.30的差异,这是我们的时区加尔各答。

    SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(UTC_TIMESTAMP()) + tz.gmt_offset, '%a, %d %b %Y, %H:%i:%s') AS local_time
FROM `timezone` tz JOIN `zone` z
ON tz.zone_id=z.zone_id
WHERE tz.time_start <= UNIX_TIMESTAMP(UTC_TIMESTAMP()) AND z.zone_name='America/Los_Angeles'
ORDER BY tz.time_start DESC LIMIT 1;
  

请注意字段名称不完全相同。

我得到的结果是SELECT to_timestamp(EXTRACT(EPOCH FROM current_timestamp at time zone 'UTC') + tz.gmt_offset) AS local_time FROM tbl_timezones tz JOIN tbl_zones z ON tz.zone_id=z.id WHERE tz.time_start <= (EXTRACT(EPOCH FROM current_timestamp at time zone 'UTC') ) AND z.zone_name='America/Los_Angeles' ORDER BY tz.time_start DESC LIMIT 1; ,其中在Google中检查的实际时间是

"2017-12-21 10:38:29.301579+05:30"

1 个答案:

答案 0 :(得分:0)

我已转换为postgres版本:

来自timezonedb.com的原始文件

SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(UTC_TIMESTAMP()) + tz.gmt_offset, '%a, %d %b %Y, %H:%i:%s') AS local_time
FROM `timezone` tz JOIN `zone` z
ON tz.zone_id=z.zone_id
WHERE tz.time_start <= UNIX_TIMESTAMP(UTC_TIMESTAMP()) AND z.zone_name='Asia/Ho_Chi_Minh'
ORDER BY tz.time_start DESC LIMIT 1;

我的postgres查询:

SELECT TO_CHAR(to_timestamp(EXTRACT(EPOCH FROM timezone('UTC', CURRENT_TIMESTAMP)::timestamptz) + tz.gmt_offset), 'yyyy-mm-dd HH24:MI:SS') AS local_time
FROM timezone tz JOIN zone z
ON tz.zone_id=z.zone_id
WHERE tz.time_start <= (EXTRACT(EPOCH FROM timezone('UTC', CURRENT_TIMESTAMP)::timestamptz ) ) AND z.zone_name='Asia/Ho_Chi_Minh'
ORDER BY tz.time_start DESC LIMIT 1;

参考:

[1] https://stackoverflow.com/a/33276622/2552249

[2] https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_timestamp_.28without_time_zone.29