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 { getWeekDay, getTime, isPM } from "../services/utils";
import { unixToLocalTime, timeTo12HourFormat } from "../services/converters";
import { getTime, getWeekDay, getAMPM } from "../services/utils";
import styles from "./DateAndTime.module.css";
const DateAndTime = ({ weatherData, systemUsed }) => {
return (
<h2 className={styles.title}>
{getWeekDay(weatherData)},{" "}
{systemUsed == "metric"
? parseInt(
convertTime(weatherData.dt, weatherData.timezone)[0].split(":")[0]
)
: timeToAMPM(
convertTime(weatherData.dt, weatherData.timezone)[0]
).split(":")[0]}
:00{" "}
{systemUsed == "imperial"
? isPM(convertTime(weatherData.dt, weatherData.timezone)[0])
: ""}
{`${getWeekDay(weatherData)} ${getTime(
systemUsed,
weatherData.dt,
weatherData.timezone
)} ${getAMPM(systemUsed, weatherData.dt, weatherData.timezone)}`}
</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) => {
let hours = time.split(":")[0];
let minutes = time.split(":")[1];
hours = hours % 12;
hours = hours ? hours : 12;
return hours + ":" + minutes;
export const timeTo12HourFormat = (time) => {
//23:43 to 11:43
const [hours, minutes] = time.split(":");
const remain = hours % 12;
return `${remain ? remain : 12}:${minutes}`;
};
export const degToCompass = (num) => {
//degree to compass direction
var val = Math.floor(num / 22.5 + 0.5);
var arr = [
"N",
@ -35,10 +35,14 @@ export const degToCompass = (num) => {
return arr[val % 16];
};
export const convertTime = (unixSeconds, timezone) => {
const time = new Date((unixSeconds + timezone) * 1000)
// unixToLocalTime
export const unixToLocalTime = (unixSeconds, timezone) => {
//convert time to 19:23 (last received data in 24h format)
let time = new Date((unixSeconds + timezone) * 1000)
.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) => {
//get the name of the week day
const weekday = [
"Sunday",
"Monday",
@ -11,31 +40,6 @@ export const getWeekDay = (weatherData) => {
"Saturday",
];
return weekday[
new Date(
convertTime(weatherData.dt, weatherData.timezone).input
).getUTCDay()
new Date((weatherData.dt + weatherData.timezone) * 1000).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]) : "";