# aardio 范例: 入门

```aardio
//入门
import console;

console.log( time.tick(),"系统运行时间(毫秒数)")

//time 库文档： https://www.aardio.com/zh-cn/doc/library-guide/builtin/time/time.html
var tm = time.now();

//指定格式化时间
tm.format = "%a %B %Y %m %d  %H:%M:%S";
tm.locale = "enu"; //整定格式化语言，无论指定什么语言，文本都必须是aardio默认的UTF8编码
console.log("时间格式化为字符串",tostring(tm));

//也可以在 tostring 函数中指定格式化参数
var str  = tostring(tm,"%Y年%m月%d日 %H时%M分%S秒","chs")
console.log("时间格式化为简体中文",str);

/*
可使用格式串参数指定的规则重新将文本解析为时间对象,
不指定时间格式串时默认值为 '%Y/%m/%d %H:%M:%S',此格式串可兼容解析 ISO8601 格式时间
*/
console.log( time("2017-05-27T16:56:01Z") )

/*
格式化输入文本并解析为时间时，使用尽可能宽松的规则：

忽略日期时间首尾部不匹配字符。
一个空白字符可以匹配任意多个空白字符。
一个标点可以匹配任意个连续的标点。
一个字母可以匹配任意个连续的字母，一个非 ASCII 字符可以匹配任意个连续的非 ASCII 字符。
支持 ISO8601 格式省略分隔符的写法。

如果输入文本中的时间数值超出日期范围，则返回 null 。
但如果出现当月不存在的日期且小于 31 号时会顺推为下月时间。

解析时如果输入文本提前结束，但解析时间未完成则返回 null 。
最后一个格式化标记解析成功以后如果还有剩余的字符串，
首先跳过前面的空白字符，其他尾部多余的字符存入时间对象的 endstr 属性内。 

*/
var tm = time(str,"%Y年%m月%d日 %H时%M分%S秒","chs")

/*
time 对象凡是 add,diff 前缀的方法都基于数值（时间戳）运算，
而这个数值运算的有效范围是 1970 年 1 月 1 日 00:00:00 到 3000 年 12 月 31 日 23:59:59 ，
超出此范围的 time 对象使用  add,diff 前缀的方法会报错 datetime out of range!
但 time.ole 支持更大的数值运算范围（ `100/01/01` 到 `9999/12/31` 的时间）。

> aardio 代码默认使用小驼峰风格，
> time 对象的 add,diff 前缀方法是因为历史遗留原因保留了全小写风格。
*/
tm.addSeconds(30)
console.log(tm,"增加30秒")

tm.addMinutes(180)
console.log(tm,"增加180分")

tm.addHours(2)
console.log(tm,"增加两小时")

tm.addDays(365)
console.log(tm,"增加365天")

tm.addMonths(-24)
console.log(tm,"倒退24个月")

tm.addYears(2)
console.log(tm,"增加两年")
console.more()

var tm2 = time.now()

console.log( tm2.diffMonths(tm) ,"相差月份")
console.log( tm2.diffDays(tm) ,"相差天数")
console.log( tm2.diffHours(tm) ,"相差小时数")
console.log( tm2.diffMinutes(tm) ,"相差分钟数")
console.log( tm2.diffSeconds(tm) ,"相差秒数")
console.log( tonumber(tm) - tonumber(tm2) ,"相差秒数,作用同上")

/*
时间对象支持字符串连接操作符 ++，左右操作数都会转换字符串进行连接。
如果其中一个操作数是 null 值，则等价于将时间对象与空字符串（ "" ）连接。
*/
console.log( "时间值" ++ tm2  )

console.log('\n关系运算符,相等、不等')
console.log( "tm2==tm", tm2 == tm  )
console.log( "time.now()== time.now()", time.now() == time.now() )

console.log('\n关系运算符,大于、小于')
console.log( "tm2>tm", tm2 > tm  )
console.log( "time.now() > time.now()", time.now() > time.now() )

console.log('\n关系运算符,大于等于、小于等于')
console.log( "tm2 <= tm", tm2 <= tm  )
console.log( "time.now() <= time.now()", time.now() <= time.now() )

/*
time.gmt() 创建一个 time 对象,并且设置 HTTP 协议协容的 GMT 时间格式,并将该对象的格式化语言设为英文
*/
console.log( time.gmt() )
console.log( time.gmt( tm2.utc() ) ); //参数是其他 time 对象时,必须先转换为 UTC 时间

//HTTP 时间RFC 1123 格式，写错了空格这些自动修正
console.log(time.gmt("Sun,07Feb2016 081122 +7"))

//兼容HTTP时间RFC 850格式，星期写错了自动修正
console.log(time.gmt("Sunddddday, 07-Feb-16 08:11:22 +0700"))

//支持 ISO8601 省略分隔符的格式
console.log(time.iso8601("20170822 123623 +0700"))

//支持 ISO8601 省略分隔符的格式
console.log(time.iso8601("20170822 123623 +7"))

//兼容 ISO8601 省略分隔符的写法
console.log(time("20170822 123623"))

//省略分隔符的写法 14 位数字
console.log(time("20170822123623"))

//省略分隔符的写法 12 位数字
console.log(time("170822123623"))

/*
time 构造函数的默认格式串 '%Y/%m/%d %H:%M:%S' 不允许省略时间部分。
time.date 兼容 time 构造函数的所有用法，默认也会使用格式串  '%Y/%m/%d %H:%M:%S' 解析文本时间，
但 time.date 会在失败后尝试用短日期格式 '%Y-%m-%d'解析文本时间（仔然会宽松匹配不同的分隔符）。

并且 time.date 总是会将返回时间对象的格式串默认设为  '%Y-%m-%d'
*/
var date = time.date("2017-05-2")

// time.date 也可以用 3 个数值直接指定年、月、日值
date = time.date(2026/*year*/,1/*month*/,20/*day*/)

// time.iso8601 也支持短日期，但会解析为 UTC 时间，并且不会主动修改输出格式串（除非输入时使用短日期格式）
var isoDate = time.iso8601("2017-05-2")

import time.util;
console.log( time.util.isLeapYear(2000) )
console.log( time.util.getDaysInMonth(2000,1) )

//重置为当前时间，time 对象可作为 SYSTEMTIME 结构体使用
::Kernel32.GetSystemTime(tm)

//与 ::FILETIME 互转
var ftm = tm.toFileTime()
tm.fromFileTime(ftm)

console.pause()
 
```