Skip to content

Commit

Permalink
I fixed a weekday problem by applying the UTCDay method.
Browse files Browse the repository at this point in the history
  • Loading branch information
aline-borges committed Nov 21, 2020
1 parent 3cd221c commit 8147988
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 40 deletions.
54 changes: 15 additions & 39 deletions src/screens/Location/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,15 @@ import { SafeAreaView } from 'react-native-safe-area-context';
const Location = ({ route, navigation }) => {
const { cityData } = route.params;

const timezone = cityData.data2.timezone;
const time = parseInt(new Date().toLocaleTimeString("pt-BR", {timeZone: timezone}).split(':')[0]);
const timestampHourNow = new Date(cityData.data2.hourly[0].dt * 1000);
const timestampOneHourLater = new Date(cityData.data2.hourly[1].dt * 1000);
const timestampTwoHoursLater = new Date(cityData.data2.hourly[2].dt * 1000);
const timestampThreeHoursLater = new Date(cityData.data2.hourly[3].dt * 1000);

const date = new Date((cityData.data2.current.dt + cityData.data2.timezone_offset) * 1000)
const time = date.getUTCHours();

const nowDayUTC = new Date(cityData.data2.daily[0].dt * 1000);
const oneMoreDayOfWeekUTC = new Date(cityData.data2.daily[1].dt * 1000);
const twoMoreDaysOfWeekUTC = new Date(cityData.data2.daily[2].dt * 1000);
const threeMoreDaysOfWeekUTC = new Date(cityData.data2.daily[3].dt * 1000);
const fourMoreDaysOfWeekUTC = new Date(cityData.data2.daily[4].dt * 1000);
const fiveMoreDaysOfWeekUTC = new Date(cityData.data2.daily[5].dt * 1000);

const convertUTCDateToLocalDate = (hour) => {
const nowHour = hour.getHours();

return nowHour;
}

const convertUTCDateToLocalDateDay = (day) => {
const convertDay = day.getDay();

return convertDay;
}
const nowDayUTC = new Date((cityData.data2.current.dt + cityData.data2.timezone_offset) * 1000);
const oneMoreDayOfWeekUTC = new Date((cityData.data2.daily[1].dt + cityData.data2.timezone_offset) * 1000);
const twoMoreDaysOfWeekUTC = new Date((cityData.data2.daily[2].dt + cityData.data2.timezone_offset) * 1000);
const threeMoreDaysOfWeekUTC = new Date((cityData.data2.daily[3].dt + cityData.data2.timezone_offset) * 1000);
const fourMoreDaysOfWeekUTC = new Date((cityData.data2.daily[4].dt + cityData.data2.timezone_offset) * 1000);
const fiveMoreDaysOfWeekUTC = new Date((cityData.data2.daily[5].dt + cityData.data2.timezone_offset) * 1000);

const convertNumberToNameOfWeek = (day) => {
if(day === 0) { return 'Domingo'}
Expand All @@ -49,21 +32,14 @@ const Location = ({ route, navigation }) => {
if(day === 6) { return 'Sábado'}
}

const nowDayConvertDay = convertUTCDateToLocalDateDay(nowDayUTC);
const oneMoreDayConvertDay = convertUTCDateToLocalDateDay(oneMoreDayOfWeekUTC);
const twoMoreDaysConvertDay = convertUTCDateToLocalDateDay(twoMoreDaysOfWeekUTC);
const threeMoreDaysConvertDay = convertUTCDateToLocalDateDay(threeMoreDaysOfWeekUTC);
const fourMoreDaysConvertDay = convertUTCDateToLocalDateDay(fourMoreDaysOfWeekUTC);
const fiveMoreDaysConvertDay = convertUTCDateToLocalDateDay(fiveMoreDaysOfWeekUTC);

const nowDay = convertNumberToNameOfWeek(nowDayConvertDay);
const oneMoreDayOfWeek = convertNumberToNameOfWeek(oneMoreDayConvertDay);
const twoMoreDaysOfWeek = convertNumberToNameOfWeek(twoMoreDaysConvertDay);
const threeMoreDaysOfWeek = convertNumberToNameOfWeek(threeMoreDaysConvertDay);
const fourMoreDaysOfWeek = convertNumberToNameOfWeek(fourMoreDaysConvertDay);
const fiveMoreDaysOfWeek = convertNumberToNameOfWeek(fiveMoreDaysConvertDay);
const nowDay = convertNumberToNameOfWeek(nowDayUTC.getUTCDay());
const oneMoreDayOfWeek = convertNumberToNameOfWeek(oneMoreDayOfWeekUTC.getUTCDay());
const twoMoreDaysOfWeek = convertNumberToNameOfWeek(twoMoreDaysOfWeekUTC.getUTCDay());
const threeMoreDaysOfWeek = convertNumberToNameOfWeek(threeMoreDaysOfWeekUTC.getUTCDay());
const fourMoreDaysOfWeek = convertNumberToNameOfWeek(fourMoreDaysOfWeekUTC.getUTCDay());
const fiveMoreDaysOfWeek = convertNumberToNameOfWeek(fiveMoreDaysOfWeekUTC.getUTCDay());

const hourNow = convertUTCDateToLocalDate(timestampHourNow)
const hourNow = time;
const oneHourLater = time === "23" ? 0 : time + 1;
const twoHoursLater = time === "22" ? 0 : time + 2;
const threeHoursLater = time === "21" ? 0 : time + 3;
Expand Down
1 change: 0 additions & 1 deletion src/screens/Locations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const Locations = ({ navigation }) => {
let newHour = ``;

hour < 10 ? newHour = `0${hour}` : newHour = `${hour}`
console.log(newHour)

const time = `${newHour}:${minutes}`;

Expand Down

0 comments on commit 8147988

Please sign in to comment.