File size: 2,850 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { useLocalizedMoment } from 'calypso/components/localized-moment';
import {
	phpToMomentDatetimeFormat,
	phpToMomentMapping,
} from 'calypso/my-sites/site-settings/date-time-format/utils';
import { useSiteSettings } from '../../plugins-scheduled-updates/hooks/use-site-settings';

export function useDateTimeFormat( siteSlug?: string ) {
	const moment = useLocalizedMoment();
	const { getSiteSetting } = useSiteSettings( siteSlug );

	// Returns the current datetime format based on the locale in PHP format
	const getDateTimeFormatFromLocale = ( locale: string ) => {
		const dateFormatter = new Intl.DateTimeFormat( locale, { dateStyle: 'long' } );
		const timeFormatter = new Intl.DateTimeFormat( locale, { timeStyle: 'short' } );

		const dateFormat = dateFormatter
			.formatToParts( new Date() )
			.map( ( part ) => {
				switch ( part.type ) {
					case 'day':
						return 'd';
					case 'month':
						return 'M';
					case 'year':
						return 'Y';
					default:
						return part.value;
				}
			} )
			.join( '' );

		const timeFormat = timeFormatter
			.formatToParts( new Date() )
			.map( ( part ) => {
				switch ( part.type ) {
					case 'hour':
						return 'H';
					case 'minute':
						return 'i';
					case 'second':
						return 's';
					case 'dayPeriod':
						return '';
					default:
						return part.value;
				}
			} )
			.join( '' );

		return {
			dateFormat,
			timeFormat,
		};
	};

	const { dateFormat, timeFormat } = siteSlug
		? {
				dateFormat: getSiteSetting( 'date_format' ),
				timeFormat: getSiteSetting( 'time_format' ),
		  }
		: getDateTimeFormatFromLocale( moment.locale() );

	const phpToMomentMap = phpToMomentMapping as {
		[ key: string ]: string;
	};

	function convertPhpToMomentFormat( phpFormat: string ): string {
		let momentFormat = '';

		for ( let i = 0; i < phpFormat.length; i++ ) {
			const char = phpFormat.charAt( i );
			if ( phpToMomentMap[ char ] ) {
				momentFormat += phpToMomentMap[ char ];
			} else {
				momentFormat += char;
			}
		}

		return momentFormat;
	}

	// Prepare timestamp based on site settings
	const prepareTime = ( unixTimestamp: number ) => {
		const ts = unixTimestamp * 1000;

		return phpToMomentDatetimeFormat( moment( ts ), timeFormat );
	};

	// Prepare timestamp based on site settings
	const prepareDateTime = ( unixTimestamp: number, customPhpDateFormat?: string ) => {
		const ts = unixTimestamp * 1000;
		const format = `${ customPhpDateFormat || dateFormat } ${ timeFormat }`;

		return phpToMomentDatetimeFormat( moment( ts ), format );
	};

	const isAmPmPhpTimeFormat = () => {
		// Regular expression to check for AM/PM indicators
		const ampmRegex = /(a|A)/;

		return ampmRegex.test( timeFormat );
	};

	return {
		dateFormat,
		timeFormat,
		prepareTime,
		prepareDateTime,
		isAmPmPhpTimeFormat,
		convertPhpToMomentFormat,
	};
}