int、DateTime、string是否可以为 null?

值类型与null

a. int、DateTime 不能
b. 因为其为 Struct 类型,而结构属于值类型,值类型不能为 null,只有引用类型才能被赋值 null。 string 可以为 null

将以下代码改写成qt中能用的c++代码,且date_formats和time_formats改为从外部配置文件中读取,该配置文件中每行除了日期或时间格式,还有示例数据,读取时示例数据省略。原代码如下:<?php declare(strict_types=1); namespace zjkal; use DateTime; use DateTimeZone; use Exception; use InvalidArgumentException; /** * 最方便的PHP时间助手类, 所有方法都可以传入任意类型的时间日期格式或者时间戳 * Class TimeHelper * @package zjkal */ class TimeHelper { //常见的特殊日期格式 private static $date_formats = [ 'Y-m-d', 'm/d/Y', 'd.m.Y', 'm.d.y', 'd/m/Y', 'Y年m月d日', 'Y年m月', 'Y年m月d号', 'Y/m/d', 'Y.m.d', 'Y.m', 'F d, Y', 'M d, Y', 'F j, Y', 'M j, Y', 'F jS, Y', 'M jS, Y' ]; //常见的特殊时间格式 private static $time_formats = ['H', 'H:i', 'H:i:s', 'H点', 'H点i分', 'H点i分s秒', 'H时', 'H时i分', 'H时i分s秒', 'g:i a', 'h:i a']; /** * 判断是否为时间戳格式 * @param int|string $timestamp 要判断的字符串 * @return bool 如果是时间戳返回True,否则返回False */ public static function isTimestamp($timestamp): bool { $start = strtotime('1970-01-01 00:00:00'); $end = strtotime('2099-12-31 23:59:59'); //判断是否为时间戳 if (!empty($timestamp) && is_numeric($timestamp) && $timestamp <= $end && $timestamp >= $start) { return true; } else { return false; } } /** * 将任意时间类型的参数转为时间戳 * 请注意 m/d/y 或 d-m-y 格式的日期,如果分隔符是斜线(/),则使用美洲的 m/d/y 格式。如果分隔符是横杠(-)或者点(.),则使用欧洲的 d-m-y 格式。 * 为了避免潜在的错误,您应该尽可能使用 YYYY-MM-DD 格式或者使用 date_create_from_format() 函数。 * @param int|string $datetime 要转换为时间戳的字符串或数字,如果为空则返回当前时间戳 * @return int 时间戳 */ public static function toTimestamp($datetime = null): int { if (empty($datetime)) { return time(); } $start = strtotime('1970-01-01 00:00:00'); $end = strtotime('2099-12-31 23:59:59'); //判断是否为时间戳 if (is_numeric($datetime) && $datetime <= $end && $datetime >= $start) { return intval($datetime); } else { $timestamp = strtotime($datetime); if ($timestamp) { return $timestamp; } else { /* 尝试处理特殊的日期格式 */ [$date, $time] = explode(' ', $datetime); if ($date) { //获取时间的格式 $time_format_str = ''; if ($time) { foreach (self::$time_formats as $time_format) { if (date_create_from_format($time_format, $time) !== false) { $time_format_str = $time_format; break; } } } foreach (self::$date_formats as $date_format) { //获取日期的格式 if (date_create_from_format($date_format, $date) !== false) { $datetime_format = ($time_format_str) ? "$date_format $time_format_str" : $date_format; //获取日期时间对象 $datetime_obj = date_create_from_format($datetime_format, $datetime); if ($datetime_obj !== false) { return strtotime($datetime_obj->format('Y-m-d' . ($time_format_str ? ' H:i:s' : ''))); } } } } throw new InvalidArgumentException('Param datetime must be a timestamp or a string time'); } } } /** * 将任意格式的时间转换为指定格式 * @param string $format 格式化字符串(默认为:Y-m-d H:i:s) * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @return false|string 格式化后的时间字符串 */ public static function format(string $format = 'Y-m-d H:i:s', $datetime = null): string { return date($format, self::toTimestamp($datetime)); } /** * 根据日期字符串, 修改指定时间, 并返回修改后的时间戳 * @param string $modifier 日期修改字符串 * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @return int 时间戳 */ public static function modifyTimestamp(string $modifier, $datetime = null): int { $date = new DateTime(); $date->setTimestamp(self::toTimestamp($datetime)); return $date->modify($modifier)->getTimestamp(); } /** * 返回截止到今天晚上零点之前的秒数 * @return int 秒数 */ public static function secondEndToday(): int { list($y, $m, $d) = explode('-', date('Y-m-d')); return mktime(23, 59, 59, intval($m), intval($d), intval($y)) - time(); } /** * 返回一分钟的秒数,传入参数可以返回数分钟的秒数 * @param int $minutes 分钟数,默认为1分钟 * @return int 秒数 */ public static function secondMinute(int $minutes = 1): int { return 60 * $minutes; } /** * 返回一小时的秒数,传入参数可以返回数小时的秒数 * @param int $hours 小时数,默认为1小时 * @return int 秒数 */ public static function secondHour(int $hours = 1): int { return 3600 * $hours; } /** * 返回一天的秒数,传入参数可以返回数天的秒数 * @param int $days 天数,默认为1天 * @return int 秒数 */ public static function secondDay(int $days = 1): int { return 86400 * $days; } /** * 返回一周的秒数,传入参数可以返回数周的秒数 * @param int $weeks 周数,默认为1周 * @return int 秒数 */ public static function secondWeek(int $weeks = 1): int { return 604800 * $weeks; } /** * 讲时间转换为友好显示格式 * @param int|string $datetime 时间日期的字符串或数字 * @param string $lang 语言,默认为中文,如果要显示英文传入en即可 * @return string 转换后的友好时间格式 */ public static function toFriendly($datetime, string $lang = 'zh'): string { $interval = self::getDateDiff($datetime); $count = 0; $type = ''; if ($interval->y) { $count = $interval->y; $type = $lang == 'zh' ? '年' : ' year'; } elseif ($interval->m) { $count = $interval->m; $type = $lang == 'zh' ? '月' : ' month'; } elseif ($interval->d) { $count = $interval->d; $type = $lang == 'zh' ? '天' : ' day'; } elseif ($interval->h) { $count = $interval->h; $type = $lang == 'zh' ? '小时' : ' hour'; } elseif ($interval->i) { $count = $interval->i; $type = $lang == 'zh' ? '分钟' : ' minute'; } elseif ($interval->s) { $count = $interval->s; $type = $lang == 'zh' ? '秒' : ' second'; } if (empty($type)) { return $lang == 'zh' ? '刚刚' : 'just now'; } else { return $count . $type . ($lang == 'zh' ? '前' : (($count > 1 ? 's' : '') . ' ago')); } } /** * 判断日期是否为今天 * @param string|int $datetime 时间日期 * @return bool 如果是今天则返回True,否则返回False */ public static function isToday($datetime): bool { return self::format('Y-m-d', $datetime) == self::format('Y-m-d'); } /** * 判断日期是否为昨天 * @param string|int $datetime 时间日期 * @return bool 如果是昨天则返回True,否则返回False */ public static function isYesterday($datetime): bool { return self::format('Y-m-d', $datetime) == self::format('Y-m-d', strtotime('-1 day')); } /** * 判断日期是否为明天 * @param string|int $datetime 时间日期 * @return bool 如果是明天则返回True,否则返回False */ public static function isTomorrow($datetime): bool { return self::format('Y-m-d', $datetime) == self::format('Y-m-d', strtotime('+1 day')); } /** * 判断日期是否为本周 * @param string|int $datetime 时间日期 * @return bool 如果是本周则返回True,否则返回False */ public static function isThisWeek($datetime): bool { $week_start = strtotime(date('Y-m-d 00:00:00', strtotime('this week'))); $week_end = strtotime(date('Y-m-d 23:59:59', strtotime('last day next week'))); $timestamp = self::toTimestamp($datetime); return $timestamp >= $week_start && $timestamp <= $week_end; } /** * 判断日期是否为本月 * @param string|int $datetime 时间日期 * @return bool 如果是本月则返回True,否则返回False */ public static function isThisMonth($datetime): bool { return self::format('Y-m', $datetime) == self::format('Y-m'); } /** * 判断日期是否为今年 * @param string|int $datetime 时间日期 * @return bool 如果是今年则返回True,否则返回False */ public static function isThisYear($datetime): bool { return self::format('Y', $datetime) == self::format('Y'); } /** * 获得指定日期是星期几(默认为当前时间) * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @return int 星期几(1-7) */ public static function getWeekDay($datetime = null): int { return intval(self::format('N', $datetime)); } /** * 判断指定日期是否为平常日(周一到周五) * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @return bool 是平常日返回true,否则返回false */ public static function isWeekday($datetime = null): bool { return in_array(self::getWeekDay($datetime), [1, 2, 3, 4, 5]); } /** * 判断指定日期是否为周末(周六和周日) * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @return bool 是周末返回true,否则返回false */ public static function isWeekend($datetime = null): bool { return in_array(self::getWeekDay($datetime), [6, 7]); } //获得两个日期的差量对象 private static function getDateDiff($datetime, $new_datetime = null): \DateInterval { $datetime = self::format('Y-m-d H:i:s', $datetime); $new_datetime = self::format('Y-m-d H:i:s', $new_datetime); return date_diff(date_create($datetime), date_create($new_datetime)); } /** * 返回两个日期相差的秒数(如果之传入一个日期,则与当前时间比较) * @param $datetime * @param $new_datetime * @return int */ public static function diffSeconds($datetime, $new_datetime = null): int { $timestamp = self::toTimestamp($datetime); $new_timestamp = self::toTimestamp($new_datetime); return abs($new_timestamp - $timestamp); } /** * 返回两个日期相差分钟数(如果之传入一个日期,则与当前时间比较) * @param $datetime * @param $new_datetime * @return int */ public static function diffMinutes($datetime, $new_datetime = null): int { return intval(self::diffSeconds($datetime, $new_datetime) / 60); } /** * 返回两个日期相差小时数(如果之传入一个日期,则与当前时间比较) * @param $datetime * @param $new_datetime * @return int */ public static function diffHours($datetime, $new_datetime = null): int { return intval(self::diffSeconds($datetime, $new_datetime) / 3600); } /** * 返回两个日期相差天数(如果只传入一个日期,则与当天时间比较) * @param int|string $datetime 要计算的时间 * @param int|string $new_datetime 要比较的时间(默认为当前时间) * @return int 相差天数 */ public static function diffDays($datetime, $new_datetime = null): int { return self::getDateDiff($datetime, $new_datetime)->days; } /** * 返回两个日期相差星期数(如果只传入一个日期,则与当天时间比较) * @param int|string $datetime 要计算的时间 * @param int|string $new_datetime 要比较的时间(默认为当前时间) * @return int 相差星期数 */ public static function diffWeeks($datetime, $new_datetime = null): int { return intval(self::diffDays($datetime, $new_datetime) / 7); } /** * 返回两个日期相差月数(如果只传入一个日期,则与当天时间比较) * @param int|string $datetime 要计算的时间 * @param int|string $new_datetime 要比较的时间(默认为当前时间) * @return int 相差月数 */ public static function diffMonths($datetime, $new_datetime = null): int { $diff = self::getDateDiff($datetime, $new_datetime); return $diff->y * 12 + $diff->m; } /** * 返回两个日期相差年数(如果只传入一个日期,则与当前时间比较) * @param int|string $datetime 要计算的时间 * @param int|string $new_datetime 要比较的时间(默认为当前时间) * @return int 相差年数 */ public static function diffYears($datetime, $new_datetime = null): int { return self::getDateDiff($datetime, $new_datetime)->y; } /** * 返回N分钟前的时间戳,传入第二个参数,则从该时间开始计算 * @param int $minute 分钟数(默认为1分钟) * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @param bool $round 是否取整(默认false),如果传入true,则返回当前分钟0秒的时间戳 * @return int 时间戳 */ public static function beforeMinute(int $minute = 1, $datetime = null, bool $round = false): int { $timestamp = self::modifyTimestamp(sprintf('-%d minute', $minute), $datetime); return $round ? strtotime(date('Y-m-d H:i:00', $timestamp)) : $timestamp; } /** * 返回N分钟后的时间戳,传入第二个参数,则从该时间开始计算 * @param int $minute 分钟数(默认为1分钟) * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @param bool $round 是否取整(默认false),如果传入true,则返回当前分钟0秒的时间戳 * @return int 时间戳 */ public static function afterMinute(int $minute = 1, $datetime = null, bool $round = false): int { $timestamp = self::modifyTimestamp(sprintf('+%d minute', $minute), $datetime); return $round ? strtotime(date('Y-m-d H:i:00', $timestamp)) : $timestamp; } /** * 返回N小时前的时间戳,传入第二个参数,则从该时间开始计算 * @param int $hour 小时数(默认为1小时) * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @param bool $round 是否取整(默认false),如果传入true,则返回当前小时0分钟的时间戳 * @return int 时间戳 */ public static function beforeHour(int $hour = 1, $datetime = null, bool $round = false): int { $timestamp = self::modifyTimestamp(sprintf('-%d hour', $hour), $datetime); return $round ? strtotime(date('Y-m-d H:00:00', $timestamp)) : $timestamp; } /** * 返回N小时后的时间戳,传入第二个参数,则从该时间开始计算 * @param int $hour 小时数(默认为1小时) * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @param bool $round 是否取整(默认false),如果传入true,则返回当前小时0分钟的时间戳 * @return int 时间戳 */ public static function afterHour(int $hour = 1, $datetime = null, bool $round = false): int { $timestamp = self::modifyTimestamp(sprintf('+%d hour', $hour), $datetime); return $round ? strtotime(date('Y-m-d H:00:00', $timestamp)) : $timestamp; } /** * 返回N天前的时间戳,传入第二个参数,则从该时间开始计算 * @param int $day 天数(默认为1天) * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @param bool $round 是否取整(默认false),如果传入true,则返回当前日期0点的时间戳 * @return int 时间戳 */ public static function beforeDay(int $day = 1, $datetime = null, bool $round = false): int { $timestamp = self::modifyTimestamp(sprintf('-%d day', $day), $datetime); return $round ? strtotime(date('Y-m-d 00:00:00', $timestamp)) : $timestamp; } /** * 返回N天后的时间戳,传入第二个参数,则从该时间开始计算 * @param int $day 天数(默认为1天) * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @param bool $round 是否取整(默认false),如果传入true,则返回当前日期0点的时间戳 * @return int 时间戳 */ public static function afterDay(int $day = 1, $datetime = null, bool $round = false): int { $timestamp = self::modifyTimestamp(sprintf('+%d day', $day), $datetime); return $round ? strtotime(date('Y-m-d 00:00:00', $timestamp)) : $timestamp; } /** * 返回N星期前的时间戳,传入第二个参数,则从该时间开始计算 * @param int $week 星期数(默认为1星期) * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @param bool $round 是否取整(默认false),如果传入true,则返回当前日期0点的时间戳 * @return int 时间戳 */ public static function beforeWeek(int $week = 1, $datetime = null, bool $round = false): int { $timestamp = self::modifyTimestamp(sprintf('-%d week', $week), $datetime); return $round ? strtotime(date('Y-m-d 00:00:00', $timestamp)) : $timestamp; } /** * 返回N星期后的时间戳,传入第二个参数,则从该时间开始计算 * @param int $week 星期数(默认为1星期) * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @param bool $round 是否取整(默认false),如果传入true,则返回当前日期0点的时间戳 * @return int 时间戳 */ public static function afterWeek(int $week = 1, $datetime = null, bool $round = false): int { $timestamp = self::modifyTimestamp(sprintf('+%d week', $week), $datetime); return $round ? strtotime(date('Y-m-d 00:00:00', $timestamp)) : $timestamp; } /** * 返回N月前的时间戳,传入第二个参数,则从该时间开始计算 * @param int $month 月数(默认为1个月) * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @param bool $round 是否取整(默认false),如果传入true,则返回当前日期1号0点的时间戳 * @return int 时间戳 */ public static function beforeMonth(int $month = 1, $datetime = null, bool $round = false): int { $timestamp = self::modifyTimestamp(sprintf('-%d month', $month), $datetime); return $round ? strtotime(date('Y-m-1 00:00:00', $timestamp)) : $timestamp; } /** * 返回N月后的时间戳,传入第二个参数,则从该时间开始计算 * @param int $month 月数(默认为1个月) * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @param bool $round 是否取整(默认false),如果传入true,则返回当前日期1号0点的时间戳 * @return int 时间戳 */ public static function afterMonth(int $month = 1, $datetime = null, bool $round = false): int { $timestamp = self::modifyTimestamp(sprintf('+%d month', $month), $datetime); return $round ? strtotime(date('Y-m-1 00:00:00', $timestamp)) : $timestamp; } /** * 返回N年前的时间戳,传入第二个参数,则从该时间开始计算 * @param int $year 年数(默认为1年) * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @param bool $round 是否取整(默认false),如果传入true,则返回当前日期1月1号0点的时间戳 * @return int 时间戳 */ public static function beforeYear(int $year = 1, $datetime = null, bool $round = false): int { $timestamp = self::modifyTimestamp(sprintf('-%d year', $year), $datetime); return $round ? strtotime(date('Y-1-1 00:00:00', $timestamp)) : $timestamp; } /** * 返回N年后的时间戳,传入第二个参数,则从该时间开始计算 * @param int $year 年数(默认为1年) * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @param bool $round 是否取整(默认false),如果传入true,则返回当前日期1月1号0点的时间戳 * @return int 时间戳 */ public static function afterYear(int $year = 1, $datetime = null, bool $round = false): int { $timestamp = self::modifyTimestamp(sprintf('+%d year', $year), $datetime); return $round ? strtotime(date('Y-1-1 00:00:00', $timestamp)) : $timestamp; } /** * 获得秒级/毫秒级/微秒级/纳秒级时间戳 * @param int $level 默认0,获得秒级时间戳. 1.毫秒级时间戳; 2.微秒级时间戳; 3.纳米级时间戳 * @return int 时间戳 */ public static function getTimestamp(int $level = 0): int { if ($level === 0) return time(); list($msc, $sec) = explode(' ', microtime()); if ($level === 1) { return intval(sprintf('%.0f', (floatval($msc) + floatval($sec)) * 1000)); } elseif ($level === 2) { return intval(sprintf('%.0f', (floatval($msc) + floatval($sec)) * 1000 * 1000)); } else { return intval(sprintf('%.0f', (floatval($msc) + floatval($sec)) * 1000 * 1000 * 1000)); } } /** * 获得毫秒级的时间戳 * @return int 毫秒级时间戳 */ public static function getMilliTimestamp(): int { return self::getTimestamp(1); } /** * 获得微秒级的时间戳 * @return int 微秒级时间戳 */ public static function getMicroTimestamp(): int { return self::getTimestamp(2); } /** * 获得纳秒级的时间戳 * @return int 纳秒级时间戳 */ public static function getNanoTimestamp(): int { return self::getTimestamp(3); } /** * 判断该日期是否为闰年 * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @return bool 闰年返回true,否则返回false */ public static function isLeapYear($datetime = null): bool { return self::format('L', $datetime) == 1; } /** * 判断该日期的当年有多少天 * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @return int 该年的天数 */ public static function daysInYear($datetime = null): int { return self::isLeapYear($datetime) ? 366 : 365; } /** * 判断该日期的当月有多少天 * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @return int 该月的天数 */ public static function daysInMonth($datetime = null): int { return intval(self::format('t', $datetime)); } /** * 不同时区的时间转换 * @param string $toTimezone 目标时区 * @param string|null $fromTimezone 原时区(默认为当前PHP运行环境所设置的时区) * @param int|string $datetime 任意格式的时间字符串或时间戳(默认为当前时间) * @param string $format 格式化字符串 * @return string * @throws Exception */ public static function timezoneFormat(string $toTimezone, ?string $fromTimezone = null, $datetime = 'now', string $format = 'Y-m-d H:i:s'): string { if (self::isTimestamp($datetime)) { $date = new DateTime(); $date->setTimestamp($datetime); $date->setTimezone(new DateTimeZone('UTC')); } else { if ($fromTimezone === null) { $fromTimezone = date_default_timezone_get(); } $date = new DateTime($datetime, new DateTimeZone($fromTimezone)); } $date->setTimezone(new DateTimeZone($toTimezone)); return $date->format($format); } /** * 比较两个时间的大小,如果第二个参数为空,则与当前时间比较 * @param $datetime1 * @param $datetime2 * @return int 第一个时间大于第二个时间则返回1,小于则返回-1,相等时则返回0 */ public static function compare($datetime1, $datetime2 = null): int { $timestamp1 = self::toTimestamp($datetime1); $timestamp2 = self::toTimestamp($datetime2); if ($timestamp1 > $timestamp2) { return 1; } elseif ($timestamp1 < $timestamp2) { return -1; } else { return 0; } } /** * 获取当前时间 * @param string $format 格式化字符串(默认为:Y-m-d H:i:s) * @return string */ public static function now(string $format = 'Y-m-d H:i:s'): string { return date($format); } /** * 返回当前的年份,如2025 * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @return int 当前年份 */ public static function getYear($datetime = null): int { return intval(date('Y', self::toTimestamp($datetime))); } /** * 返回当前的季度,如1,2,3,4 * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @return int 当前季度 */ public static function getQuarter($datetime = null): int { return intval(ceil(date('n', self::toTimestamp($datetime)) / 3)); } /** * 返回当前的月份,如1,2,3,4,5,6,7,8,9,10,11,12 * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @return int 当前月份 */ public static function getMonth($datetime = null): int { return intval(date('m', self::toTimestamp($datetime))); } /** * 返回当前是该年的第几周 * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @return int 当前周数 */ public static function getWeek($datetime = null): int { return intval(date('W', self::toTimestamp($datetime))); } /** * 返回当前是该月的第几天 * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @return int 当前天数 */ public static function getDay($datetime = null): int { return intval(date('d', self::toTimestamp($datetime))); } /** * 返回当前是该天的第几小时 * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @return int 当前小时数 */ public static function getHour($datetime = null): int { return intval(date('H', self::toTimestamp($datetime))); } /** * 返回当前是该小时的第几分钟 * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @return int 当前分钟数 */ public static function getMinute($datetime = null): int { return intval(date('i', self::toTimestamp($datetime))); } /** * 返回当前是该分钟的第几秒 * @param int|string $datetime 任意格式时间字符串或时间戳(默认为当前时间) * @return int 当前秒数 */ public static function getSecond($datetime = null): int { return intval(date('s', self::toTimestamp($datetime))); } /** * 后续开发计划: * TODO: 修改before和after相关方法,使取整不仅可以向前,还可以向后,如afterMonth可以返回该月最后一天的23点59分59秒的时间戳 * TODO: 返回日期范围,主要用于SQL查询, 比如今天,昨天,最近7天, 本月, 本周等等. 为了方便使用, 会另外再起一个类 */ }
最新发布
11-14
就框架 接口文档如下: 2.站点报表数据 接口基本信息 接口说明 获取站点报表数据 请求方式 http://58.17.73.100:8081/dataapi/GetReportData?ParentId=b637b7ad-34ca-4062-8e52-2ffd3bb62f20&Level=4&StationTypeId=6&ReportTimeType=3&sdtDate=2020-09-26 10:00&edtDate=2020-09-26 10:00 接口地址 Get 请求参数(区分大小写): 参数名称 属性类型 最大长度 必须 详细说明 ReportTimeType int 是 报表时间类型:分钟=10; 小时 =3;天=4;月=6;年=8 ParentId string 否 父级编号,用于指定宜春市 Level int 是 站点层级,市=2;区=3;常规站=4;微型站=5; StationTypeId int 否 站点类型,国控站=1;省控站=2;工业园区站=6;乡镇街道站=5; StationId string 否 站点编号,指定某一站点数据 sdtDate dateTime 是 开始时间,严格的时间格式,如果是年月,以yyyy-MM-dd格式为标准 edtDate dateTime 是 结束时间,严格的时间格式,如果是年月,以yyyy-MM-dd格式为标准 响应参数Data: 参数名称 属性类型 最大长度 必须 详细说明 StationId string 是 站点编号 StationName string 是 站点名称 Level int 是 站点层级,市=2;区=3;常规站=4;微型站=5; StationTypeId int 是 站点类型,国控站=1;省控站=2;工业园区站=6;乡镇街道站=5; Latitude string 是 纬度 Longitude string 是 经度 TimePoint dateTime 是 时间点 PM2_5 string 是 污染物浓度 PM2_5_Level int 是 污染物等级,日以上不计算 PM2_5_IAQI/PM2_5_Index string 是 污染物IAQI/分指数 AirPress string 是 气压 AirTemperature string 是 气温 RelHumidity string 是 湿度 WindDirection string 是 风向 WindSpeed string 是 风速 AQI/ComplexIndex string 是 AQI/综合指数 LevelText string 是 污染等级文字描述 AQI_Level/ComplexIndex_Level int 是 污染等级 PrimaryPollutant string 是 首要污染物 响应示例: {     "Result": true,     "Data": [         {             "StationId": "1DAFC008-BA10-4839-BF3D-54F0B201D66B",             "StationName": "上高县工业园",             "Level": 4,             "StationTypeId": "6",             "Latitude": "28.25888889",             "Longitude": "114.9552778",             "TimePoint": "2020-09-26 10:00",             "PM2_5": "11",             "PM2_5_Level": 1,             "PM2_5_IAQI": "16",             "PM10": "55",             "PM10_Level": 2,             "PM10_IAQI": "53",             "SO2": "3",             "SO2_Level": 1,             "SO2_IAQI": "1",             "NO2": "14",             "NO2_Level": 1,             "NO2_IAQI": "7",             "CO": "0.6",             "CO_Level": 1,             "CO_IAQI": "6",             "O3": "46",             "O3_Level": 1,             "O3_IAQI": "15",             "AirPress": "100.8",             "AirTemperature": "19.9",             "RelHumidity": "94",             "WindDirection": "75.5",             "WindSpeed": "0.8",             "AQI": "53",             "LevelText": "良",             "AQI_Level": 2,             "PrimaryPollutant": "PM10"         },         ......     ],     "Message": "" } 新框架已有代码: /// <summary> /// 兼容旧APP接口 /// </summary> // 旧接口响应结构 public class ApiResponse { public bool Result { get; set; } public List<OldStationResponse> Data { get; set; } public string Message { get; set; } } // 旧接口站点数据结构 public class OldStationResponse { public string Id { get; set; } public string Name { get; set; } public int Level { get; set; } public string ParentId { get; set; } public List<OldStationAttribute> Attribute { get; set; } } // 旧接口属性结构 public class OldStationAttribute { public string StationId { get; set; } public string Value { get; set; } public string Id { get; set; } = Guid.NewGuid().ToString(); public string Item { get; set; } } /// <summary> /// 获取站点信息(兼容旧接口数据结构) /// </summary> [HttpGet] [Route("GetNewStations")] public ApiResponse GetNewStations( string ParentId = null, int? Level = null, int? StationTypeId = null, string StationId = null) { try { ParentId = "360900"; //固定 宜春 // Level int 是 站点层级,市 = 2;区 = 3;常规站 = 4;微型站 = 5; //StationTypeId 站点类型,国控站 = 1;省控站 = 2;工业园区站 = 6;乡镇街道站 = 5; //情况1 Level = 4 & StationTypeId = 6 常规站,工业园区站 //情况2 Level = 5 & StationTypeId = 5 微型站,乡镇街道站 // 1. 获取所有站点数据 var stations = UserStationDtoList.ToList(); // 2. 转换为旧接口数据结构 var oldStations = stations .Where(s => (string.IsNullOrEmpty(ParentId) || s.CityCode == ParentId) && (!Level.HasValue || GetStationLevel(s) == Level.Value) && (!StationTypeId.HasValue || s.StationTypeId == StationTypeId.Value) && (string.IsNullOrEmpty(StationId) || s.UniqueCode == StationId) ) .Select(s => new OldStationResponse { Id = s.UniqueCode, Name = s.PositionName, Level = GetStationLevel(s), ParentId = s.AreaCode, Attribute = CreateAttributes(s) }) .ToList(); return new ApiResponse { Result = true, Data = oldStations, Message = "" }; } catch (Exception ex) { return new ApiResponse { Result = false, Data = null, Message = ex.Message }; } } // 辅助方法:根据站点类型确定层级 private int GetStationLevel(BSDStationDto station) { return station.StationTypeId switch { 5 => 5, // 微型站 6 => 4, // 工业园区站 _ => 4 // 默认常规站 }; } // 创建属性数组(行转列) private List<OldStationAttribute> CreateAttributes(BSDStationDto station) { return new List<OldStationAttribute> { new() { Item = "StationTypeId", Value = station.StationTypeId?.ToString() ?? "" }, new() { Item = "IsContant", Value = station.Status ? "1" : "0" }, new() { Item = "StationCode", Value = station.StationCode ?? "" }, new() { Item = "Address", Value = station.Address ?? "" }, new() { Item = "Longitude", Value = station.Longitude ?? "" }, new() { Item = "Latitude", Value = station.Latitude ?? "" }, new() { Item = "DeviceId", Value = station.Mn ?? "" } }; } 请帮我写出方法GetReportData,以及需要的返回类,获取数据方法未知 待定。 public ApiResponse GetReportData(int ReportTimeType, string ParentId = null, int? Level = null, int? StationTypeId = null, string StationId = null ,DateTime ?sdtDate = null, DateTime? edtDate = null) { } // 旧接口响应结构 public class ApiResponse { public bool Result { get; set; } public List<OldStationResponse> Data { get; set; } public string Message { get; set; } } 这部分需要修改 Data 应该要 string 类型 返回json数据 ,这样 GetReportData 也可以用这个返回类,后续其他接口也用这个通用返回类
08-19
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值