Date/Time处理函数总结 [To Do]

本文总结了多种编程语言中用于处理日期和时间的函数,包括Perl、Java、JavaScript和Sybase。提供了详细的函数说明和使用示例,适用于不同场景的需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

几种我所用到的用来处理日期,时间的函数总结。

[u][b]Perl[/b][/u]

1. localtime


# 0 1 2 3 4 5 6 7 8
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);


If parameter is not provides, it will use current time. Note the $year returned contains the number of years since 1900, to get a 4-digit year you need:

$year += 1900;

And the $mon starts from 0 to 11.


2. strftime


use POSIX qw(strftime);
$now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;
# or for GMT formatted appropriately for your locale:
$now_string = strftime "%a %b %e %H:%M:%S %Y", gmtime;




Strftime 时间域 ( 这个和 date 的命令的字符格式是一样的), 参见

[url]http://www.php-oa.com/2010/08/21/perl-strftime.html[/url]

% H 小时(00..23)
% I 小时(01..12)
% k 小时(0..23)
% l 小时(1..12)
% M 分(00..59)
% p 显示出AM或PM
% r 时间(hh:mm:ss AM或PM),12小时
% s 从1970年1月1日00:00:00到目前经历的秒数
% S 秒(00..59)
% T 时间(24小时制)(hh:mm:ss)
% X 显示时间的格式(%H:%M:%S)
% Z 时区 日期域
% a 星期几的简称( Sun..Sat)
% A 星期几的全称( Sunday..Saturday)
% b 月的简称(Jan..Dec)
% B 月的全称(January..December)
% c 日期和时间( Mon Nov 8 14:12:46 CST 1999)
% d 一个月的第几天(01..31)
% D 日期(mm/dd/yy)
% h 和%b选项相同
% j 一年的第几天(001..366)
% m 月(01..12)
% w 一个星期的第几天(0代表星期天)
% W 一年的第几个星期(00..53,星期一为第一天)
% x 显示日期的格式(mm/dd/yy)
% y 年的最后两个数字( 1999则是99)
% Y 年(例如:1970,1996等)

3. time

Returns the number of non-leap seconds since whatever time the system considers to be the epoch, suitable for feeding to gmtime and localtime. On most systems the epoch is 00:00:00 UTC, January 1, 1970

4. Date::Calc

Quite useful. Some methods I frequently use:

Add_Delta_Days
($year,$month,$day) =
Add_Delta_Days($year,$month,$day,
$Dd);


Today
($year,$month,$day) = Today([$gmt]);

Now
($hour,$min,$sec) = Now([$gmt]);

Delta_Days
$Dd = Delta_Days($year1,$month1,$day1,
$year2,$month2,$day2);

5. Date::Parse

[url]http://search.cpan.org/~gbarr/TimeDate-2.30/lib/Date/Parse.pm[/url]
Used to parse date string into time values.

str2time(DATE [, ZONE])

str2time parses DATE and returns a unix time value, or undef upon failure. ZONE, if given, specifies the timezone to assume when parsing if the date string does not specify a timezone.
strptime(DATE [, ZONE])

strptime takes the same arguments as str2time but returns an array of values ($ss,$mm,$hh,$day,$month,$year,$zone). Elements are only defined if they could be extracted from the date string. The $zone element is the timezone offset in seconds from GMT. An empty array is returned upon failure.

[u][b]Java[/b][/u]
参考:[url]http://tianlovv.iteye.com/blog/402116[/url]

[u][b]JavaScript[/b][/u]

创建一个日期对象:

var objDate=new Date([arguments list]);

参数形式有以下5种:

new Date("month dd,yyyy hh:mm:ss");
new Date("month dd,yyyy");
new Date(yyyy,mth,dd,hh,mm,ss);
new Date(yyyy,mth,dd);
new Date(ms);

需要注意最后一种形式,参数表示的是需要创建的时间和GMT时间1970年1月1日之间相差的毫秒数。各种函数的含义如下:

month:用英文表示月份名称,从January到December

mth:用整数表示月份,从0到11

dd:表示一个月中的第几天,从1到31

yyyy:四位数表示的年份

hh:小时数,从0(午夜)到23(晚11点)

mm:分钟数,从0到59的整数

ss:秒数,从0到59的整数

