javascript:显示远程位置的AM / PM时间,以获取远程位置的天气数据

时间:2018-04-13 20:27:05

标签: javascript class time google-places-api openweathermap

什么样的功能会在这里返回当地时间,当地时间,那里的日出,那里的日落,天气报告的时间以及每次3小时预报的时间?

在下面的单独帖子中回答我自己的问题。

我使用google places api获取远程位置的纬度,经度和utc_offset。

我正在使用openweathermap api以3小时的时间增量获取远程位置的当前天气预报,日出,日落和24小时预报的时间(以utc格式)。

我创建了一个javascript date()对象,使用getTimezoneOffset()函数从中获取我的(非远程)位置的utc_offset。

我使用一个函数(如下所示)将UNIX时间戳转换为时钟小时和分钟(在stackoverflow 5-21-2011 Convert a Unix timestamp to time in JavaScript上从这篇文章修改供我自己使用。)

我需要做的是:
     - 获取远程utc Unix时间,以便报告任何天气信息(当前天气,日出,日落,多个3h预测)      - 减去我当地的utc偏移量,以获得格林威治当前时间的调整后的unix时间      - 添加远程utc偏移量以获得远程位置的调整后的unix时间
     - 将调整后的unix时间传递给TimeConverter函数。

使用此信息,我发布了下面的函数,该函数使用javascript类对象来保存并返回所需的所有信息。

这是我的约会日期 来自我本地设备的地理定位呼叫的时间戳     时间戳:1523646017649(这必须调低1000以匹配来自googleplaces和openweather地图的调整后的时间戳)

来自openweathermap的天气api的时间戳呼叫远程经度和纬度
    dt:1523644200(response.dt)
    日出:1523595750(response.sys.sunrise)
    日落:1523644821(response.sys.sunset)
    前3h预测:1523653200(response.sys.sunrise)

我的utcoffset来自dateObj = new date();然后dateObj.getTimezoneOffset()
    我的utc_offset:18000

来自googleplacesapi的远程utc_offset
    utc_offset:7200

//To get current time where I am at, I can use this call and this function<br>
var myTime = timeConverter(Date.now()/1000,1);    
// Return Result: Apr-13-2018, 2pm    

function timeConverter(UNIX_timestamp,hhmmOpts){
  // based on code written by @shomrat and edited by @Chin at
  // https://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript    
  var a = new Date((UNIX_timestamp) * 1000);    
  var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];    
  var year = a.getFullYear();
  var month = months[a.getMonth()];
  var day = a.getDate();
  var hour = a.getHours();
  var amPm = ( (hour < 12) ? "a" : "p" );
  if ( hour == 0) {
    hour = "12" ;
  } else if ( hour > 12 ) {
    hour = (hour - 12);
  }
  var min = a.getMinutes().toString().padStart(2,"0") ;
  var time = "" ;
  if (hhmmOpts == 0) { // Apr 18, 2018, 11a    
    time += month + '-' + padLeft(day,2,'0') + '-' + year + ',&nbsp;' + padLeft(hour,2,'&nbsp;&nbsp;') + amPm;
  } else
  if (hhmmOpts == 1) { // Apr 18, 2018, 11:27a
    time += month + '-' + padLeft(day,2,'0') + '-' + year + ',&nbsp;' + hour + ":" + min + amPm;
  } else
  if (hhmmOpts == 2) { // 11p
    time += hour + ":" + min + amPm;
  }
  return time;
}

1 个答案:

答案 0 :(得分:0)

Placetime课程 用法:

// Get the googleplaces api offset (snipet)
...
google.maps.event.addListener(autocomplete, 'place_changed', function () {
  var place = autocomplete.getPlace();
  // console.log(place);
  utcOffset = place.utc_offset;
  getWeather(utcOffset);
}
...

// Create a new placetime object
// To get device location local time use utcOffset==0.
plTimeObj = new placeTime(utcOffset);

// Get current time at present location
var plTime = timeConverter(plTimeObj.getCurrentTime(),1) ;

// Get sunrise at remote location 
// Comes from call to openweathermap (see response.sys.sunrise above )
sunriseTime = timeConverter( plTimeObj.getPlaceTime(resp.sys.sunrise) ,2) ;

// Get the time of the weather report (from openweathermap)
var rptTime = timeConverter( plTimeObj.getPlaceTime(resp.list[i].dt),2 ) ;

以下类包含计算远程位置本地时间所需的所有信息,并提供了根据需要检索信息的方法。

class placeTime {
  constructor(utcOffset) {
    this.dateObj   = new Date();
    this.local2Utc = this.dateObj.getTimezoneOffset() * 60 ;
    this.utc2Place = (utcOffset==0 ? (0-this.local2Utc) : (utcOffset * 60) );
    this.currentTime = this.dateObj.getTime()/1000 + this.local2Utc + this.utc2Place ;
  }
  getPlaceTime(localTime) {
    var pTime = localTime + this.local2Utc + this.utc2Place ;
    return pTime;
  }
  getCurrentTime() {
    return this.currentTime ;
  }
}
相关问题