<< Click to Display Table of Contents >> Navigation: Multi-Protocol MasterOPC Server > Lua 5.1 Reference Manual > Function library > The TIME Library > Time Operations |
The function argument is a string presentation of a local time in the specified format.
Returns an 8-byte array (bytes 0-3 – seconds since January 1, 1970; bytes 4-6 – milliseconds; and byte 7 – 0x0a).
Example
ts = time.StringToTimeStamp("2010-05-03 12:34:45.100");
This function is reversed with respect to time.StringToTimeStamp .
The function argument is an 8-byte array (bytes 0-3 – seconds since January 1, 1970; bytes 4-6 – milliseconds; and byte 7 – 0x0a).
Returns a string presentation of a local time in the specified format.
Example
ts = time.StringToTimeStamp("2010-05-03 12:34:45.100");
stringts = time.TimeStampToString(ts);
The function arguments are: a time in the format os.time , and number of milliseconds. A time in the format os.time can be formed by the function time.PackTime, time.TimeNow or time.StringToTimeStamp.
Returns an 8-byte array (bytes 0-3 – seconds since January 1, 1970; bytes 4-6 – milliseconds; and byte 7 – 0x0a).
Example
timesec = os.time{year=2010,month=05,day=03,hour=12,min=34,sec=45};
timemsec=200;
ts = time.TimeToTimeStamp(timesec,timemsec);
This function is reversed with respect to time.TimeToTimeStamp .
The function argument is an 8-byte array (bytes 0-3 – seconds since January 1, 1970; bytes 4-6 – milliseconds; and byte 7 – 0x0a).
Returns a time in the format os.time , and number of milliseconds.
Example
timesec = os.time{year=2010,month=05,day=03,hour=12,min=34,sec=45};
timemsec=200;
ts = time.TimeToTimeStamp(timesec,timemsec);
timesec,timemsec = time.TimeStampToTime(ts);
Returns a current OS time.
Example
timesec= time.TimeNow( );
Returns a current OS time in the timestamp format (date&time + ms).
Collects a timestamp from separate elements.
The function arguments are: 6 numerical elements (year, month, day, hours, minutes and seconds).
Returns an error code (0 if there is no an error), and a formed timestamp in the format os.time - number of seconds from January 1, 1970 to UTC. If an error occurs, returns January1, 1970.
Note. Pay attention to that the function produces a time in the format UTC. If you need to get a local time (for example, for sending to a device), you have to add the respective number of hours, using the function time.TimeAddHour. The function time.OffsetLocalTime returns an offset of a current timezone of a computer. But if a result of the function is destined to form an OPC variable timestamp by the function time.ToTimeStamp, you do not need to convert a time to a local one, because that function requires a time in the UTC format.
Error codes are:
0 – no errors
2 – year error (from 1970 through 2037)
3 – month error (from 1 through 12);
4 – day error (from 1 through 31)
5 – hour error (from 0 through 23)
6 – minute error (from 0 through 59)
7 – second error (from 0 through 59)
Example 1
err,timesec = time.PackTime(2012,5,3,12,31,45);
--err=0, timesec equals May 3, 2012, 12:31:45
Example 2
err,timesec = time.PackTime(2011,2,29,12,31,45);
--err=4 (there is no February 29 in 2011), therefore a date of timesec equals 01.01.1970
This function is reversed with respect to time.PackTime , that is, it divides a local time to separate elements.
The function argument is a time in the format os.time .
Returns a 6-byte array: year (1), month (2), day (3), hours (4), minutes (5) and seconds (6).
Example
err,timesec = time.PackTime(2012,5,3,12,31,45);
w={};
w=time.UnpackTime(timesec);
--w[1]=2012, w[2]=5, w[3]=3, w[4]=12, w[5]=31, w[6]=45
Converts a time received in the format os.time to a string.
Example
local Now=time.TimeNow();
local str=time.TimeToString(Now );
-- str contains a string presentation of the current time
Adds the specified number of seconds to a time variable.
The function arguments are: a time in the format os.time , and a number of seconds to be added (a signed integer).
Example 1
local Now=time.TimeNow();
local dt=time.TimeAddSec(Now,10);
-- value of dt is greater than current time by 10 seconds
Example 2
local Now=time.TimeNow();
local dt=time.TimeAddSec(Now,-10);
-- value of dt is less than current time by 10 seconds
Adds the specified number of minutes to a time variable.
The function arguments are: a time in the format os.time , and a number of minutes to be added (a signed integer).
Example
local Now=time.TimeNow();
local dt=time.TimeAddMin (Now,10);
-- value of dt is greater than current time by 10 minutes
Adds the specified number of hours to a time variable.
The function arguments are: a time in the format os.time , and a number of hours to be added (a signed integer).
Example
local Now=time.TimeNow();
local dt=time.TimeAddHour (Now,5);
-- value of dt is greater than current time by 5 hours
Adds the specified number of days to a time variable.
The function arguments are: a time in the format os.time , and a number of days to be added (a signed integer).
Example
local Now=time.TimeNow();
local dt=time.TimeAddDay (Now,1);
-- value of dt is greater than current time by 1 day
Adds the specified number of months to a time variable.
The function arguments are: a time in the format os.time , and a number of months to be added (a signed integer).
The function takes into consideration number of days in months. If adding a month leads to non-existent date, then the function returns the last month day. For instance, if 1 month is added to January 31, 2012, the function returns February 29, 2012.
In other cases, the function returns the same month day. That is, if 1 month is added to February 29, 2012, then the function returns March 29, 2012.
Example 1
local err,Time=time.PackTime(2012,2,29,0,0,0);
local dt=time.TimeAddMonth(Time,1);
-- date in the value of the variable dt is March 29, 2012
Example 2
local err,Time=time.PackTime(2012,1,31,0,0,0);
local dt=time.TimeAddMonth(Time,1);
--date in the value of the variable dt is February 29, 2012
Adds the specified number of years to a time variable.
The function arguments are: a time in the format os.time , and a number of years to be added (a signed integer).
The function takes into consideration number of days in months (an algorithm is the same as in time.TimeAddMonth ).
Example 1
local err,Time=time.PackTime(2012,1,31,0,0,0);
local dt=time.TimeAddYear (Time,1);
--date in the value of the variable dt is January 31, 2013
Example 2
local err,Time=time.PackTime(2012,2,29,0,0,0);
local dt=time.TimeAddYear (Time,1);
-- date in the value of the variable dt is February 28, 2013
The function returns an offset of a current local time of a computer. First of all, the function is used when converting a time from local to UTC and vice versa, using the function time.TimeAddHour.
Example
--assume we need form a time from elements, and send it in the local format to a device
local TimeUTC=time.PackTime(2017,5,1,0,0,0 ); --a time in the UTC format
local HourOffset=time.OffsetLocalTime( ); --an offset of the local time
local TimeLocal=time.TimeAddHour(TimeUTC,HourOffset); --add the offset; the result is a local time
--TimeLocal equals 2017-5-1 3:00:00