1

What would a function look like that would return local time here, local time there, sunrise there, sunset there, the time of the weather report and the times of each of the 3hr forecasts?

Answering my own question in separate post below.

I am using google places api to get latitude, longitude, and utc_offset of remote location.

I am using openweathermap api to get time (in utc format) of remote location's current weather report, sunrise, sunset, and 24 hour forecast in 3hr time increments.

I create a javascript date() object from which I obtain my (not remote) location's utc_offset using getTimezoneOffset() function.

I use a function (shown below) that converts a UNIX timestamp into clock hours and minutes (modified for my own use from this post on stackoverflow 5-21-2011 Convert a Unix timestamp to time in JavaScript.)

What I need to do is this:
- get the remote utc Unix time for whatever weather information being reported (current weather, sunrise, sunset, multiple 3h forecasts)
- subtract my local utc offset to get an adjusted unix time for my current time at Greenwich.
- add the remote utc offset to get an adjusted unix time at remote location
- pass adjusted unix time to the TimeConverter function.

Using this information, I posted the function below which uses a javascript class object to hold and return all the information needed.

Here are my utc dates Timestamp from geolocation call on my local device.
timestamp: 1523646017649 (this has to be adjusted down by 1000 to match adjusted timestamps from googleplaces and openweather map)

Timestamp from openweathermap's weather api call for remote longitude and latitude
dt:1523644200 (response.dt)
sunrise: 1523595750 (response.sys.sunrise)
sunset: 1523644821 (response.sys.sunset)
first 3h forecast: 1523653200 (response.sys.sunrise)

My utcoffset from dateObj = new date(); then dateObj.getTimezoneOffset()
my utc_offset: 18000

Remote utc_offset from googleplacesapi
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;
}
Bobby
  • 137
  • 11

1 Answers1

0

Placetime class Useage:

// 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 ) ;

The following class holds all the information needed to calculate the local times at a remote location and provides methods to retrieve the information as needed.

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 ;
  }
}
Bobby
  • 137
  • 11