CAPL内置的时间函数

在CAPL中我们要经常和时间打交道,为了方便的写CAPL脚本,所以我整理了Vector官方提供的与时间有关的函数,并对常用的进行简单说明。

本文主体部分摘录了Vector的官方文档,做了整理与翻译;另外增加了一些我自己的理解与编写的实际函数 。

一、CAPL中与时间管理有关的函数

Windows和Linux支持这些CAPL功能。Linux下的功能尚未经过全面测试。

Functions Short Description
addTimeToMeasurementStartTime 计算测量开始的绝对日期/时间加上偏移量
cancelTimer 停止一个已经激活的定时器
convertGPSTimestamp 将GPS时间戳转换为基于UTC的日期和时间信息
convertTimestamp 将时间戳转换为单独的部分
convertTimestampNS 将时间戳转换为单独的部分
convertTimestampToNS 将以天、小时、分钟和秒为单位的时间戳转换为纳秒时间戳
convertUTCDateToUnixTimestamp 将给定的UTC时间和日期转换为UNIX时间戳(自1970-01-01以来的秒数)
EnvVarTimeNS 返回环境变量 envVariable 的时间戳(以纳秒为单位)
getDrift Determines the constant deviation when Drift is set.
getGPSTimeString Copies a printed representation of the GPS time stamp represented as UTC date and time into the supplied character buffer.
getJitterMax 确定设置抖动(Jitter)时允许偏差的上限
getJitterMin 确定设置抖动(Jitter)时允许偏差的下限
getLocalTime 返回当前日期和时间的详细信息
getLocalTimeString 复制当前日期和时间的打印表示
getMeasurementStartTime 返回有关开始测量的绝对时间的详细信息
isTimerActive 返回值指示特定计时器是否处于活动状态
MessageTimeNS 返回以纳秒为单位的时间戳
setDrift 为网络节点的计时器设置恒定偏差
setJitter 设置网络节点的计时器的抖动间隔(Jitter interval)
setTimer 设置一个定时器
setTimerCyclic 设置一个周期性的定时器
timeDiff 消息之间或消息与当前时间之间的时间差(毫秒)
timeNow 提供当前模拟时间[10微秒]
timeNowFloat 提供当前模拟时间[10微秒]
timeNowInt64 提供当前模拟时间[纳秒]
timeNowNS 提供当前模拟时间[纳秒]
timeToElapse 返回一个值,该值指示在调用计时器上的事件过程之前还要经过多少时间

二、 获取当前的模拟时间

这里主要介绍 timeNow()timeNowFloat()TimeNowNS() 三个函数,它们的作用几乎相同,主要是返回值类型不同。

2.1 函数: timeNow()

返回当前模拟时间(最长时间:.2^32*10微秒 = 11小时55分49秒672毫秒96微秒)

函数语法

dword timeNow();

函数功能描述

模拟时间(The simulation time)可以与网络接口的硬件结果相关联。这个时间的分辨率取决于所使用的硬件(通常是一毫秒或更好)。

根据硬件配置的不同,模拟时间(The simulation time)可能有两种情况:

  • 将与网络接口计算的消息时间相同。
  • 消息时间将具有更高的准确性(accuracy)

返回值介绍

模拟时间(单位:10微秒)

举例说明

示例代码:

float x;
x = timeNow()/100000.0; //current time in seconds

2.2 函数: timeNowFloat()

这个函数与timeNow()函数功能几乎完全相同,只有返回值类型有点不同,语法如下:

float timeNowFloat();

示例代码:

float x;
x = timeNowFloat()/100000.0; //current time in seconds

2.3 函数: TimeNowNS()

这个函数与timeNowFloat()函数功能相同,提供当前模拟时间,但是以纳秒为单位,语法如下:

float TimeNowNS();

三、获取机器的绝对时间以及测量的绝对时间

这里主要介绍:

  1. getLocalTime()getLocalTimeString() 它们的作用几乎相同,主要是返回值类型不同。
  2. getMeasurementStartTime()addTimeToMeasurementStartTime() 它们通常配合使用,记录测量的绝对时间。

3.1. 函数: getLocalTime()

函数语法

void getLocalTime(long time[]);

函数功能描述