ms:毫秒数,为大于等于0的整数

参考:
[url]http://www.cnblogs.com/east-liujie/archive/2006/10/21/535784.html[/url]

[u][b]Sybase[/b][/u]
http://wang-zhi-peng2007.iteye.com/blog/1199091
#include "include.h" #include "sensor_hal.h" #include "auxiliary.h" // 软件定时器设定 static Timer timer_detect_sensor; static Timer timer_recv_data; static Timer timer_MAX30102; static Timer timer_1s; static Timer timer_key; /*本地函数*/ void HardWare_Init(void); // 硬件初始化 void SoftWare_Init(void); // 软件初始化 void UserBoot_Init(void); // 用户初始化 void detect_sensor(void); // 周期采集传感器信息 void user_send_data(const unsigned char *src, int len); // 串口发送函数 void uart_recv_IdValue(const char *id, const char *value); // 处理串口接收到的指令 void service_connect(void) { const char *mqtt_service = "423423.iotcloud.tencentdevices.com"; // mqtt服务器地址 const char *mqtt_port = "1883"; // mqtt 端口 const char *mqtt_client_ID = "1334342345"; // mqtt client 名称ID const char *mqtt_device_key = "AF8234234234FSF234AA"; // mqtt client连接秘钥 const char *mqtt_client_topic = "50FK234Fef234V3cfs"; // mqtt订阅主题 user_send_data((unsigned char *)mqtt_service, strlen(mqtt_service)); user_send_data((unsigned char *)mqtt_port, strlen(mqtt_port)); user_send_data((unsigned char *)mqtt_client_ID, strlen(mqtt_client_ID)); user_send_data((unsigned char *)mqtt_device_key, strlen(mqtt_device_key)); user_send_data((unsigned char *)mqtt_client_topic, strlen(mqtt_client_topic)); } void wireless_config() { const char *cfg_wifi_ssid = "{\"cfg_wifi_ssid\":\"wifi\"}"; const char *cfg_wifi_passwd = "{\"cfg_wifi_passwd\":\"12345678\"}"; // config wifi and gprs/4g user_send_data((unsigned char *)cfg_server_host, strlen(cfg_server_host)); // wifi only user_send_data((unsigned char *)cfg_wifi_passwd, strlen(cfg_wifi_passwd)); user_send_data((unsigned char *)cfg_wifi_ssid, strlen(cfg_wifi_ssid)); service_connect(); // 连接mqtt服务器 } void wireless_config_refresh() { static unsigned int tick_count = 0; tick_count++; if (tick_count % 20 == 3) { wireless_config(); // 初始化无线配置 } } /*****************************************/ // 硬件部分初始化 /*****************************************/ void HardWare_Init(void) { NVIC_Configuration(); /* 配置NVIC中断模式 */ delay_init(); /* 延时函数初始化 */ UART2_Init(38400); // 串口2初始化 // UART1_Init(9600); // 串口1初始化 -- 解析串口数据 // UART3_Init(9600); // 串口1初始化 -- 解析串口数据 TIM4_Int_Init(1000, 72); /* 开TIM4定时中断 计数到为 1ms */ // 5个引脚全为普通引脚,但不能再用JTAG&SWD仿真器调试,只能用st-link调试 // GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE); // 如果你用PB3,PB4,PA15做普通IO,PA13&14用于SWD调试 GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); } void func_timer_recv_data() { protocol_pipe_get_IdValue(&protocol_pipe_uart, uart_recv_IdValue); // 解析串口2的控制指令 protocol_pipe_get_IdValue(&protocol_pipe_uart_2, uart_recv_IdValue_sensor); // 解析串口 传感器数据 protocol_pipe_get_IdValue(&protocol_pipe_uart_3, uart_recv_IdValue_sensor3); // 解析串口 传感器数据 parse_uart_sensor(); // 解析串口1传感器数据 parse_uart_sensor3(); // 解析串口1传感器数据 if (GlobalVar_flag_is_inited_zhendong_DO) { process_zhendong_DO_100ms(); } if (GlobalVar_flag_liuliang_inited) { process_liuliang_100ms(); } } void SoftWare_Init(void) { // 通讯协议初始化 protocol_pipe_init(&protocol_pipe_uart, protocol_pipe_uart_buff, sizeof(protocol_pipe_uart_buff)); protocol_send_init(&send_protocol_uart, send_protocol_uart_buff, sizeof(send_protocol_uart_buff)); protocol_pipe_init(&protocol_pipe_uart_2, protocol_pipe_uart_buff_2, sizeof(protocol_pipe_uart_buff_2)); protocol_pipe_init(&protocol_pipe_uart_3, protocol_pipe_uart_buff_3, sizeof(protocol_pipe_uart_buff_3)); sensor_hal_protocol_init(&send_protocol_uart); // 初始化 // 定时器初始化 timer_init(&timer_detect_sensor, detect_sensor, 2000, 1); // 周期采集传感器信息 timer_init(&timer_recv_data, func_timer_recv_data, 100, 1); // 周期解析串口数据指令 timer_init(&timer_MAX30102, func_timer_MAX30102, 1000, 1); // MAX30102 timer_init(&timer_1s, func_timer_1s, 1000, 1); // 1秒定时器 timer_init(&timer_key, func_timer_key, 10, 1); // 按键检测 timer_start(&timer_detect_sensor); timer_start(&timer_recv_data); timer_start(&timer_MAX30102); timer_start(&timer_1s); timer_start(&timer_key); // TJC4824_Screen_Directive_send(user_send_data); // 串口显示屏发送函数注册 key_button_register(key_button_process); // 注册按键处理函数 RINGBUF_Init(&Uart_sensor_RB, uart_sensor_buff, sizeof(uart_sensor_buff)); // 初始化串口环形缓冲器 RINGBUF_Init(&Uart_sensor_RB3, uart_sensor_buff3, sizeof(uart_sensor_buff3)); // 初始化串口环形缓冲器 } /**************************************************************************************/ /***********************************main 程序入口**************************************/ /**************************************************************************************/ int main(void) { SoftWare_Init(); // 软件初始化 HardWare_Init(); // 硬件初始化 UserBoot_Init(); // 用户初始化 while (1) { timer_loop(); // 定时器执行 } } /**************************************************************************************/ // 定时器 硬件定时器震荡源 /**************************************************************************************/ void TIM4_IRQHandler(void) /* TIM4中断 */ { if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) /* 检查指定的TIM中断发生与否:TIM 中断源 */ { TIM_ClearITPendingBit(TIM4, TIM_IT_Update); /* 清除TIMx的中断待处理位:TIM 中断源 */ timer_ticks(); // 1ms tick to uodate timer if (GlobalVar_flag_key_inited) { key_button_scan_1ms(); } } } ///////////////////////////////////////////////////////////////////////////////// // // // 核心逻辑函数 // // ///////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////// // // // 核心逻辑函数 // // ///////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////// // // // 核心逻辑函数 // // ///////////////////////////////////////////////////////////////////////////////// // 上电初始化 void UserBoot_Init(void) { // SetSensor_Jidianqi_1(0); // SetSensor_Jidianqi_2(0); // SetSensor_Jidianqi_3(0); // SetSensor_Jidianqi_4(0); // SetSensor_Beep_1(0); // SetSensor_Beep_interval_1(0); // SetSensor_Deng_1(0); // SetSensor_Deng_2(0); // SetSensor_Deng_3(0); // SetSensor_Deng_4(0); // SetSensor_Fan_1(0); // SetSensor_Fan_2(0); // SetSensor_Ws2812b_rgb(0, 0, 0); // SetSensor_MOTOR(2, 5); // Getsensor_16key(); // Set_GPIO_Init_Mode_DO_PB15(1); } // 自动调节函数 // 开始自动调节判断(如高温开风扇、天黑开灯) // 1:首先判断是否开启了自动调节(如是否开启了天黑开灯的功能) // 2:如果开启了自动调节的功能,那么进入自动调节判断(如 光强低于设定的阈值,那么开灯,否则关灯) void Adjust_Sensor(void) { static unsigned int time_value = 0; unsigned char wen_value = 0, dia_value = 0, zao_value = 0, r_value = 0, g_value = 0; static unsigned char beep_value = 0; static char oled_buff[40]; memset(oled_buff, 0, sizeof(oled_buff)); sprintf(oled_buff, ":%d ", GlobalVar_sensor_ds18b20_wendu); SetSensor_OLED_Sring(32, 2, (unsigned char *)oled_buff, 16); memset(oled_buff, 0, sizeof(oled_buff)); sprintf(oled_buff, ":%d ", GlobalVar_sensor_dianliu); SetSensor_OLED_Sring(96, 2, (unsigned char *)oled_buff, 16); memset(oled_buff, 0, sizeof(oled_buff)); sprintf(oled_buff, ":%d ", GlobalVar_sensor_distance); SetSensor_OLED_Sring(48, 4, (unsigned char *)oled_buff, 16); memset(oled_buff, 0, sizeof(oled_buff)); sprintf(oled_buff, ":%d ", GlobalVar_sensor_zhendong_AO); SetSensor_OLED_Sring(32, 6, (unsigned char *)oled_buff, 16); // 温度电流噪音超声波 SetSensor_OLED_Chinese(0, 2, 0); SetSensor_OLED_Chinese(16, 2, 1); SetSensor_OLED_Chinese(64, 2, 2); SetSensor_OLED_Chinese(80, 2, 3); SetSensor_OLED_Chinese(0, 6, 4); SetSensor_OLED_Chinese(16, 6, 5); SetSensor_OLED_Chinese(0, 4, 6); SetSensor_OLED_Chinese(16, 4, 7); SetSensor_OLED_Chinese(32, 4, 8); // GlobalVar_Threshold_alert_data2 wen_value = GlobalVar_sensor_ds18b20_wendu > GlobalVar_Threshold_alert_wendu ? 1 : 0; dia_value = GlobalVar_sensor_dianliu > GlobalVar_Threshold_alert_data1 ? 1 : 0; zao_value = GlobalVar_sensor_distance < GlobalVar_Threshold_alert_distance ? 1 : 0; if (wen_value || dia_value) { time_value = 0; beep_value = 1; } else if (time_value < GlobalVar_Threshold_alert_data2) { time_value++; } else { beep_value = 0; } SetSensor_Beep_1(beep_value +zao_value); SetSensor_Jidianqi_1(!beep_value); if (beep_value) { r_value = 255; } else { g_value = 255; } if (zao_value) { r_value = 255; g_value = 255; } SetSensor_Ws2812b_rgb(r_value, g_value, 0); } void detect_sensor(void) { //---------------------------------------///// wireless_config_refresh(); // 配置wifi或无线模块 //---------------------------------------///// protocol_send_start(&send_protocol_uart); // // ################################################################ GetSensor_Time("online1"); // 离线检查 开机时间 GetSensor_date_time(); // 获取当前时间需要接收time GetSensor_Ds18b20_float("wendu1"); // ds18b20 三位小数 GetSensor_Dianliu("dianliu1"); // dianliu A1 10mA GetSensor_Zhendong_AO("zhendong1"); // 震动DO接口数据 鼓型震动传感器 声音 ---默认用这个 GetSensor_Chaoshengbo("chaoshengbo1"); // 超声波 GetSensor_GPS("x1", "y1"); // GetSensor_IdValue_int("dianliang", int_value);//直接上传IDvalue value为int // GetSensor_IdValue_str("xxx" , "xxx");//直接上传IDvalue value为string // ################################################################ Adjust_Sensor(); // 传感器调节判断 protocol_send_complete(&send_protocol_uart, user_send_data); } void uart_recv_IdValue(const char *id, const char *value) { int value_len = protocol_valuelen(value); // 获取协议的数值长度 int num_value; GlobalVar_sensor_online_response++; if (protocol_compare(id, "time")) { SetSensor_time_update(value); // 更新时间 } else if (protocol_compare(id, "l_wen1")) { num_value = protocol_atoi(value); GlobalVar_Threshold_alert_wendu = num_value; } else if (protocol_compare(id, "l_dia1")) { num_value = protocol_atoi(value); GlobalVar_Threshold_alert_data1 = num_value; } else if (protocol_compare(id, "l_cha1")) { num_value = protocol_atoi(value); GlobalVar_Threshold_alert_distance = num_value; } else if (protocol_compare(id, "l_shi1")) { num_value = protocol_atoi(value); GlobalVar_Threshold_alert_data2 = num_value; } } 对此代码进行分析
最新发布
05-19
### 嵌入式系统代码功能分析 #### 硬件初始化 硬件初始化通常涉及对微控制器外设的配置,例如GPIO、SPI、UART等接口。基于引用中的描述,在实际应用中可能需要设置SPI通信的相关参数以确保数据传输正常[^1]。以下是典型的硬件初始化代码示例: ```c void Hardware_Init(void) { // 配置 GPIO 引脚为 SPI 功能 GPIO_Setup(SPI_SCK_PIN, MODE_OUTPUT); GPIO_Setup(SPI_MISO_PIN, MODE_INPUT); GPIO_Setup(SPI_MOSI_PIN, MODE_OUTPUT); // 初始化 SPI 接口 SPI_Init(SPI_MODULE, CLOCK_DIVIDER, DATA_ORDER_MSB_FIRST); } ``` 此部分代码负责将通用输入/输出(GPIO)引脚分配给特定的功能,并配置串行外设接口(SPI),以便后续能够与其他设备进行通信。 --- #### 软件定时器 软件定时器常用于周期性任务调度或延迟操作。在嵌入式系统中,可以通过中断服务程序或者轮询机制来实现定时器功能。以下是一个简单的软件定时器实现方式: ```c volatile uint32_t timer_counter = 0; // 定时器中断服务函数 void Timer_ISR(void) { timer_counter++; } bool Check_Timer(uint32_t timeout_ms) { if (timer_counter >= timeout_ms) { timer_counter = 0; return true; } return false; } ``` 在此设计中,`Timer_ISR` 函数会在每次计数溢出时被调用并更新全局变量 `timer_counter` 的值。当检测到时间达到设定阈值时,返回真值表示超时事件发生[^1]。 --- #### 无线配置 对于无线通信模块而言,其配置过程主要包括绑定目标地址、选择工作信道以及调整发射功率等方面的内容。下面展示了一个利用 Z-Stack 协议栈发起数据请求的例子[^3]: ```c uint8 Data_Request(afAddrType_t *dst_addr, endPointDesc_t *src_ep, uint16 cluster_id, uint16 data_len, uint8 *data_buf, uint8 *trans_id, uint8 opts, uint8 radius) { status_t result = afStatus_tA_DataRequest(dst_addr, src_ep, cluster_id, data_len, data_buf, trans_id, opts, radius); return result; } ``` 这里展示了如何通过调用 `afStatus_tA_DataRequest` 来向远程节点发送消息包的过程。其中包含了目的地信息 (`dst_addr`) 和源端点描述符(`src_ep`)等内容。 --- #### 传感器数据采集 传感器作为感知外界变化的重要组件之一,在许多物联网项目里扮演着不可或缺的角色。常见的有温湿度感应器、光照强度探测仪等等[^2]。为了读取这些装置上的测量结果,往往需要用到 ADC 或者 IIC/SPI 总线来进行交互。如下所示的是一个假想出来的例子: ```c float Read_Temperature(void){ float temp_value=0.0f; uint8 raw_data[2]; // 使用 I2C 获取温度传感器寄存器内的原始数值 I2C_ReadRegisters(I2C_BUS_TEMP_SENSOR,TEMP_REG_ADDR,(uint8*)&raw_data,sizeof(raw_data)); // 将字节数组转换成浮点型摄氏度单位下的真实温度值 temp_value=(int)(raw_data[0]<<8 | raw_data[1])/SCALE_FACTOR+OFFSET_VALUE; return temp_value; } ``` 上述片段演示了怎样借助 I²C 方法访问某款具体型号的热敏电阻芯片内部存储单元进而得到当前环境空气里的热度状况的方法论思路。 --- #### 协议处理 最后关于协议解析方面的工作,则主要是围绕着接收到的数据帧展开进一步剖析判断逻辑流程图绘制等工作环节上来讲的话呢?那么我们就拿 ZigBee/Zigbee Pro Stack 中 AF 层所提供的 API 函数为例吧——即前面提到过的那个名为 **`afStatusTADataRequest()`** 的家伙啦!它允许应用程序层制定好要传递出去的消息体之后再交给 MAC 子层去执行具体的链路建立握手等一系列复杂繁琐的操作步骤直至最终完成整个通讯会话为止哦! --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值