refactor utils fx

This commit is contained in:
madarsbiss 2021-09-22 23:09:32 +03:00
parent 2121673532
commit 614ee69cc9
3 changed files with 55 additions and 54 deletions

View File

@ -1,23 +1,16 @@
import { convertTime, timeToAMPM } from "../services/converters"; import { unixToLocalTime, timeTo12HourFormat } from "../services/converters";
import { getWeekDay, getTime, isPM } from "../services/utils"; import { getTime, getWeekDay, getAMPM } from "../services/utils";
import styles from "./DateAndTime.module.css"; import styles from "./DateAndTime.module.css";
const DateAndTime = ({ weatherData, systemUsed }) => { const DateAndTime = ({ weatherData, systemUsed }) => {
return ( return (
<h2 className={styles.title}> <h2 className={styles.title}>
{getWeekDay(weatherData)},{" "} {`${getWeekDay(weatherData)} ${getTime(
{systemUsed == "metric" systemUsed,
? parseInt( weatherData.dt,
convertTime(weatherData.dt, weatherData.timezone)[0].split(":")[0] weatherData.timezone
) )} ${getAMPM(systemUsed, weatherData.dt, weatherData.timezone)}`}
: timeToAMPM(
convertTime(weatherData.dt, weatherData.timezone)[0]
).split(":")[0]}
:00{" "}
{systemUsed == "imperial"
? isPM(convertTime(weatherData.dt, weatherData.timezone)[0])
: ""}
</h2> </h2>
); );
}; };

View File

@ -1,18 +1,18 @@
export const ctoF = (c) => (c * 9) / 5 + 32; export const ctoF = (c) => (c * 9) / 5 + 32; //celsius to fahrenheit
export const mpsToMph = (mps) => (mps * 2.236936).toFixed(2); export const mpsToMph = (mps) => (mps * 2.236936).toFixed(2); //meters per second - miles per hour
export const kmToM = (km) => (km / 1.609).toFixed(1); export const kmToMiles = (km) => (km / 1.609).toFixed(1); //kilometers to miles
export const timeToAMPM = (time) => { export const timeTo12HourFormat = (time) => {
let hours = time.split(":")[0]; //23:43 to 11:43
let minutes = time.split(":")[1]; const [hours, minutes] = time.split(":");
hours = hours % 12; const remain = hours % 12;
hours = hours ? hours : 12; return `${remain ? remain : 12}:${minutes}`;
return hours + ":" + minutes;
}; };
export const degToCompass = (num) => { export const degToCompass = (num) => {
//degree to compass direction
var val = Math.floor(num / 22.5 + 0.5); var val = Math.floor(num / 22.5 + 0.5);
var arr = [ var arr = [
"N", "N",
@ -35,10 +35,14 @@ export const degToCompass = (num) => {
return arr[val % 16]; return arr[val % 16];
}; };
export const convertTime = (unixSeconds, timezone) => { // unixToLocalTime
const time = new Date((unixSeconds + timezone) * 1000) export const unixToLocalTime = (unixSeconds, timezone) => {
//convert time to 19:23 (last received data in 24h format)
let time = new Date((unixSeconds + timezone) * 1000)
.toISOString() .toISOString()
.match(/(\d{2}:\d{2})/); .match(/(\d{2}:\d{2})/)[0];
return time; // console.log(time);
// time = time.startsWith("0") ? time.substring(1) : time;
return time.startsWith("0") ? time.substring(1) : time;
}; };

View File

@ -1,6 +1,35 @@
import { convertTime, kmToM, mpsToMph, timeToAMPM } from "./converters"; import {
unixToLocalTime,
kmToMiles,
mpsToMph,
timeTo12HourFormat,
} from "./converters";
export const getWindSpeed = (systemUsed, windInMps) =>
systemUsed == "metric" ? windInMps : mpsToMph(windInMps); //meters per second to miles per hour
export const getVisibility = (
systemUsed,
visibilityInMeters // visibility in kilometers or in miles
) =>
systemUsed == "metric"
? (visibilityInMeters / 1000).toFixed(1)
: kmToMiles(visibilityInMeters / 1000);
export const getTime = (systemUsed, currentTime, timezone) =>
systemUsed == "metric"
? unixToLocalTime(currentTime, timezone)
: timeTo12HourFormat(unixToLocalTime(currentTime, timezone));
export const getAMPM = (systemUsed, currentTime, timezone) =>
systemUsed === "imperial"
? unixToLocalTime(currentTime, timezone).split(":")[0] >= 12
? "PM"
: "AM"
: "";
export const getWeekDay = (weatherData) => { export const getWeekDay = (weatherData) => {
//get the name of the week day
const weekday = [ const weekday = [
"Sunday", "Sunday",
"Monday", "Monday",
@ -11,31 +40,6 @@ export const getWeekDay = (weatherData) => {
"Saturday", "Saturday",
]; ];
return weekday[ return weekday[
new Date( new Date((weatherData.dt + weatherData.timezone) * 1000).getUTCDay()
convertTime(weatherData.dt, weatherData.timezone).input
).getUTCDay()
]; ];
}; };
export const isPM = (time) => {
let hours = time.split(":")[0];
return hours >= 12 ? "PM" : "AM";
};
export const getWindSpeed = (systemUsed, windInMph) =>
systemUsed == "metric" ? windInMph : mpsToMph(windInMph);
export const getVisibility = (systemUsed, visibilityInKm) =>
systemUsed == "metric"
? (visibilityInKm / 1000).toPrecision(2)
: kmToM(visibilityInKm / 1000);
export const getTime = (systemUsed, currentTime, timezone) =>
systemUsed == "metric"
? `${parseInt(convertTime(currentTime, timezone)[0].split(":")[0])}:${
convertTime(currentTime, timezone)[0].split(":")[1]
}`
: timeToAMPM(convertTime(currentTime, timezone)[0]);
export const getAMPM = (systemUsed, currentTime, timezone) =>
systemUsed == "imperial" ? isPM(convertTime(currentTime, timezone)[0]) : "";