long类型的数组(array)返回当前日期和时间的详细信息。

分布式模式(distributed mode)使用的注意事项:
此函数始终返回用户计算机的本地时间。

函数参数介绍

类型为long的数组(array),至少有9个条目。
数组的条目将填充以下信息:

Index Information
0 Seconds (0 - 59)
1 Minutes (0 - 59)
2 Hours (0 - 23)
3 Day of month (1 - 31)
4 Month (0 - 11)
5 Year (0 - xxx, offset of 1900, e.g. 117 = 2017)
6 Day of week (0 - 6, sunday is 0)
7 Day of Year (0 - 365)
8 Flag for daylight saving time (0 - 1, 1 = daylight saving time)

注意: 这个精确度只能到秒,如果需要更高的精确度,则不能使用这个函数。

举例说明

示例代码:

long tm[9];
getLocalTime(tm);
// now tm contains the following entries:
// tm[0] = 3; (seconds)
// tm[1] = 51; (minutes)
// tm[2] = 16; (hours)
// tm[3] = 21; (day of month)
// tm[4] = 7; (month stating with 0)
// tm[5] = 98; (year)
// tm[6] = 5; (weekday)
// tm[7] = 232;(day of year)
// tm[8] = 1; (Summer time)

我写的一个可以格式化当前时间的函数(本文的后面章节中,我会用到这个函数):

// 获取当前时间(精确到秒), 入参保证至少要容纳20个字符
void get_current_Local_Time(char time_str[]){long tm[9];getLocalTime(tm);snprintf(time_str, elcount(time_str), "%04d-%02d-%02d %02d:%02d:%02d",tm[5]+ 1900, tm[4]+1, tm[3], tm[2], tm[1], tm[0]);
}

3.2 函数: getLocalTimeString()

函数语法

void getLocalTimeString(char timeBuffer[]);

函数功能描述

将当前日期和时间的打印表示复制到提供的字符缓冲区中。字符串的格式为ddd mmm dd hh:mm:ss jjj(例如"Fri Aug 21 15:22:24 1998")。

分布式模式(distributed mode)使用的注意事项:
此函数始终返回用户计算机的本地时间。

函数参数介绍

参数 含义
timeBuffer 将写入字符串的缓冲区(buffer)。此缓冲区必须至少有26个字符长

举例说明

示例代码:

char timeBuffer[64];getLocalTimeString(timeBuffer);
// now timeBuffer contains for example. "Fri Aug 21 15:22:24 1998"

3.3 函数: getMeasurementStartTime()

函数语法

long getMeasurementStartTime(long time[]);

函数功能描述

返回有关开始测量的绝对时间的详细信息。

注意事项:
在CANoe的offline模式下,函数返回日志文件中存储的最早开始时间。

函数参数介绍

只有一个参数time, 它的类型为 long类型的数组(array) ,至少有8个条目。数组的条目将填充以下信息:

Index Information
0 Milliseconds (0 - 999)
1 Seconds (0 - 59)
2 Minutes (0 - 59)
3 Hours (0 - 23)
4 Day of month (1 - 31)
5 Month (0 - 11)
6 Year (0 - xxx, offset of 1900, e.g. 117 = 2017)
7 Day of week (0 - 6, Sunday is 0)

返回值介绍

1 if successful, 0 if not (e.g. array to small).

举例说明

我写的一个获取测量开始时间的函数:

variables
{char g_Measure_Start_Time[24];
}// 获取测量开始时间(精确到毫秒), 存储到全局变量g_Measure_Start_Time中
void get_MeasurementStartTime_Str(){long time[8];getMeasurementStartTime(time);snprintf(g_Measure_Start_Time, elcount(g_Measure_Start_Time), "%04d-%02d-%02d %02d:%02d:%02d",time[6]+ 1900, time[5]+1, time[4], time[3], time[2], time[1], time[0]);
}

获取测量开始时间的举例,注意:这里使用到了本文前面部分定义的两个函数:

on key 's'
{char temp_local_time[20];get_MeasurementStartTime_Str();write("Measurement was started on %s",g_Measure_Start_Time);get_current_Local_Time(temp_local_time);write("current time is: %s",temp_local_time);
}

输出结果:

3.4 函数: addTimeToMeasurementStartTime()

函数语法

long addTimeMeasurementStartTime(int64 timeSpan, long time[]);

函数功能描述

计算测量开始的绝对日期/时间加上偏移量(例如时间戳)。

函数参数介绍

  • 参数timeSpan : 要添加到测量开始时间的时间,例如测量帧的时间戳。

  • 参数time : 与 getMeasurementStartTime() 函数中的入参完全一致

示例代码:

on errorframe
{long time[8];addTimeToMeasurementStartTime(timeNowNS(), time);write("ErrorFrame occured on %02d/%02d/%02d %02d:%02d:%02d.%-3d",time[5]+1, time[4], time[6]-100, time[3], time[2], time[1], time[0]);getMeasurementStartTime(time);write("Measurement was started on %02d/%02d/%02d %02d:%02d:%02d.%-3d",time[5]+1, time[4], time[6]-100, time[3], time[2], time[1], time[0]);
}// Output e.g.:
// ErrorFrame occured on 08/15/17 14:39:46.787
// Measurement was started on 08/15/17 14:39:29.547

四、时间转换

这里主要介绍 convertTimestamp(), convertTimestampNS() 两个函数。

4.1. 函数: convertTimestamp()

函数语法

void convertTimestamp(dword timestamp, dword& days, byte& hours, byte& minutes, byte& seconds, word& milliSeconds, word& microSeconds);

函数功能描述

将时间戳转(time stamp)换为单独的部分(最长时间:.2^32*10微秒=11小时55分49秒672毫秒96微秒)。

函数参数介绍

参数 含义
timestamp 以10微秒为单位的时间戳
days 接收的时间戳的天数
hours 接收的时间戳的小时数(介于0和23之间)
minutes 接收的时间戳的分钟数(介于0和59之间)
seconds 接收的时间戳的秒数(介于0和59之间)
milliseconds 接收的时间戳的毫秒数(介于0和999之间)
microseconds 接收的时间戳的微秒(介于0和999之间)

示例代码可以参见函数convertTimestampNS()中的介绍。

4.2. 函数: convertTimestampNS()

函数语法

void convertTimestampNS(qword timestamp, dword& days, byte& hours, byte& minutes, byte& seconds, word& milliSeconds, word& microSeconds, word& nanoSeconds);

函数功能描述

将时间戳转(time stamp)换为单独的部分。

函数参数介绍

参数 含义
timestamp 以纳秒为单位的时间戳
days 接收的时间戳的天数
hours 接收的时间戳的小时数(介于0和23之间)
minutes 接收的时间戳的分钟数(介于0和59之间)
seconds 接收的时间戳的秒数(介于0和59之间)
milliseconds 接收的时间戳的毫秒数(介于0和999之间)
microseconds 接收的时间戳的微秒(介于0和999之间)
nanoseconds 接收的时间戳的纳秒(介于0和999之间)

举例说明

示例代码:

on envVar EnvGearUp
{dword d;byte h, m, s;word ms, us, ns;convertTimestampNS(timeNowNS(), d, h, m, s, ms, us, ns);write("Gear up at %d days, %d::%d::%d,%d.%d.%d", d, h, m, s, ms, us, ns);
}

五、定时器

这里主要介绍:

  1. setTimer() : 普通的单次定时器。
  2. setTimerCyclic(): 普通的周期性定时器
  3. cancelTimer(): 停止一个定时器

5.1. 函数: setTimer()

函数语法

void setTimer(msTimer t, long duration); // form 1void setTimer(timer t, long duration); // form 2void setTimer(timer t, long durationSec, long durationNanoSec); // form 3

如果使用面向对象编程,语法如下:

void msTimer::set(long);

函数功能描述

设置一个定时器

函数参数介绍

Timer or msTimer variable and an expression which specifies the duration of the timer.

举例说明

示例代码:

