Skip to content

Commit

Permalink
use browser language for number & date formating
Browse files Browse the repository at this point in the history
  • Loading branch information
io53 committed Aug 28, 2024
1 parent c7805be commit e059f43
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/TimeHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ export function getTimestamp(date) {
let part_date = `${date.getDate()}.${date.getMonth()+1}.${date.getFullYear()}`
let part_time = `${("0" + date.getHours()).slice(-2)}:${("0" + date.getMinutes()).slice(-2)}`
return part_date + " " + part_time
}

export function secondsToUserDateString(seconds) {
return new Date(seconds * 1000).toLocaleString(navigator.language || "fi-FI")
}
6 changes: 4 additions & 2 deletions src/UnitHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,10 @@ export function getUnitHelper(key, plaintext) {
export function localeNumber(value, decimals) {
if (typeof (value) !== "number") return value
if (isNaN(value)) return "-"
if (decimals === undefined) return value.toLocaleString("fi-FI")
return value.toLocaleString("fi-FI", { minimumFractionDigits: decimals, maximumFractionDigits: decimals })
let lng = navigator.language;
if (!lng) lng = "fi-FI"
if (decimals === undefined) return value.toLocaleString(lng)
return value.toLocaleString(lng, { minimumFractionDigits: decimals, maximumFractionDigits: decimals })
}

export function temperatureToUserFormat(temperature, offset) {
Expand Down
3 changes: 2 additions & 1 deletion src/components/CompareView.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Box, useColorMode } from "@chakra-ui/react";
import { t } from "i18next";
import { getUnitHelper } from "../UnitHelper";
import UplotTouchZoomPlugin from "./UplotTouchZoomPlugin";
import { secondsToUserDateString } from "../TimeHelper";

function ddmm(ts) {
var d = new Date(ts * 1000);
Expand Down Expand Up @@ -188,7 +189,7 @@ function CompareView(props) {
{
label: t('time'),
class: "graphLabel",
value: "{YYYY}-{MM}-{DD} {HH}:{mm}:{ss}",
value: (_, ts) => secondsToUserDateString(ts),
},
...sensorData.map((x, i) => {
return {
Expand Down
3 changes: 2 additions & 1 deletion src/components/Graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { IconButton } from "@chakra-ui/react";
import { MdInfo } from "react-icons/md";
import notify from "../utils/notify";
import { calculateAverage } from "../utils/dataMath";
import { secondsToUserDateString } from "../TimeHelper";
const UplotReact = React.lazy(() => import('uplot-react'));

function ddmm(ts) {
Expand Down Expand Up @@ -358,7 +359,7 @@ class Graph extends Component {
series: [{
label: this.props.t('time'),
class: "graphLabel",
value: "{YYYY}-{MM}-{DD} {HH}:{mm}:{ss}",
value: (_, ts) => secondsToUserDateString(ts),
}, {
label: this.props.dataName || this.props.t(this.props.dataKey),
class: "graphLabel",
Expand Down

0 comments on commit e059f43

Please sign in to comment.