C# Time Class using MySQL
http://www.csharphelp.com/2007/09/c-time-class/
- /*
- datatypes.
- Time class is writen in C# and .NET 2.0.
- Time class explantation.
- This is simple class and is not much to explain.
- 1.Public fields:
- .public int Hour,
- .public int Minute and
- .public int Second.
- .public const char TIME_SEPERATOR = ':'
- 2.Constructors:
- .current system time (public Time()),
- .from string value (public Time(string value)),
- .from parameters (public Time(int hour, int minute, int second)) and
- .from seconds (public Time(int seconds)).
- 3.
- Public method Add:
- Example 1:
- InDoc.Systems.Time time1 = new InDoc.Systems.Time("12:00:55");
- // calculate 12:00:55 + 14:55:20
- time1.Add("14:55:20");
- // result: 26:56:15
- 4. To addition two times you can use + and to subtraction you can use -.
- Example 2:
- InDoc.Systems.Time time1 = new InDoc.Systems.Time("12:00:55") +
- new InDoc.Systems.Time("14:55:20");
- // result: 26:56:15
- InDoc.Systems.Time time2 = new InDoc.Systems.Time("14:55:20") .
- new InDoc.Systems.Time("12:00:55");
- // result: 02:54:25
- 5. We have some convert methods:
- .public int ToSeconds(),
- .public override string ToString()
- and static method that convert secontd to Time object:
- .public static Time GetTimeFromSeconds(int seconds).
- */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace MysqlBig
- {
- /// <summary>
- ///
- /// </summary>
- public class Time
- {
- #region Public constants
- public const char TIME_SEPERATOR = ':';
- #endregion
- #region Declarations
- public int Hour;
- public int Minute;
- public int Second;
- #endregion
- #region Constructors
- /// <summary>
- /// Create time object from current system time.
- /// </summary>
- public Time()
- {
- Hour = DateTime.Now.Hour;
- Minute = DateTime.Now.Minute;
- Second = DateTime.Now.Second;
- }
- /// <summary>
- /// Create time object from string value must be seperated as TIME_SEPERATOR constant.
- /// </summary>
- /// <param name="value"></param>
- public Time(string value)
- {
- string[] vals = value.Split(TIME_SEPERATOR); //new char[] { ':' });
- Hour = int.Parse(vals[]);
- Minute = int.Parse(vals[]);
- if (vals.Length > )
- Second = int.Parse(vals[]);
- new Time(this.ToSeconds());
- }
- /// <summary>
- /// Create time object from parameters hour, minute and seconds.
- /// </summary>
- /// <param name="hour"></param>
- /// <param name="minute"></param>
- /// <param name="second"></param>
- public Time(int hour, int minute, int second)
- {
- Hour = hour;
- Minute = minute;
- Second = second;
- new Time(this.ToSeconds());
- }
- /// <summary>
- /// Create time object from seconds.
- /// </summary>
- /// <param name="seconds"></param>
- public Time(int seconds)
- {
- Minute = seconds / ;
- Second = seconds % ;
- Hour = Minute / ;
- Minute = Minute % ;
- }
- #endregion
- #region Public methods
- /// <summary>
- /// Add new time object and addition (+) it to previus time object.
- /// </summary>
- /// <param name="time"></param>
- /// <returns></returns>
- public Time Add(Time time)
- {
- this.Hour += time.Hour;
- this.Minute += time.Minute;
- this.Second += time.Second;
- return new Time(GetStringTime(this.ToSeconds()));
- }
- /// <summary>
- /// Add new string value and addition (+) it to previus time object.
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- public Time Add(string value)
- {
- return Add(new Time(value));
- }
- #endregion
- #region Public static methods
- /// <summary>
- /// Get current system time.
- /// </summary>
- /// <returns></returns>
- public static Time Now()
- {
- DateTime dt = DateTime.Now;
- return GetTimeFromSeconds(ToSeconds(dt));
- }
- /// <summary>
- /// Calculate time difference between two time objects.
- /// </summary>
- /// <param name="time1"></param>
- /// <param name="time2"></param>
- /// <returns></returns>
- public static Time TimeDiff(Time time1, Time time2)
- {
- try
- {
- int _secs1 = time1.ToSeconds();
- int _secs2 = time2.ToSeconds();
- int _secs = _secs1 - _secs2;
- return GetTimeFromSeconds(_secs);
- }
- catch
- {
- return new Time(, , );
- }
- }
- /// <summary>
- /// Calculate time difference between two string values.
- /// </summary>
- /// <param name="time1"></param>
- /// <param name="time2"></param>
- /// <returns></returns>
- public static Time TimeDiff(string time1, string time2)
- {
- try
- {
- Time t1 = new Time(time1);
- Time t2 = new Time(time2);
- return TimeDiff(t1, t2);
- }
- catch
- {
- return new Time(, , );
- }
- }
- /// <summary>
- /// Calculate time difference between two DateTime objects.
- /// </summary>
- /// <param name="dateTime1"></param>
- /// <param name="dateTime2"></param>
- /// <returns></returns>
- public static Time TimeDiff(DateTime dateTime1, DateTime dateTime2)
- {
- try
- {
- TimeSpan span = dateTime1 - dateTime2;
- return new Time(span.Seconds);
- }
- catch
- {
- return new Time(, , );
- }
- }
- /// <summary>
- /// Calculate time difference between two second values.
- /// </summary>
- /// <param name="seconds1"></param>
- /// <param name="seconds2"></param>
- /// <returns></returns>
- public static Time TimeDiff(int seconds1, int seconds2)
- {
- try
- {
- Time t1 = new Time(seconds1);
- Time t2 = new Time(seconds2);
- return TimeDiff(t1, t2);
- }
- catch
- {
- return new Time(, , );
- }
- }
- #endregion
- #region Convert methods
- /// <summary>
- /// Convert current time object to seconds.
- /// </summary>
- /// <returns></returns>
- public int ToSeconds()
- {
- return this.Hour * + this.Minute * + this.Second;
- }
- /// <summary>
- /// Convert DateTime object to seconds.
- /// </summary>
- /// <param name="dateTime"></param>
- /// <returns></returns>
- public static int ToSeconds(DateTime dateTime)
- {
- return dateTime.Hour * + dateTime.Minute * + dateTime.Second;
- }
- /// <summary>
- /// Convert current time object to string.
- /// </summary>
- /// <returns></returns>
- public override string ToString()
- {
- return String.Format("{0:00}:{1:00}:{2:00}", Hour, Minute, Second);
- }
- /// <summary>
- /// Convert seconds to time object.
- /// </summary>
- /// <param name="seconds"></param>
- /// <returns></returns>
- public static Time GetTimeFromSeconds(int seconds)
- {
- int _mins = seconds / ;
- seconds = seconds % ;
- int _hours = _mins / ;
- _mins = _mins % ;
- return new Time(_hours, _mins, seconds);
- }
- /// <summary>
- /// Convert seconds to string time.
- /// </summary>
- /// <param name="seconds"></param>
- /// <returns></returns>
- private string GetStringTime(int seconds)
- {
- int _mins = seconds / ;
- seconds = seconds % ;
- int _hours = _mins / ;
- _mins = _mins % ;
- this.Hour = _hours;
- this.Minute = _mins;
- this.Second = seconds;
- return String.Format("{0:00}:{1:00}:{2:00}", _hours, _mins, seconds); ;
- }
- /// <summary>
- /// Parse string to time.
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- public static Time Parse(string value)
- {
- try
- {
- return new Time(value);
- }
- catch
- {
- throw new ApplicationException("Error parsing time!");
- }
- }
- #endregion
- #region Subtract time objects
- public static Time operator +(Time t1, Time t2)
- {
- Time t3 = new Time(t1.Hour, t1.Minute, t1.Second);
- t3.Add(t2);
- return t3;
- }
- public static Time operator -(Time t1, Time t2)
- {
- return TimeDiff(t1, t2);
- }
- #endregion
- }
- }
用法:
- /// <summary>
- ///
- /// </summary>
- public class AttendrecordInfo
- {
- int _Seq;
- public int Seq
- {
- set { _Seq = value; }
- get { return _Seq; }
- }
- string _Emp_no;
- public string Emp_no
- {
- set { _Emp_no = value; }
- get { return _Emp_no; }
- }
- DateTime _Rdate;
- public DateTime Rdate
- {
- set { _Rdate = value; }
- get { return _Rdate; }
- }
- Time _Ttime;
- public Time Ttime
- {
- set { _Ttime = value; }
- get { return _Ttime; }
- }
- string _Rdescription;
- public string Rdescription
- {
- set { _Rdescription = value; }
- get { return _Rdescription; }
- }
- string _Rdes_reasnon;
- public string Rdes_reasnon
- {
- set { _Rdes_reasnon = value; }
- get { return _Rdes_reasnon; }
- }
- string _Branch;
- public string Branch
- {
- set { _Branch = value; }
- get { return _Branch; }
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="seq"></param>
- /// <returns></returns>
- public AttendrecordInfo SelectAttendrecord(int seq)
- {
- AttendrecordInfo attendrecord = null;
- try
- {
- MySqlParameter par = new MySqlParameter("?param1", MySqlDbType.Int32, );
- par.Value = seq;
- using (MySqlDataReader reader = MySqlHelpDu.GetReader("proc_Select_attendrecord", CommandType.StoredProcedure, par))
- {
- if (reader.Read())
- {
- attendrecord = new AttendrecordInfo();
- attendrecord.Seq = (!object.Equals(reader["seq"], null)) ? (int)reader["seq"] : ;
- attendrecord.Branch = (!object.Equals(reader["branch"], null)) ? (string)reader["branch"] : "";
- attendrecord.Emp_no = (!object.Equals(reader["emp_no"], null)) ? (string)reader["emp_no"] : "";
- attendrecord.Rdate = (!object.Equals(reader["rdate"], null)) ? DateTime.Parse(reader["rdate"].ToString()): DateTime.Now;
- attendrecord.Ttime = (!object.Equals(reader["rtime"], null)) ? Time.Parse(reader["rtime"].ToString()): Time.Now();
- attendrecord.Rdes_reasnon = (!object.Equals(reader["rdes_reasnon"], null)) ? (string)reader["rdes_reasnon"] : "";
- attendrecord.Rdescription = (!object.Equals(reader["rdescription"], null)) ? (string)reader["rdescription"] : "";
- }
- }
- }
- catch (MySqlException ex)
- {
- throw ex;
- }
- return attendrecord;
- }
Mysql 表:
- create table attendrecord
- (
- seq INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
- emp_no varchar(20) null,
- rdate datetime not null,
- rtime time not null,
- rdescription varchar(100),
- rdes_reasnon varchar(100),
- branch varchar(50)
- );
- insert into attendrecord(emp_no,rdate,rtime,rdescription,rdes_reasnon,branch) values('L00094','2015-03-10','10:45','geovindu','du','sz');
C# Time Class using MySQL的更多相关文章
- Hadoop 中利用 mapreduce 读写 mysql 数据
Hadoop 中利用 mapreduce 读写 mysql 数据 有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...
- mysql每秒最多能插入多少条数据 ? 死磕性能压测
前段时间搞优化,最后瓶颈发现都在数据库单点上. 问DBA,给我的写入答案是在1W(机械硬盘)左右. 联想起前几天infoQ上一篇文章说他们最好的硬件写入速度在2W后也无法提高(SSD硬盘) 但这东西感 ...
- LINUX篇,设置MYSQL远程访问实用版
每次设置root和远程访问都容易出现问题, 总结了个通用方法, 关键在于实用 step1: # mysql -u root mysql mysql> Grant all privileges o ...
- nodejs进阶(6)—连接MySQL数据库
1. 建库连库 连接MySQL数据库需要安装支持 npm install mysql 我们需要提前安装按mysql sever端 建一个数据库mydb1 mysql> CREATE DATABA ...
- MySQL高级知识- MySQL的架构介绍
[TOC] 1.MySQL 简介 概述 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司. MySQL是一种关联数据库管理系统,将数据保存在不同的表中,而 ...
- 闰秒导致MySQL服务器的CPU sys过高
今天,有个哥们碰到一个问题,他有一个从库,只要是启动MySQL,CPU使用率就非常高,其中sys占比也比较高,具体可见下图. 注意:他的生产环境是物理机,单个CPU,4个Core. 于是,他抓取了CP ...
- 我的MYSQL学习心得(一) 简单语法
我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(五) 运 ...
- Entity Framework Core 实现MySQL 的TimeStamp/RowVersion 并发控制
将通用的序列号生成器库 从SQL Server迁移到Mysql 遇到的一个问题,就是TimeStamp/RowVersion并发控制类型在非Microsoft SQL Server数据库中的实现.SQ ...
- Docker笔记一:基于Docker容器构建并运行 nginx + php + mysql ( mariadb ) 服务环境
首先为什么要自己编写Dockerfile来构建 nginx.php.mariadb这三个镜像呢?一是希望更深入了解Dockerfile的使用,也就能初步了解docker镜像是如何被构建的:二是希望将来 ...
- 当忘记mysql数据库密码时如何进行修改
因为长时间没有使用数据库了,或者把密码改完之后就忘了数据库密码,不能正常进入数据库,也无法修改密码,有一个简单的常用修改密码方式: 1.首先找到和打开mysql.exe和mysqld.exe所在的文件 ...
随机推荐
- byte转文件流 下载到本地
此方法将byte类型文件转为文件流保存到本地 byte 经过BASE64Decoder 进行编码之后的类型 所以需要解码 防止出现乱码及文件损毁 /** * byte 转文件 下载到本地 * @par ...
- 计算机网络课设之TCP通讯录
这篇文章我主要是想对这学期末计算机网络课程设计所做的一个小项目也就是基于tcp协议的通讯录做一个总结梳理.项目的具体的代码实现是基于C语言,当然在此之前网上也有一些基于c++编写的tcp通讯录,原理都 ...
- tomcat运行springboot项目war包
以最简单的spring boot demo项目来演示如何发布项目war包到tomcat,并成功运行(有很多小伙伴会出现404错误) 一.准备一个最简单的demo项目 在IDEA中新建一个项目,一直ne ...
- 架构师养成记--19.netty
一.Netty初步 为什么选择Netty? 和NIO比较,要实现一个通信要简单得很多,性能很好.分布式消息中间件.storm.Dubble都是使用Netty作为底层通信. Netty5.0要求jdk1 ...
- C#(同步调用、异步调用、异步回调)
Review: 原作者虽然使用了汉字的类名,看起来十分蹩脚,但是,还是把同步调用.异步调用.异步回调的使用讲解的很详细的.原理讲解的很清晰. ------ 本文将主要通过“同步调用”.“异步调用”.“ ...
- 配置bootstrap环境
bootstrap是一个优雅,灵活,可扩展的前端工具集,可搭建WEB页面的HTML,CSS,JavaScript工具集,最重要的是它的栅格系统. 这里不做更多的详细介绍具体可参照官方网站:http:/ ...
- Scrum Meeting 汇总
Alpha [Alpha]Scrum Meeting 0&1 [Alpha]Scrum Meeting 2 [Alpha]Scrum Meeting 3 [Alpha]Scrum Meetin ...
- JS实现值复制
在JS中对象一般都是传地址,后续修改也会影响原始数据.例如这样. var a={ b:"b" }; var c=a; c.b="c"; console.log( ...
- 阅读The Java® Language Specification需要知道的术语
Null Pointer Exception,简称NPE 在java中,static final修饰的是常量.根据编译器的不同行为,常量又可分为编译时常量和运行时常量. 举例说明吧 public st ...
- 实现Date函数属性中的format方法
js中没有Date.format方法的,所以在date属性中加format方法 //js格式化属性 Date.prototype.format = function (format) { var o ...