variables
{msTimer t1;Timer t23;char press_key[4];float current_time;
}void print_current_time(char press_key[]){current_time = timeNow()/100000.0;write("%s press time: %fs", press_key, current_time);
}on key F1 {print_current_time("F1");setTimer(t1, 200); // set timer t1 to 200 ms
}on key F2 {print_current_time("F2");setTimer (t23, 2); // set timer t23 to 2 sec
}on key F3 {print_current_time("F3");setTimer (t23, 0, 1250*1000 ); // set timer t23 to 1.250 milliseconds
}on timer t1 {write("current_time:%fs, F1 was pressed 200ms ago", timeNow()/100000.0);
}on timer t23 {write("current_time:%fs, F2 was pressed 2 sec ago or F3 1250000 nsec ago", timeNow()/100000.0);
}

输出结果:

5.2. 函数: setTimerCyclic()

函数语法

void setTimerCyclic(msTimer t, long firstDuration, long period); // form 1void setTimerCyclic(msTimer t, long period); // form 2void setTimerCyclic(timer t, int64 periodInNs); // form 3

如果使用面向对象编程,语法如下:

void msTimer::setCyclic(long firstDuration, long period);void msTimer::setCyclic(long period);void timer::setCyclic(int64 periodInNs);

函数功能描述

设置一个周期性的定时器;

对于形式2,firstDuration隐式地与period相同,即计时器精确地根据第一次的period运行。

函数参数介绍

参数 含义
t The timer to be set.
firstDuration 计时器第一次用完之前的时间(以毫秒为单位)
period 计时器在过期时重新启动的时间(以毫秒为单位)
periodInNs 计时器在过期时重新启动的时间(以纳秒为单位)

举例说明

start 中通过setTimerCyclic设置周期性定时器, 示例代码:

variables
{msTimer t;float current_time;
}
on start {setTimerCyclic(t, 10, 20);
}void print_current_time(){current_time = timeNow()/100000.0;write("current time: %fs", current_time);
}on Timer t
{print_current_time();
}

输出结果:

通过键盘按键,使用setTimerCyclic设置周期性定时器, 示例代码:

variables
{msTimer t;float current_time;
}void print_current_time(){current_time = timeNow()/100000.0;write("current time: %fs", current_time);
}on Timer t
{print_current_time();
}on key 's'
{write("current time: %fs, 'S' key Pressed , start timer", timeNow()/100000.0);setTimerCyclic(t, 10, 20);
}

输出结果:

5.3. 函数: cancelTimer()

函数语法

void cancelTimer(msTimer t); // from 1void cancelTimer(timer t); // from 2

如果使用面向对象编程,语法如下:

void msTimer::cancel();

函数功能描述

停止一个已经激活的定时器

函数参数介绍

一个 Timer 或者 msTimer 变量

举例说明

使用cancelTimer()停止普通的定时器,示例代码:

variables {float current_time;char print_time_info[100];msTimer ms_t_key;message 0x100 test_msg = {dlc = 1, byte(0) = 0xFF, dir = Tx};
}void print_current_time(char time_info[]){current_time = timeNow()/100000.0;write("current time: %fs, %s", current_time, time_info);
}on Timer ms_t_key{output(test_msg);setTimer(ms_t_key, 200);
}on key F2 {setTimer(ms_t_key, 200);  // set timer to 200msprint_current_time("F2 key pressed, Start timer!!");
}on key F3 {cancelTimer(ms_t_key);    // cancel timerprint_current_time("F3 key pressed, Cancel timer!!");
}

输出结果:

使用cancelTimer()停止周期性的定时器,示例代码:

variables
{msTimer t;float current_time;
}void print_current_time(){current_time = timeNow()/100000.0;write("current time: %fs", current_time);
}on Timer t
{print_current_time();
}on key 's'
{write("current time: %fs, 'S' key Pressed , start timer", timeNow()/100000.0);setTimerCyclic(t, 30, 100);
}on key 't' {write("current time: %fs, 'T' key Pressed , Stop timer", timeNow()/100000.0);cancelTimer(t);    // cancel timer
}

输出结果:

CAPL内置的时间函数相关推荐

  1. JS(内置对象,全局函数,事件,事件对象)

    目录 内置对象 全局函数 事件 事件对象 内置对象 <!DOCTYPE html> <html><head><meta charset="utf-8 ...

  2. python内置数字类型转换函数_Python学习 Day2-2 Python3的基本数据类型、数据内置类型转换函数...

    Python3的基本数据类型 Python 中的变量不需要声明.每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建. 在 Python 中,变量就是变量,它没有类型,我们所说的"类型& ...

  3. Python中匿名函数与内置高阶函数详解

    大家好,从今天起早起Python将持续更新由小甜同学从 初学者的角度 学习Python的笔记,其特点就是全文大多由 新手易理解 的 代码与注释及动态演示 .刚入门的读者千万不要错过! 很多人学习pyt ...

  4. python中的内置高阶函数

    一. 内置高阶函数的类型 (一).内置高阶函数map """ map():接收两个参数,一个是函数 一个是序列 map将传入的函数依次作用到序列的每个元素,并且把结果作为 ...

  5. Tornado-02-Tornado、路由进阶、视图进阶(视图中内置的钩子方法、视图方法调用顺序、冲刷缓存、用户认证)、模板语法(基本语法、内置标签或函数)

    Tornado 一.路由进阶 路由语法和参数 在路由列表的路由成员中,我们一共可以设置4个参数 url(r"/uri路径", 视图类, {"参数名":" ...

  6. fopen是python内置函数吗_Python内置的open函数,打开文件的时候可能会产生异常_学小易找答案...

    [简答题]模仿操作一回. 上传JPG. [单选题]关于唐.宋诗之间的差异,缪钺.钱钟书.启功等学者都曾有过论述,请问以下哪一项评论出自钱钟书的<谈艺录>? [单选题]"羌笛何须怨 ...

  7. python通过内置的什么函数打开一个文件_利用python进行文件操作

    这篇文章主要介绍了如何利用python进行文件操作,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下 什么是文件 文件是系统存储区域的一个命名位置,用来存储一些信息,便于后续访问.能够在非 ...

  8. python内置的数学函数_Python数字和内置数学函数

    python内置的数学函数 In this section, we will be learning about Numbers and various Math functions availabl ...

  9. Python 2.7 学习笔记 内置语句、函数、标准库

    使用任何开发语言进行软件开发,都离不开语言提供的内置库(或Api),甚至说内置库的强大及使用是否方便都会影响大家对开发语言的选择. python语言,一样提供了很多内置的功能,可供开发时使用.主要有如 ...

最新文章

  1. 关于函数式宏定义的学习
  2. JavaScript语言基础2
  3. kickstart+tftp部署redhat系统
  4. 获得程序运行结果的返回值
  5. php实现工厂模式,PHP基于工厂模式实现的计算器实例
  6. WebSen!NT的行业分类说明
  7. JavaSE——链表集合
  8. [ Typescript 手册] JavaScript `Date` 在 Typescript 中的接口
  9. 常用HTML5开发工具有哪些?
  10. Zookeeper之数据同步原理
  11. python绘制柱状图和折线图_python绘制散点图,柱状图和折线图
  12. 均匀分布、正态分布、二项分布、泊松分布、指数分布
  13. linux startx无效_Linux使用startx不能进入图形界面解决方案
  14. draggrid简单用法
  15. FLV.js播放报错,及浏览器播放flv缓存内存不足导致黑屏
  16. ftp登录成功,无法取得列表
  17. 使用uniapp注册全局组件
  18. Flutter elevation属性名称的含义
  19. Pagehelper的详细文档
  20. 思科无线AP版本15.2配置实例

热门文章

  1. 【信息安全第三章同余习题】把剩余类1(mod 5)写成模15的剩余类之和
  2. .验证哥德巴赫猜想,哥德巴赫猜想的内容是:任何一个大于2的偶数都能写成写成两个素数和的形式。 设计一个函数 int isPrime(int n)判断n是否为素数,如果n是素数,函数返回值1,否则返
  3. Python带进度条的小说爬虫(笔趣网爬小说—简约版)
  4. 输出1到2000之间的双胞胎数。双胞胎数:两质数差为2称为双胞胎数。例如227和229是一对双胞胎数,它们都是素数且差为2。
  5. 项目错误日志之Error running ‘DictDAOTest.testCount‘: Failed to resolve org.junit.platform:junit-platf
  6. System32下几乎所有文件的简单说明
  7. windows命令行指令
  8. 2020年重庆二手房数据分析
  9. 链家网沈阳二手房数据分析——从数据爬取到数据分析
  10. 【大葱虽有4大治病功效】